应 云友 - “mango爱好 ” 在 帖子 要求,找在 CentOS 7 系统安装 nginx + uWSGI + Diango 的资料。
找到一篇英文的,粗略翻译及实践如下,此例中是假设将相应的文件放在 liujia 用户目录下(/home/liujia),以普通用户 liujia 用户登录到控制台:
1.添加 EPEL 软件源:
sudo yum install epel-release
2.安装 pip 工具:
sudo yum install python-pip
3.安装 python 虚拟环境工具:
sudo pip install virtualenv virtualenvwrapper
4.更改bash Shell的设置:
echo "export WORKON_HOME=~/Env" >> ~/.bashrc
echo "source /usr/bin/virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc
5.创建第一个python应用:
mkvirtualenv firstsite
6.安装 diango
pip install django
7.生成相应的目录:
cd ~
django-admin.py startproject firstsite
8.完成基本设置,如设置管理用户名和密码:
cd ~/firstsite
./manage.py migrate
./manage.py createsuperuser
9.编辑配置文件,在末行添加静态文件目录:
vi firstsite/settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
10.更新应用设置,自动生成 static 目录:
./manage.py collectstatic
11.测试应用是否能正常运行,如果在浏览器里能访问8080端口的内容,即成功:
./manage.py runserver 0.0.0.0:8080
12.安装 python 开发包:
sudo yum install python-devel gcc
13.安装uWSGI:
sudo pip install uwsgi
14.测试 uWSGi 是否能正常运行:
uwsgi --http :8080 --home /home/user/Env/firstsite --chdir /home/user/firstsite -w firstsite.wsgi
15.创建 uWSGI 运行的配置文件 firstsite.ini:
sudo mkdir -p /etc/uwsgi/sites
cd /etc/uwsgi/sites
sudo vi firstsite.ini
配置文件内容如下:
[uwsgi]
project = firstsite
username = liujia
base = /home/%(username)
chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application
master = true
processes = 5
uid = %(username)
socket = /run/uwsgi/%(project).sock
chown-socket = %(username):nginx
chmod-socket = 660
vacuum = true
16.创建 uWSGI 启动脚本 uwsgi.service:
sudo vi /etc/systemd/system/uwsgi.service
文件内容如下:
[Unit]
Description=uWSGI Emperor service
[Service]
ExecStartPre=/usr/bin/bash -c 'mkdir -p /run/uwsgi; chown liujia:nginx /run/uwsgi'
ExecStart=/usr/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
17.安装 nginx:
sudo yum install nginx
18.编辑 nginx 的配置文件,添加 server 内容:
sudo nano /etc/nginx/nginx.conf
增加的内容如下:
server {
listen 80;
server_name portal.anqun.org;
location = favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/liujia/firstsite;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/firstsite.sock;
}
}
19.保存后测试配置文件是否有错误:
sudo nginx -t
20.将普通用户 nginx 加到 liujia 组中:
sudo usermod -a -G liujia nginx
chmod 710 /home/liujia
21.启动 uWSGI 和 nginx 服务:
sudo systemctl start nginx
sudo systemctl start uwsgi
22.如果启动成功,将俩服务设置为自动启动:
sudo systemctl enable nginx
sudo systemctl enable uwsgi
源文:How To Serve Django Applications with uWSGI and Nginx on CentOS 7, https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-centos-7