django2.0 uwsgi nginx
[TOC]
# 1.安装pip
```
sudo apt-get update
sudo apt-get install python-pip
```
# 2.使用pip 安装virtualenv 和 virtualenvwrapper
```
sudo pip install virtualenv virtualenvwrapper
```
# 3.配置环境变量
```
echo "export WORKON_HOME=~/Env" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
# 如果是zsh
# echo "export WORKON_HOME=~/Env" >> ~/.zshrc
# echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.zshrc
```
# 4.配置生效
```
source ~/.bashrc
# 如果是zsh
#source ~/.zshrc
```
# 5.创建虚拟目录
```
# python 2.7 默认
mkvirtualenv python_health
# 指定 python 3.5
mkvirtualenv -p /usr/bin/python3.5 python_mall
```
# 6.安装django 等其他包
```
# pip install django
# ... ...
pip install -r requirements.txt
```
# 7.退出virtualenv环境
```
deactivate
```
如果您想再次进入可以` workon python_health
# 8.安装uWSGI
```
sudo apt-get install python-dev
sudo pip install uwsgi
```
# 9.设置配置文件
```
sudo mkdir -p /etc/uwsgi/sites
cd /etc/uwsgi/sites
```
# 10.编写项目ini文件
```
sudo vi python_health.ini
```
```
[uwsgi]
project = python_health
uid = teresa
base = /home/%(uid)
chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application
master = true
processes = 5
socket = /run/uwsgi/%(project).sock
chown-socket = %(uid):www-data
chmod-socket = 660
vacuum = true
```
# 11. 创建系统unit file 为uWSGI
```
sudo vi /etc/systemd/system/uwsgi.service
```
```
[Unit]
Description=uWSGI Emperor service
[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown teresa:www-data /run/uwsgi'
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
```
# 12.配置nginx
```
sudo apt-get install nginx
sudo vi /etc/nginx/sites-available/python_health
```
```
server {
listen 80;
server_name baigedata.cn;
location = /favicon.ico { access_log off; log_not_found off;}
location /static/ {
root /home/teresa/python_health;
}
location /media/ {
root /home/teresa/python_health;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/python_health.sock;
}
}
```
# 13.创建软连接到enabled目录
```
sudo ln -s /etc/nginx/sites-available/python_health /etc/nginx/sites-enabled
```
重复一遍检查是否存在
# 14.重启nginx
```
sudo systemctl restart nginx
```
# 15 启动uwsgi
```
sudo systemctl start uwsgi
```
# 16 自动启动
```
sudo systemctl enable nginx
sudo systemctl enable uwsgi
```