1. 安装Nginx
- 安装
sudo apt-get install nginx
- 启动服务
sudo service nginx start
sudo service nginx stop
sudo service nginx restart
或者
sudo /etc/init.d/ngnix start
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx restart
- 查看错误日志
grep /var/log/nginx/error.log
2. 安装uWSGI
- 安装python-dev
apt-get install python-dev
- 安装uWSGI
pip install uwsgi
- 测试uWSGI
使用函数测试uWSGI是否正常,新建test.py文件,定义如下函数:
def application(env, start_response):
start_response('200 OK', [('Contest-Type', 'text/html')] )
return [b'uWSGI is Good!'] #python3
#return ['uWSGI is Good!'] #python2
测试uWSGI,如果显示正确,则说明 web client <-> uWSGI <-> Python 这三个环节是畅通的:
uwsgi --http :8080 --wsgi-file test.py
参数说明:
1. http :8080 : 使用http协议,端口号为8080
2. wsgi-file test.py : 加载指定的文件
- 测试Django项目
通过测试确定 web client <-> uWSGI <-> Django 这三个环节是否通畅,执行以下语句需要先进入到Django项目目录
uwsgi --http :8080 --module mysite.uwsg
3. 配置Nginx
3.1 增加服务器配置
- 将/etc/nginx/下的 uwsgi_params 文件拷贝到项目目录下。
- 在项目目录下创建配置文件mysite_nginx.conf,并写入以下内容:
# the upstream component nginx needs to connect to
upstream django {
# server unix:///home/wfc/works/Django/LicenseSite/license.sock; #使用socket文件
server 127.0.0.1:8080; # 使用TCP端口
}
# configuration of the server
server {
# the port your site will be served on
listen 8080;
# the domain name it will serve for
server_name 127.0.0.1; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /static {
alias /home/wfc/works/Django/LicenseSite/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/wfc/works/Django/LicenseSite/uwsgi_params; # the uwsgi_params file you installed
}
}
- 在 /etc/nginx/sites-enabled/ 目录下创建配置文件的连接
sudo ln -s /home/wfc/works/Django/LicenseSite/mysite_nginx.conf /etc/nginx/sites-enabled/
3.2 部署static文件
在django项目的setting文件中,添加一行内容:
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
运行collectstatic,主要将admin下静态文件拷贝到项目目录,否则打开的网页没有格式效果:
python manage.py collectstatic
3.3 测试Nginx
- 重启服务
sudo service nginx restart
- 测试静态文件是否显示正常
访问 http://127.0.0.1:8080/static/image.jpg
4. 使用Unix socket代替TCP port
- 修改配置文件
取消 server unix 前的注释,并将 server 127. 注释掉
server unix:///home/wfc/works/Django/LicenseSite/license.sock; #使用socket文件
#server 127.0.0.1:8080; # 使用TCP端口
- 重启Nginx并运行uWSGI
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=666 # (very permissive)
或者
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664 # (more sensible)
5. 使用.ini文件运行uWSGI
- 在项目目录下创建 mysite_uwsgi.ini 文件
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /home/wfc/works/Django/LicenseSite/
# Django's wsgi file
module = LicenseSite.wsgi
# the virtualenv (full path)
home = /home/wfc/works/Django/LicenseSite/venv/
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /home/wfc/works/Django/LicenseSite/license.sock
# ... with appropriate permissions - may be needed
chmod-socket = 666
# clear environment on exit
vacuum = true
- 启动uWSGI
uwsgi --ini mysite_uwsgi.ini
6. 设置uWSGI自启动
- 在 /etc/systemd/system/ 目录下创建服务配置文件 mysite.service
[Unit]
Description=uWSGI instance
After=network.target
[Service]
User=wfc
WorkingDirectory=/home/wfc/works/Django/LicenseSite
Enviroment=/home/wfc/works/Django/LicenseSite/venv/pyvenv.cfg
ExecStart=/home/wfc/.local/bin/uwsgi --ini /home/wfc/works/Django/LicenseSite/license_uwsgi.ini
[Install]
WantedBy=multi-user.target
- 设置项目自启动
sudo systemctl enable mysite
- 服务的其他操作
sudo systemctl start epolicense
sudo systemctl restart epolicense
sudo systemctl stop epolicense
sudo systemctl status epolicense