众所周知,现在绝大部分动态网站都是用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: http://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:后续有时间将编写一键安装配置脚本,敬请期待!
- 微信扫码赞助
-
- 支付宝赞助
-
2016/10/26 21:17:52
注意关闭selinux
临时关闭:setenfore 0
永久生效,修改配置文件:/etc/selinux/config
SELINUX=disabled
2016/10/26 21:16:39
更新了/etc/uwsgi.ini中的指定用户权限运行
#使用nginx用户和组
uid = nginx
gid = nginx
否则可能出现如下错误:
2016/10/27 05:14:04 [crit] 3374#0: *19 connect() to 127.0.0.1:9090 failed (13: Permission denied) while connecting to upstream, client: 192.168.1.10, server: localhost, request: “GET / HTTP/1.1”, upstream: “uwsgi://127.0.0.1:9090”, host: “192.168.1.9”