共计 8964 个字符,预计需要花费 23 分钟才能阅读完成。
环境:
CentOS X64 6.4
nginx 1.5.6
Python 2.7.5
正文:
一:安装需要的类库及 Python2.7.5
安装必要的开发包
yum groupinstall "Development tools" | |
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel |
CentOS 自带 Python2.6.6,但我们可以再安装 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 |
安装完毕后,可是使用”python2.7”命令进入 python2.7 的环境。
二:安装 Python 包管理
easy_install 包 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 |
红色部分必须是“python2.7”,否则将安装到默认的 2.6 环境内。
pip 包 https://pypi.python.org/pypi/pip
安装 pip 的好处是可以 pip list、pip uninstall 管理 Python 包,easy_install 没有这个功能,只有 uninstall
easy_install pip | |
pip --version |
三:安装 uwsgi
uwsgi:https://pypi.python.org/pypi/uWSGI
uwsgi 参数详解:http://uwsgi-docs.readthedocs.org/en/latest/Options.html
pip install uwsgi | |
uwsgi --version |
测试 uwsgi 是否正常:
新建 test.py 文件,内容如下:
def application(env, start_response): | |
start_response('200 OK', [('Content-Type','text/html')]) | |
return "Hello World" |
然后在终端运行:
uwsgi --http :8001 --wsgi-file test.py
在浏览器内输入:http://127.0.0.1:8001,看是否有“Hello World”输出,若没有输出,请检查你的安装过程。
四:安装 django
pip install django
测试 django 是否正常,运行:
django-admin.py startproject demosite | |
cd demosite | |
python2.7 manage.py runserver 0.0.0.0:8002 |
在浏览器内输入:http://127.0.0.1:8002,检查 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 |
推荐阅读:
你应该使用 Nginx + uWSGI http://www.linuxidc.com/Linux/2013-07/87286.htm
uWSGI + Nginx 部署 Flask Web 应用 http://www.linuxidc.com/Linux/2013-06/85828.htm
Django+Nginx+uWSGI 部署 http://www.linuxidc.com/Linux/2013-02/79862.htm
Linux 下 Nginx+uWSGI 部署 Python 应用 http://www.linuxidc.com/Linux/2012-10/72443.htm
Ubuntu Server 12.04 安装 Nginx+uWSGI+Django 环境 http://www.linuxidc.com/Linux/2012-05/60639.htm
CentOS 5.5 + Nginx 0.8.50 + uWSGI + Django 1.2.3 部署 Django 项目 http://www.linuxidc.com/Linux/2011-05/36399.htm
Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里
六:配置 uwsgi
uwsgi 支持 ini、xml 等多种配置方式,但个人感觉 ini 更方便:
在 /ect/ 目录下新建 uwsgi9090.ini,添加如下配置:
[uwsgi] | |
socket = 127.0.0.1:9090 | |
master = true // 主进程 | |
vhost = true // 多站模式 | |
no-stie = true // 多站模式时不设置入口模块和文件 | |
workers = 2 // 子进程数 | |
reload-mercy = 10 | |
vacuum = true // 退出、重启时清理文件 | |
max-requests = 1000 | |
limit-as = 512 | |
buffer-sizi = 30000 | |
pidfile = /var/run/uwsgi9090.pid //pid 文件,用于下面的脚本启动、停止该进程 | |
daemonize = /website/uwsgi9090.log |
设置 uwsgi 开机启动,在 /ect/init.d/ 目录下新建 uwsgi9090 文件,内容如下:
# 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 |
然后在终端执行:
-- 添加服务 | |
chkconfig --add uwsgi9090 | |
-- 设置开机启动 | |
chkconfig uwsgi9090 on |
七:设置 nginx
找到 nginx 的安装目录,打开 conf/nginx.conf 文件,修改 server 配置
server { | |
listen 80; | |
server_name localhost; | |
location / { | |
include uwsgi_params; | |
uwsgi_pass 127.0.0.1:9090; // 必须和 uwsgi 中的设置一致 | |
uwsgi_param UWSGI_SCRIPT demosite.wsgi; // 入口文件,即 wsgi.py 相对于项目根目录的位置,“.”相当于一层目录 | |
uwsgi_param UWSGI_CHDIR /demosite; // 项目根目录 | |
index index.html index.htm; | |
client_max_body_size 35m; | |
} | |
} |
设置 nginx 开机启动,在 /ect/init.d/ 目录下新建 nginx 文件,内容如下:
# | |
# 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|force-reload|configtest}" | |
exit 2 | |
esac |
然后在终端执行:
-- 添加服务 | |
chkconfig --add nginx | |
-- 设置开机启动 | |
chkconfig nginx on |
八:测试
OK,一切配置完毕,在终端运行
service uwsgi9090 start | |
service nginx start |
在浏览器输入:http://127.0.0.1,恭喜你可以看到 django 的“It work”了~
九:多站配置
我采用运行多个 uwsgi 服务的方法来实现多个站点。
重复第六步,创建 uwsgi9091.ini,并相应修改文件中的
socket = 127.0.0.1:9091 | |
pidfile = /var/run/uwsgi9091.pid | |
daemonize = /website/uwsgi9091.log |
并创建服务 uwsgi9091,设置开机启动。
然后修改 nginx 的配置文件为:
server {listen 80; | |
server_name localhost; | |
location / {include uwsgi_params; | |
uwsgi_pass 127.0.0.1:9090; | |
uwsgi_param UWSGI_SCRIPT demosite.wsgi; | |
uwsgi_param UWSGI_CHDIR /website/demosite; | |
index index.html index.htm; | |
client_max_body_size 35m; | |
} | |
} | |
server {listen 1300; | |
location / {include uwsgi_params; | |
uwsgi_pass 127.0.0.1:9091; | |
uwsgi_param UWSGI_SCRIPT DjangoStudy.wsgi; | |
uwsgi_param UWSGI_CHDIR /website/DjangoStudy; | |
index index.html index.htm; | |
} | |
} |
然后我们就可以通过 http://127.0.0.1:1300 来访问新的网站了。
十:其他配置
防火墙设置
CentOS 默认关闭外部对 80、3306 等端口的访问,所以要在其他计算机访问这台服务器,就必须修改防火墙配置,打开 /etc/sysconfig/iptables
在“-A INPUT –m state –state NEW –m tcp –p –dport 22 –j ACCEPT”,下添加:
-A INPUT m state --state NEW m tcp p dport 80 j ACCEPT | |
-A INPUT m state --state NEW m tcp p dport 3306 j ACCEPT |
然后保存,并关闭该文件,在终端内运行下面的命令,刷新防火墙配置:
service iptables restart
安装 Mysqldb
yum -y install mysql-devel | |
easy_install-2.7 MySQL-Python |
注意红色部分,easy_install-2.7, 否则它将默认安装到 Python2.6 环境内。
Python 的详细介绍:请点这里
Python 的下载地址:请点这里
更多 CentOS 相关信息见CentOS 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=14
环境:
CentOS X64 6.4
nginx 1.5.6
Python 2.7.5
正文:
一:安装需要的类库及 Python2.7.5
安装必要的开发包
yum groupinstall "Development tools" | |
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel |
CentOS 自带 Python2.6.6,但我们可以再安装 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 |
安装完毕后,可是使用”python2.7”命令进入 python2.7 的环境。
二:安装 Python 包管理
easy_install 包 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 |
红色部分必须是“python2.7”,否则将安装到默认的 2.6 环境内。
pip 包 https://pypi.python.org/pypi/pip
安装 pip 的好处是可以 pip list、pip uninstall 管理 Python 包,easy_install 没有这个功能,只有 uninstall
easy_install pip | |
pip --version |
三:安装 uwsgi
uwsgi:https://pypi.python.org/pypi/uWSGI
uwsgi 参数详解:http://uwsgi-docs.readthedocs.org/en/latest/Options.html
pip install uwsgi | |
uwsgi --version |
测试 uwsgi 是否正常:
新建 test.py 文件,内容如下:
def application(env, start_response): | |
start_response('200 OK', [('Content-Type','text/html')]) | |
return "Hello World" |
然后在终端运行:
uwsgi --http :8001 --wsgi-file test.py
在浏览器内输入:http://127.0.0.1:8001,看是否有“Hello World”输出,若没有输出,请检查你的安装过程。
四:安装 django
pip install django
测试 django 是否正常,运行:
django-admin.py startproject demosite | |
cd demosite | |
python2.7 manage.py runserver 0.0.0.0:8002 |
在浏览器内输入:http://127.0.0.1:8002,检查 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 |
推荐阅读:
你应该使用 Nginx + uWSGI http://www.linuxidc.com/Linux/2013-07/87286.htm
uWSGI + Nginx 部署 Flask Web 应用 http://www.linuxidc.com/Linux/2013-06/85828.htm
Django+Nginx+uWSGI 部署 http://www.linuxidc.com/Linux/2013-02/79862.htm
Linux 下 Nginx+uWSGI 部署 Python 应用 http://www.linuxidc.com/Linux/2012-10/72443.htm
Ubuntu Server 12.04 安装 Nginx+uWSGI+Django 环境 http://www.linuxidc.com/Linux/2012-05/60639.htm
CentOS 5.5 + Nginx 0.8.50 + uWSGI + Django 1.2.3 部署 Django 项目 http://www.linuxidc.com/Linux/2011-05/36399.htm
Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里
