共计 4502 个字符,预计需要花费 12 分钟才能阅读完成。
文章目录 [隐藏]
众所周知,现在绝大部分动态网站都是用 PHP 玩出来的,前端用 html+div+css+js……balabala 还有一大堆前端框架,后端用 thinkphp,yii,symfony 等框架结合数据库操作展示给前端。你以为除了这些 code 应用就搞定网站了吗?NO! 对于开发人员来说,网站访问是一系列的服务器与用户电脑间的“请求 -> 响应 -> 请求”反复的过程。当前比较常见的 WEB 服务端架构是 LNMP(Linux+Nginx+Mysql+PHP) 或者 LAMP(Linux+Apache+Mysql+PHP)。本文将介绍的是如何在 CentOS 7 中极速搭建 python web 开发框架,即:Linux+Django+Ngnx+uwsgi。文章中所有操作均在 root 管理员身份下进行。
1、安装 EPEL 源
yum update -y # 非必要步骤
yum install epel-release -y
2、安装 nginx
安装比较简单粗暴
yum install nginx -y
2、安装 uwsgi
科普知识:什么是 uwsgi?
uWSGI 是一个 Web 服务器,它实现了 WSGI 协议、uwsgi、http 等协议。Nginx 中 HttpUwsgiModule 的作用是与 uWSGI 服务器进行交换。这时我们应该马上能想到 nginx 和 apache 都是 WEB 服务器,为什么我们安装了 nginx 还要安装 uwsgi 呢?
我们可以这样理解,在 LNMP 环境中,Nginx 作为前端反向代理服务器,它的作用就是响应用户静态资源的请求和转发动态请求到后端 fcgi 解析器,即:php-fpm。这里我们可以理解成使用 uwsgi 来作为 python 框架——Django 的解析器。此时我们先要安装一系列的开发环境,依次使用如下命令安装即可:
yum groupinstall "Development Tools" -y
yum install python-pip python-setuptools -y # 此步用来安装 pip 工具,方便后面安装 python 系列包
pip install uwsgi # 此步才真正安装 uwsgi
我们建一个 test.py 来测试一下,文件内容如下:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return "Hello World"
启动 uwsgi 服务:
uwsgi --http :8001 --wsgi-file test.py
喂!醒醒,可以访问了!
此时在浏览器中输入 http://yourip:8001 如果不出意外的话,应该会出现程序猿界经典的第一个世界问好了。
注意:yourip 表示你的机器的 IP 地址。
有的小伙伴表示经过上面操作后仍然不能出现“Hello World”,别急,此时你应该好好检查下你的 selinux 或者 firewalld 有没有放开 8001 端口,此问题不在本篇中解释了。
3、安装 Django
pip install django
测试一下 django 是否安装正常:
# 注意:此项目路径将在后面的 nginx 配置中使用
cd /home
django-admin.py startproject guoziproject
cd guoziproject
python manage.py runserver 0.0.0.0:8002
此时在浏览器中输入:http://yourip:8002
说明:Django 中本来就带了一个 WEB 服务器,不过一般很少人在生产环境中使用它,源于它的性能和稳定性都比较差,所以也就有了本文环境的搭建了。不出意外的话,你应该可以看到如下访问页面:
4、添加配置文件和启动脚本
接下来,我们来给 uwsgi 添加一个配置文件 /etc/uwsgi.ini,内容如下:
[uwsgi]
socket = 127.0.0.1:9090
#主进程
master = true
#多站模式
vhost = true
#多站模式时不设置入口模块和文件
no-site = true
#转到对应项目目录
chdir = /home/guoziproject
#uwsgi 文件
wsgi-file = /home/guoziproject/guoziproject/wsgi.py
#子进程数
workers = 2
reload-mercy = 10
#退出、重启时清理文件
vacuum = true
max-requests = 1000
limit-as = 512
buffer-size = 30000
#pid 文件,用于下面的脚本启动、停止该进程
pidfile = /var/run/uwsgi.pid
#日志文件存放路径
daemonize = /tmp/uwsgi.log
#使用 nginx 用户和组
uid = nginx
gid = nginx
接着,我们来给 uwsgi 写一个启动脚本 /etc/init.d/uwsgid(虽然 CentOS 7 中已经使用 systemctl 来管理启动进程了,但这种 init.d 启动脚本还是支持的):
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for uwsgi webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f uwsgi defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add uwsgi'
### BEGIN INIT INFO
# Provides: uwsgi
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the uwsgi web server
# Description: starts uwsgi using start-stop-daemon
### END INIT INFO
# Author: licess
# website: https://www.guoziweb.com
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="uwsgi daemon"
NAME=uwsgi
DAEMON=/usr/bin/uwsgi
CONFIGFILE=/etc/$NAME.ini
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() { $DAEMON $CONFIGFILE || echo -n "uwsgi already running" }
do_stop() {
$DAEMON --stop $PIDFILE || echo -n "uwsgi not running"
rm -f $PIDFILE
echo "$DAEMON STOPED."
}
do_reload() { $DAEMON --reload $PIDFILE || echo -n "uwsgi can't reload" }
do_status() { ps aux|grep $DAEMON}
case "$1" in
status)
echo -en "Status $NAME: \n"
do_status
;;
start)
echo -en "Starting $NAME: \n"
do_start
;;
stop)
echo -en "Stopping $NAME: \n"
do_stop
;;
restart)
echo -en "Restarting $NAME:\n"
do_stop
do_start
;;
reload|graceful)
echo -en "Reloading $NAME: \n"
do_reload
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2
exit 3
;;
esac
exit 0
赋予一下脚本可执行权限和开机启动:
chmod a+x /etc/init.d/uwsgid
chkconfig uwsgid on
修改 nginx 配置文件:/etc/nginx/conf/nginx.conf,将 server 段修改如下:
upstream django_ops{ server 127.0.0.1:9090;}
server {
listen 80;
server_name localhost;
location /media {
alias /home/oss/media;
}
location /static {
alias /home/oss/static;
}
# 以上为静态文件分离配置
# 非静态文件都转到 uwsgi
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
uwsgi_pass django_ops;
client_max_body_size 35m;
}
}
启动所有服务并加载开机启动项:
/etc/init.d/uwsgid start
systemctl start nginx.service
systemctl enable nginx.service
All Done! Enjoy your code!
PS: 后续有时间将编写一键安装配置脚本,敬请期待!

注意关闭selinux
临时关闭:setenfore 0
永久生效,修改配置文件:/etc/selinux/config
SELINUX=disabled