1.通常centos 都自帶有python ,centos5默認(rèn)的版本是2.43 建議安裝2.7 版本,安裝 ngixn(略)
2.安裝Python工具
- yum install python-devel libxml2 libxml2-devel python-setuptools zlib-devel wget openssl-devel pcre pcre-devel sudo gcc make autoconf automake
3.安裝pip
- wget http:
- tar xvfz pip-1.0.2.tar.gz
- cd pip-1.0.2
- python setup.py install
之所以安裝 pip ,我們后面可以用pip 命令直接安裝 uwsgi 和 django
4.安裝uwsgi
pip install uwsgi
或者你可以去官網(wǎng)下載自己解壓編譯安裝
5.安裝virtualenv (主要用于多站點(diǎn)創(chuàng)建虛擬環(huán)境)
6.設(shè)置python 虛擬環(huán)境變量
- mkdir /srv/vpython
- cd /srv/vpython
- virtualenv --no-site-packages mywebsite
- cd /srv/vpython/mywebsite
- source bin/activate
會(huì)看到提示符前面多了一個(gè)(mywebsite)?,F(xiàn)在運(yùn)行python,可以看到sys.path已經(jīng)更改為/srv/vpython/mywebsite目錄下的各個(gè)包。
這個(gè)目錄下的是供該站點(diǎn)使用的虛擬包,換句話說我們再增加新站點(diǎn)的時(shí)候也要?jiǎng)?chuàng)建屬于它的虛擬包 不能共用一份哦
7.部署django
- cd /srv/www/
- django-admin.py startproject mywebsite
執(zhí)行完畢后會(huì)在/srv/www/下面生成website目錄 我們會(huì)發(fā)現(xiàn)在website 目錄下面還有一個(gè)website目錄。我們無需理會(huì)
在/srv/www/website/下創(chuàng)建 uwsgi.py 代碼如下:
- import sys
- import os
-
- sys.path.append(os.path.abspath(os.path.dirname(__file__)))
- sys.path.append('/srv/www')
- os.environ['DJANGO_SETTINGS_MODULE'] = 'mywebsite.settings'
-
- import django.core.handlers.wsgi
-
- application = django.core.handlers.wsgi.WSGIHandler()
8.修改默認(rèn)配置或者在conf.d目錄下添加mywebsite.conf
- vim /etc/nginx/conf.d/mywebsite.conf
添加代碼如下:
- server {
-
- listen 80 ;
-
- server_name 127.0.0.1;
-
-
- root /srv/www/mywebsite;
-
- index index.html index.htm;
- access_log /var/log/nginx/access.log ;
-
- error_log /var/log/nginx/error.log ;
-
- location / {
-
- uwsgi_pass 127.0.0.1:3031;
-
- include uwsgi_params;
- uwsgi_param UWSGI_SCRIPT uwsgi;
-
- uwsgi_param UWSGI_PYHOME $document_root;
-
- uwsgi_param UWSGI_CHDIR $document_root;
-
- }
- }
注意上面標(biāo)紅的地方是我們在在/srv/www/website/下創(chuàng)建 uwsgi.py 的文件名 不用帶.py
9.創(chuàng)建 uwsgi 的配置文件 可支持xml yaml ini等格式 我習(xí)慣用ini,將其放在etc目錄下,當(dāng)然你也可以放到其他地方
vim /etc/uwsgi.ini
添加代碼如下:
- [uwsgi]
- socket=127.0.0.1:3031
- master=true
- vhost=true
- gid=nginx
- uid=nginx
- workers=10
- max-requests=1000
- limit-as=512
- pidfile=/var/run/uwsgi.pid
- daemonize=/var/log/uwsgi.log
注意我使用的端口是3031 因此我們可以看nginx 配置文件里的端口也是3031,還有注意有防火墻的話放行該端口,
裝完需要的包后,用deactivate命令退出虛擬環(huán)境。
10.創(chuàng)建 uwsgi的服務(wù)腳本,在lnmp nginx腳本上直接改的,支持stop start reload
vim /etc/ini.d/uwsgi
- #! /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:
-
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- DESC="uwsgi daemon"
- NAME=uwsgi
- <span style="color:#ff0000">DAEMON=/usr/bin/uwsgi</span>
- 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í)行 whereis uwsgi 就能知道 uwsgi 的bin 路徑
添加執(zhí)行權(quán)限
chmod +x uwsgi
11 安裝 mysql-server mysql-devel 同時(shí)還要安裝
mysql-python
- yum install mysql-server
- yum install mysql-devel
- pip install mysql-python
12 .啟動(dòng) service uwsgi start service nginx start service mysqld start
13 .完畢
注意:如果要添加一個(gè)站點(diǎn) 我們要重復(fù) 創(chuàng)建python 虛擬環(huán)境 然后再安裝相應(yīng)的web框架 如 web.py 等
添加nginx 配置文件 特別注意 uwsgi_param UWSGI_PYHOME 要填寫正確哦。