Djanngo项目部署到服务器

Djanngo项目部署到服务器

virtualenv虚拟机

安装命令

1
sudo apt install python3-virtualenv

创建虚拟机

1
virtualenv env

:env是虚拟机名

启动虚拟机

1
source env/bin/activate

安装需要python包,如Django

1
pip install django

运行python项目

使用git把包传进服务器

进入项目目录启动django

1
python manage.py runserver

1
python manage.py runserver 0.0.0.0:8000

把静态文件放到指定目录

如 /home/static

:不建议放root目录下,可能没权限访问

项目setting.py里设置

1
2
3
4
STATIC_URL = '/static/'

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
STATIC_ROOT = '/home/static/'

然后收集静态资源文件

1
python manage.py collectstatic

安装和配置nginx

安装命令

1
sudo apt install nginx

nginx目录/etc/nginx

1
cd /etc/nginx

进入sites-enabled目录

1
cd /etc/nginx/sites-enabled

里面的default文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name _;

location / {
# First attempt to serve request as file, then

不管它,直接删除

1
rm default

新建一个配置文件

1
vim cf.conf

添加到文件里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
server_name www.com; #这里改为你的域名

# 新添加的静态文件处理块
location /static/ {
alias /home/static/; # 替换为您的STATIC_ROOT路径
expires 30d;
access_log on;
add_header Cache-Control "public";
}

location / {
include proxy_params;
proxy_pass http://127.0.0.1:8000;
}

}

保存好文件

1
2
sudo nginx -t
#检查nginx配置语法,成功有successful字样

重载nginx

1
2
3
4
5
6
7
sudo systemctl reload nginx
#或者
sudo nginx -s reload


sudo systemctl start nginx # 启动nginx
sudo systemctl stop nginx # 关闭nginx

开启80和443端口

1
2
3
4
5
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

#查看防火墙状态
sudo ufw status

安装SSL证书并启用HTTPS

1. 安装Certbot和Nginx插件

1
sudo apt install certbot python3-certbot-nginx

2. 获取SSL证书

1
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

(将yourdomain.com替换为你的实际域名)

3. 自动续期测试

1
sudo certbot renew --dry-run

4. 修改Nginx配置自动重定向HTTP到HTTPS

在之前的nginx配置中添加:

1
2
3
4
5
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}

5. 重启Nginx使配置生效

1
sudo systemctl restart nginx

这样就完成了HTTPS的配置,Certbot会自动处理证书续期问题。

注意:在执行这些命令前,请确保:

  1. 域名已解析到服务器IP
  2. 80和443端口已开放
  3. Nginx配置中的server_name与域名一致