Ubuntu+Gunicorn部署Django
先安装一些依赖包
1 2 3 4 5 6 7
| sudo apt-get update sudo apt-get upgrade sudo apt-get install nginx sudo apt-get install python3 sudo apt-get install python3-pip sudo pip3 install virtualenv sudo apt install mysql-server
|
测试MySQL能否运行并创建数据库
1 2 3 4 5 6 7 8 9 10 11
| sudo mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword'; mysql> FLUSH PRIVILEGES;
exit;
mysql -u root -p # 测试链接
mysql> CREATE DATABASE IF NOT EXISTS n DEFAULT CHARSET utf8; # 创建表
|
在djangoProject1/settings.py
修改以下配置。
1 2 3 4 5 6
| DEBUG = False ALLOWED_HOSTS = ['*'] # 静态文件收集目录 STATIC_ROOT = os.path.join(BASE_DIR, 'nb_static')
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| INSTALLED_APPS = [ ...... 'corsheaders', ...... ] MIDDLEWARE = [ ...... 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', ...... ] CORS_ORIGIN_ALLOW_ALL = True CORS_ALLOW_CREDENTIALS = True SECURE_CROSS_ORIGIN_OPENER_POLICY = 'None'
|
1 2 3 4 5 6 7 8 9 10
| DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'djtest', 'HOST': '127.0.0.1', 'PORT': 3306, 'USER': 'root', 'PASSWORD': '123456', } }
|
1
| pip freeze > requirements.txt # 本地导出环境
|
- 将项目上传到服务器
- 进入服务器项目文件夹
1 2 3 4 5
| cd / mkdir /envs virtualenv /envs/nb --python=python3.10 # 创建虚拟环境
source /envs/nb/bin/activate # 启动虚拟环境
|
1
| pip3 install -r requirements.txt # 安装python库,如无法成功可逐一安装
|
安装库慢的时候可通过豆瓣源(将xxxx处替换为包名也可以加版本)
pip install xxxx -i http://pypi.douban.com/simple/ –trusted-host pypi.douban.com
1 2
| python3 manage.py collectstatic # 收集静态文件 python3 manage.py migrate # 创建数据库
|
配置nginx
- 分别进入
/etc/nginx/sites-available
,/etc/nginx/sites-enabled
两个文件夹输入命令:sudo rm -r default
删除default
文件。
修改为root
1 2
| cd /etc/nginx vim nginx.conf
|
1 2 3
| cd /etc/nginx/sites-available
vim nbiot
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| server { charset utf-8 listen 80 server_name 47.109.35.113 location /static { alias /root/nbiot/nb_static } location / { proxy_set_header Host $host proxy_pass http://unix:/tmp/47.109.35.113.socket } }
|
1 2 3
| sudo ln -s /etc/nginx/sites-available/nbiot /etc/nginx/sites-enabled
sudo service nginx reload # 刷新nginx配置
|
1 2 3 4
| source /envs/nb/bin/activate python manage.py createsuperuser pip3 install gunicorn gunicorn --bind unix:/tmp/47.109.35.113.socket djangoProject1.wsgi:application
|
1 2
| pstree -ap|grep gunicorn kill -9 进程号
|
1
| python3 manage.py runserver # 命令运行程序并测试
|
https部署
- 先在云服务器官网完成ssl申请并下载验证文件,将key文件放在下面目录
- 删除原来的配置文件
/etc/nginx/sites-available
,/etc/nginx/sites-enabled
两个文件
1 2 3
| cd /etc/nginx/sites-available
vim nbiot
|
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
| server { listen 80; server_name aiold.cloud; return 301 https://$server_name$request_uri; }
server { listen 443 ssl; server_name aiold.cloud www.aiold.cloud;
ssl_certificate /usr/share/nginx/ssl/www.aiold.cloud.pem; ssl_certificate_key /usr/share/nginx/ssl/www.aiold.cloud.key;
location / { proxy_pass http://unix:/tmp/47.109.35.113.socket; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto https; proxy_redirect off; }
location /static { alias /root/nbiot/nb_static/; expires 1d; add_header Cache-Control "public"; add_header Vary "Accept-Encoding"; try_files $uri $uri/ =404; }
location /MO7Xwo9amq.txt { alias /var/www/html/MO7Xwo9amq.txt; } }
|
1
| sudo apt-get install python3-openssl # 安装python3-openssl包
|
1
| sudo ln -s /etc/nginx/sites-available/nbiot /etc/nginx/sites-enabled/
|
1
| sudo systemctl restart nginx
|
若部署为https后前端样式丢失,可能是一些静态文件没有成功加载,对此将其导入修改为CDN
1 2 3
| <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
|