开发者社区> 问答> 正文

在CentOS 7系统装 uWSGI Nginx Django

应 云友 - “mango爱好 ” 在 帖子 要求,找在 CentOS 7 系统安装 nginx + uWSGI + Diango 的资料。

找到一篇英文的,粗略翻译及实践如下,此例中是假设将相应的文件放在 liujia 用户目录下(/home/liujia),以普通用户 liujia 用户登录到控制台:

1. 添加 EPEL 软件源:
sudo yum install epel-release
[attachment=84774]

2. 安装 pip 工具:
sudo yum install python-pip
3. 安装 python 虚拟环境工具:
sudo pip install virtualenv virtualenvwrapper
[attachment=84775]

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
[attachment=84776]

12.安装 python 开发包:
sudo yum install python-devel gcc
[attachment=84777]

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;
    }
}

[attachment=84778]

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

[attachment=84779]

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

展开
收起
dongshan8 2015-09-11 12:39:48 13273 0
3 条回答
写回答
取消 提交回答
  • Re在CentOS 7系统装 uWSGI Nginx Django
    请问这是什么情况

    connect() to unix:/run/uwsgi/mysite.sock failed (111: Connection refused) while connecting to upstream

    -------------------------

    回 楼主dongshan8的帖子
    connect() to unix:/run/uwsgi/mysite.sock failed (2: No such file or directory) while connecting to upstream
    2015-12-04 13:05:55
    赞同 展开评论 打赏
  • 旺旺:nectar2。
    回 1楼(ivmmff) 的帖子
    您好,

    没细心核对过。

    但从图1中的来看,好象需要安装epel。
    Package epel-release.noarch 0:7-5 will be installed



    -------------------------

    回 4楼(冰淤) 的帖子
    您好,

    欢迎来到阿里云论坛。

    Connection refused,这可能是权限的问题,请检查一下web用户权限是否有权限读取sock文件。

    No such file or directory,进程可能退出,没有创建sock文件了。

    如有疑问,欢迎跟帖交流喔。
    2015-09-11 13:25:13
    赞同 展开评论 打赏
  • 解决方案工程师,负责为企业规划上云迁移方案和云上架构设计,在网站建设开发和云计算领域有多年经验,专注于Linux平台的系统维护以及应用部署。致力于以场景化的方式让云计算,用更加通俗易懂的方式让更多人体验云计算,让云端的计算更质朴的落地。
    对了,你有没有研究过,
    貌似阿里云的Centos7 默认就安装了epel。。。

    -------------------------

    回 2楼(dongshan8) 的帖子
    奇怪了,我再去看看
    2015-09-11 12:45:42
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
《Nginx 代理系统常用手册》 立即下载
CentOS Nginx PHP JAVA 多语言镜像使用手 立即下载
CentOS Nginx PHP JAVA多语言镜像使用手册 立即下载