初学 Python 语言,想用 Django 写个例子来练练手,写没用几天, 但部署 Nginx+Django 时花费了很多时间,弄完之后特总结一下,希望对你有帮助
本教程只针对 Python3.* 版本,Python2 没有试验,
pip3 install virtualenv
若有虚拟环境,如 pycharm 生成Django项目时自带 virtualenv ,可跳过此步,到下一步安装、配置uWSGI
virtualenv --no-site-packages venv
source /venv路径/bin/activate
pip3 install django
pip3 install mysqlclient
...
安装 uWSI 时一定要把 python 环境切换到项目所以依赖的虚拟环境,切换方法如上 2.2
pip3 install uwsgi # 网上可能会安装失败,反正我没安装失败,也就不多解释了
uwsgi --http 0:8000 --chdir /项目根目录 --wsgi-file /项目中wsgi文件的路径/wsgi.py --home /项目虚拟换进的目录
#例如我的
# uwsgi --http 0:8000 --chdir /home/mrwang/code/python/jianshu --wsgi-file /home/mrwang/code/python/jianshu/JianShu/wsgi.py --home /home/mrwang/code/python/jianshu/venv/
若提示端口占用,可以更换端口或者使用kill
命令自行解决,当启动后会有如下提示, 仅做参考
*** Starting uWSGI 2.0.17.1 (64bit) on [Mon Jul 23 14:11:35 2018] ***
compiled with version: 7.3.0 on 21 July 2018 11:22:07
os: Linux-4.15.0-29-generic #31-Ubuntu SMP Tue Jul 17 15:39:52 UTC 2018
nodename: mrwang-ThinkPad-E560
machine: x86_64
clock source: unix
detected number of CPU cores: 4
current working directory: /home/mrwang
detected binary path: /home/mrwang/code/python/jianshu/venv/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/mrwang/code/python/jianshu
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 30972
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 http bound on 0:8000 fd 4
spawned uWSGI http 1 (pid: 7159)
uwsgi socket 0 bound to TCP address 127.0.0.1:36613 (port auto-assigned) fd 3
Python version: 3.6.5 (default, Apr 1 2018, 05:46:30) [GCC 7.3.0]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x564216057eb0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72904 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x564216057eb0 pid: 7158 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 7158, cores: 1)
之后访问浏览器 127.0.0.1:8000
端口即可看到你的项目了
我们可以写配置文件的方式来简化启动方式,在项目根目录建立uWSGI配置文件 test.ini
如下
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# 项目根目录的路径
chdir = /home/mrwang/code/python/jianshu
# Django's wsgi file
#module = /home/mrwang/code/python/jianshu/JianShu/wsgi.py
wsgi-file = /home/mrwang/code/python/jianshu/JianShu/wsgi.py
# the virtualenv (full path)
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# 当配置uWSGI+Nginx作为服务器时需要关闭http或者换一个端口,如8001
#socket = :8000
# 当单使用 uWSGI 作为服务器时使用http
http = :8000
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
# 虚拟环境的目录
home = /home/mrwang/code/python/jianshu/venv/
# 设置缓存大小
buffer-size = 65536
保存配置文件后, 就可以根据配置文件来启动uWSGI了, 访问 127.0.0.1:8000
查看结果
uwsgi -i /配置文件路径/test.ini
sudo apt install nginx
nginx 常用命令
sudo service nginx start #启动服务器
sudo service nginx stop #停止服务器
sudo service nginx restart #重启服务器
启动服务起后访问 127.0.0.1
出现 Welcome to nginx!
字样说明nginx安装成功
在uWSGI配置文件中开启socket 关闭 http或者socket更换端口, 这里将http关闭
默认安装的 Nginx 会在 Ubuntu 中的 /etc/nginx
目录下
# 进入 `sites-available` 目录
cd /etc/nginx/sites-available
# 创建 test 文件
sudo vim test
# 向文件中添加如下内容
server {
listen 80;
server_name jsc.cn;
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass 127.0.0.1:8000; # 该端口好一定要跟uwsgi 中的 socket 一致
}
location /static {
alias /项目跟目录/static; # 指向django的static目录
}
}
#保存退出后,将 test 链接到 `sites-enabled` 文件中
sudo ln -s /etc/nginx/sites-available/jianshu /etc/nginx/sites-enabled/
完成上述功能后, 重启Nginx服务器, 启动 uWSGI 服务器, 访问 127.0.0.0.1
就会出现 Django 项目的界面了。