Ubuntu下 Virtualenv+Django+uWSGI+Nginx 项目部署

初学 Python 语言,想用 Django 写个例子来练练手,写没用几天, 但部署 Nginx+Django 时花费了不少时间,弄完以后特总结一下,但愿对你有帮助node

1、安装Virtualenv

本教程只针对 Python3.* 版本,Python2 没有试验,python

pip3 install virtualenv

2、建立虚拟环境

如有虚拟环境,如 pycharm 生成Django项目时自带 virtualenv ,可跳过此步,到下一步安装、配置uWSGImysql

1. 建立虚拟 python 环境 virtualev
virtualenv --no-site-packages venv
2. 将当前的Python环境切换到虚拟环境
source /venv路径/bin/activate
3. 安装项目所需的扩展
pip3 install django
pip3 install mysqlclient
...

3、安装、配置uWSGI

安装 uWSI 时必定要把 python 环境切换到项目因此依赖的虚拟环境,切换方法如上 2.2nginx

1. 安装 uWSGI
pip3 install uwsgi   # 网上可能会安装失败,反正我没安装失败,也就很少解释了
2. 经过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 命令自行解决,当启动后会有以下提示, 仅作参考web

*** 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 端口便可看到你的项目了sql

3. 写配置文件来启动项目

咱们能够写配置文件的方式来简化启动方式,在项目根目录创建uWSGI配置文件 test.ini 以下django

# 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 查看结果vim

uwsgi -i /配置文件路径/test.ini

4、安装Nginx

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安装成功缓存

5、配置Nginx+uWSGI

在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 项目的界面了。