接下来我将为您讲解“django2+uwsgi+nginx上线部署到服务器Ubuntu16.04”的完整攻略。
准备工作
在进行上线部署前,我们需要先做好一些准备工作:
- 确认服务器已安装Ubuntu16.04操作系统。
- 安装必要的软件包,如Python3、pip、virtualenv、nginx、uwsgi和git等。
- 在服务器上创建项目文件夹,并将Django项目上传至该文件夹中。
- 配置服务器的防火墙,以允许访问80端口。
安装uwsgi
-
使用pip安装uwsgi:
pip3 install uwsgi
-
在项目的根目录下,创建uwsgi配置文件uwsgi.ini:
[uwsgi]
chdir = /path/to/your/project
module = project.wsgi:application
master = true
processes = 4
socket = /tmp/uwsgi.sock
chmod-socket = 664
vacuum = true
- 测试uwsgi是否能够正常启动:
uwsgi --ini uwsgi.ini
如果成功启动,会输出如下信息:
*** Starting uWSGI 2.0.14 (64bit) on [Wed Sep 14 15:51:20 2016] ***
compiled with version: 5.4.0 20160609 on 22 August 2016 15:07:47
os: Linux-4.4.0-36-generic #55-Ubuntu SMP Thu Aug 11 18:01:55 UTC 2016
nodename: ubuntu
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /path/to/your/project
detected binary path: /usr/local/bin/uwsgi
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7180
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /tmp/uwsgi.sock fd 3
Python version: 3.5.2 (default, Aug 25 2016, 11:11:35) [GCC 5.4.0 20160609]
Set PythonHome to /home/username/.virtualenvs/project
Python main interpreter initialized at 0x1f05410
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72752 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
uWSGI http bound on :9000 fd 4
spawned uWSGI http 1 (pid: 18688)
*** WARNING: you are running uWSGI without its master process manager ***
*** Spawned uWSGI http 1 (pid: 18688) ***
- 停止uwsgi服务:
uwsgi --stop /tmp/uwsgi.pid
配置nginx
- 修改nginx配置文件/etc/nginx/sites-available/default:
```
server {
listen 80;
server_name yourdomain.com;
charset utf-8;
client_max_body_size 75M;
location / {
uwsgi_pass unix:///tmp/uwsgi.sock;
include uwsgi_params;
}
location /static/ {
alias /path/to/your/static/files/;
}
location /media/ {
alias /path/to/your/media/files/;
}
}
```
- 检查配置文件是否正确:
sudo nginx -t
如果配置文件正确无误,会输出如下信息:
nginx: configuration file /etc/nginx/nginx.conf test is successful
- 启动nginx服务:
sudo systemctl start nginx
部署Django项目
-
在服务器上克隆Django项目代码库:
git clone git@github.com:yourusername/yourproject.git
-
为Django项目创建Python虚拟环境:
virtualenv -p /usr/bin/python3 env
-
激活虚拟环境:
source env/bin/activate
-
安装项目所需的Python包:
pip3 install -r requirements.txt
-
运行集成测试,确保项目可以在服务器上成功运行:
python3 manage.py test
-
创建Django项目的数据库:
python3 manage.py migrate
-
收集静态文件:
python3 manage.py collectstatic --noinput
-
启动uwsgi服务:
uwsgi --ini uwsgi.ini
-
确认项目已经成功部署:
curl http://localhost
如果返回了Django项目的欢迎页面,表明项目已经成功部署到服务器上。
示例说明
以下是两个示例,分别针对不同的场景:
示例一:部署单个Django项目
假设我们现在需要将单个Django项目部署到服务器上。我们可以按照以下步骤进行操作:
-
在服务器上创建目录,用于存放Django项目:
mkdir /path/to/project
-
将Django项目上传到该目录下。
-
安装必要的软件包:
sudo apt-get update
sudo apt-get install python3 python3-pip nginx
sudo pip3 install virtualenv uwsgi -
创建Python虚拟环境:
virtualenv -p /usr/bin/python3 env
-
激活虚拟环境:
source env/bin/activate
-
安装项目所需的Python包:
pip3 install -r requirements.txt
-
创建数据库:
python3 manage.py migrate
-
收集静态文件:
python3 manage.py collectstatic --noinput
-
创建uwsgi配置文件uwsgi.ini:
[uwsgi]
chdir = /path/to/project
module = project.wsgi:application
master = true
processes = 4
socket = /tmp/uwsgi.sock
chmod-socket = 664
vacuum = true -
创建nginx配置文件/etc/nginx/sites-available/default:
```
server {
listen 80;
server_name yourdomain.com;location / { uwsgi_pass unix:///tmp/uwsgi.sock; include uwsgi_params; } location /static/ { alias /path/to/project/static/; }
}
``` -
创建软链接:
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/
-
测试nginx是否能够正常启动:
sudo nginx -t
-
启动uwsgi服务:
uwsgi --ini uwsgi.ini
-
启动nginx服务:
sudo systemctl start nginx
-
确认项目已经成功部署:
curl http://localhost
示例二:部署多个Django项目
假设我们现在需要将多个Django项目部署到服务器上。我们可以按照以下步骤进行操作:
-
在服务器上创建目录,用于存放多个Django项目:
mkdir /path/to/projects
-
将各个Django项目上传到该目录下。
-
安装必要的软件包:
sudo apt-get update
sudo apt-get install python3 python3-pip nginx
sudo pip3 install virtualenv uwsgi -
为每个Django项目创建Python虚拟环境,分别命名为env1、env2、env3等:
virtualenv -p /usr/bin/python3 env1
virtualenv -p /usr/bin/python3 env2
virtualenv -p /usr/bin/python3 env3 -
激活第一个项目的虚拟环境:
source env1/bin/activate
-
安装第一个项目所需的Python包:
pip3 install -r requirements.txt
-
创建第一个项目的数据库:
python3 manage.py migrate
-
收集第一个项目的静态文件:
python3 manage.py collectstatic --noinput
-
创建uwsgi配置文件uwsgi1.ini:
[uwsgi]
chdir = /path/to/projects/project1
module = project.wsgi:application
master = true
processes = 4
socket = /tmp/uwsgi1.sock
chmod-socket = 664
vacuum = true -
创建nginx配置文件/etc/nginx/sites-available/project1:
```
server {
listen 80;
server_name yourdomain1.com;location / { uwsgi_pass unix:///tmp/uwsgi1.sock; include uwsgi_params; } location /static/ { alias /path/to/projects/project1/static/; }
}
``` -
创建软链接:
sudo ln -s /etc/nginx/sites-available/project1 /etc/nginx/sites-enabled/
-
激活第二个项目的虚拟环境:
source env2/bin/activate
-
安装第二个项目所需的Python包:
pip3 install -r requirements.txt
-
创建第二个项目的数据库:
python3 manage.py migrate
-
收集第二个项目的静态文件:
python3 manage.py collectstatic --noinput
-
创建uwsgi配置文件uwsgi2.ini:
[uwsgi]
chdir = /path/to/projects/project2
module = project.wsgi:application
master = true
processes = 4
socket = /tmp/uwsgi2.sock
chmod-socket = 664
vacuum = true -
创建nginx配置文件/etc/nginx/sites-available/project2:
```
server {
listen 80;
server_name yourdomain2.com;location / { uwsgi_pass unix:///tmp/uwsgi2.sock; include uwsgi_params; } location /static/ { alias /path/to/projects/project2/static/; }
}
``` -
创建软链接:
sudo ln -s /etc/nginx/sites-available/project2 /etc/nginx/sites-enabled/
-
激活第三个项目的虚拟环境:
source env3/bin/activate
-
安装第三个项目所需的Python包:
pip3 install -r requirements.txt
-
创建第三个项目的数据库:
python3 manage.py migrate
-
收集第三个项目的静态文件:
python3 manage.py collectstatic --noinput
-
创建uwsgi配置文件uwsgi3.ini:
[uwsgi]
chdir = /path/to/projects/project3
module = project.wsgi:application
master = true
processes = 4
socket = /tmp/uwsgi3.sock
chmod-socket = 664
vacuum = true -
创建nginx配置文件/etc/nginx/sites-available/project3:
```
server {
listen 80;
server_name yourdomain3.com;location / { uwsgi_pass unix:///tmp/uwsgi3.sock; include uwsgi_params; } location /static/ { alias /path/to/projects/project3/static/; }
}
``` -
创建软链接:
sudo ln -s /etc/nginx/sites-available/project3 /etc/nginx/sites-enabled/
-
测试nginx配置文件是否正确:
sudo nginx -t
-
启动uwsgi服务:
uwsgi --ini uwsgi1.ini
uwsgi --ini uwsgi2.ini
uwsgi --ini uwsgi3.ini -
启动nginx服务:
sudo systemctl start nginx
-
确认项目已经成功部署:
curl http://yourdomain1.com
curl http://yourdomain2.com
curl http://yourdomain3.com
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:django2+uwsgi+nginx上线部署到服务器Ubuntu16.04 - Python技术站