CentOS+nginx+uwsgi+Python 多站点环境搭建
环境:
CentOS X64 6.4
nginx 1.5.6
Python 2.7.5
一【yī】:安装需要的类👯库及【kù jí】👴Python2.7.5
安装必要的开发包
yum groupinstall "Development tools" yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel read LINE -devel tk-devel
CentOS 自带【zì dài】🍉Python2.6.6,但我们【dàn wǒ men】可以再安装📵Python2.7.5:
cd ~ wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2 tar xvf Python-2.7.5.tar.bz2 cd Python-2.7.5 ./configure --prefix=/usr/local make && make altinstall
安装完【ān zhuāng wán】毕后【bì hòu】🥟,可是使用🐸”python2.7”命令进入👗python2.7的环境【de huán jìng】。
二:安装Python包管理
easy_install包【bāo】👘 https://pypi.python.org/pypi/distribute
方便安装Python的开发包
cd ~ wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.49.tar.gz tar xf distribute-0.6.49.tar.gz cd distribute-0.6.49 python2.7 setup.py install easy_install --version
红色部🏏分必须【fèn bì xū】是🏯“python2.7”,否则将安装到【ān zhuāng dào】默认的【mò rèn de】🕯2.6环境内。
pip包【bāo】👐 https://pypi.python.org/pypi/pip
安装pip的好处是可以😥pip list、pip uninstall 管理【guǎn lǐ】🚎Python包, easy_install没有这【méi yǒu zhè】个功能【gè gōng néng】😡,只有uninstall
easy_install pip pip --version
三:安装【ān zhuāng】🏳uwsgi
uwsgi:https://pypi.python.org/pypi/uWSGI
uwsgi参数详【cān shù xiáng】解🎵:http://uwsgi-docs.readthedocs.org/en/latest/Options.html
pip install uwsgi uwsgi --version
测试uwsgi是否正⬆常【cháng】:
新建test.py文件,内容如下:
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return "Hello World"
然后在【rán hòu zài】🚡终端运行:
uwsgi --http :8001 --wsgi-file test.py
在浏览器内输【qì nèi shū】入:http://127.0.0.1:8001,看是否【kàn shì fǒu】有🦇“Hello World”输出,若没有【ruò méi yǒu】🦇输出,请检查💕你的安🕝装过程【zhuāng guò chéng】🔝。
四:安装django
pip install django
测试【cè shì】📩django是否正🕌常,运行【yùn háng】:
django-admin.py startproject demosite cd demosite python2.7 manage.py runserver 0.0.0.0:8002
在浏览器内输入【rù】:http://127.0.0.1:8002,检查【jiǎn chá】🎹django是否运行正常💆。
五:安装nginx
cd ~ wget http://nginx.org/download/nginx-1.5.6.tar.gz tar xf nginx-1.5.6.tar.gz cd nginx-1.5.6 ./configure --prefix=/usr/local/nginx-1.5.6 \ --with-http_stub_status_module \ --with-http_gzip_static_module make && make install
六【liù】🙀:配置uwsgi
uwsgi支持🖍ini、xml等多种配置方【pèi zhì fāng】🔝式,但个人感觉【gǎn jiào】🎗ini更方便【gèng fāng biàn】:
在😓/ect/目录下新建📪uwsgi9090.ini,添加如【tiān jiā rú】下配置【xià pèi zhì】:
[uwsgi] socket = 127.0.0.1:9090 master = true //主进程【jìn chéng】🕑 vhost = true //多站模【duō zhàn mó】🌥式【shì】 no-stie = true //多站模【duō zhàn mó】🌥式【shì】时不设置入口模块【kǒu mó kuài】和文件【wén jiàn】🥇 workers = 2 //子进程【jìn chéng】👭数【shù】⛎ reload-mercy = 10 vacuum = true //退出【tuì chū】、重启时清理文💾件 max-requests = 1000 limit-as = 512 buffer-sizi = 30000 pidfile = /var/run/uwsgi9090.pid //pid文件【wén jiàn】🥇,用于下面的脚本启动【dòng】🚓、停止该🎗进程【jìn chéng】👭 daemonize = /website/uwsgi9090.log
设置uwsgi开机启动【dòng】🚓,在🧟/ect/init.d/目录下新建uwsgi9090文件【wén jiàn】🥇,内容如下:
#! /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://lnmp.org PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DESC="uwsgi daemon" NAME=uwsgi9090 DAEMON=/usr/local/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 ;; reload|graceful) echo -en "Reloading $NAME: \n" do_reload ;; *) echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2 exit 3 ;; esac exit 0
然后在终端执【zhōng duān zhí】⏬行:
-- 添加服🕣务【wù】 chkconfig --add uwsgi9090 -- 设置【shè zhì】开📋机启动 chkconfig uwsgi9090 on
七:设置【shè zhì】nginx
找到【zhǎo dào】🛋nginx的安装目录,打开conf/nginx.conf文件🌠,修改【xiū gǎi】🕙server配置【pèi zhì】
server { listen 80; server_name localhost; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:9090; //必须和🔻uwsgi中的设置一致【zhì yī zhì】 uwsgi_param UWSGI_SCRIPT demosite.wsgi; //入口文件🆗,即【jí】wsgi.py相对于🍓项目根【xiàng mù gēn】🥎目录【mù lù】📻的位置【de wèi zhì】,“.”相当于一层目录【mù lù】📻 uwsgi_param UWSGI_CHDIR /demosite; //项目根【xiàng mù gēn】🥎目录【mù lù】📻 index index.html index.htm; client_max_body_size 35m; } }
设置nginx开机启⌚动🥄,在【zài】/ect/init.d/目录【mù lù】📻下新建nginx文件🆗,内容如下:
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /usr/local/nginx/conf/nginx.conf # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/opt/nginx-1.5.6/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/opt/nginx-1.5.6/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|f
关键词【guān jiàn cí】🤩:CentOS,nginx,uwsgi,Python
阅读本文后您有什么感想? 已有 人给出评价!
- 2
- 1
- 1
- 1
- 2
- 1