環境:html
CentOS X64 6.4python
nginx 1.5.6mysql
Python 2.7.5nginx
正文:web
安裝必要的開發包sql
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:django
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的環境。瀏覽器
easy_install包 https://pypi.python.org/pypi/distribute服務器
方便安裝Python的開發包app
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: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」輸出,若沒有輸出,請檢查你的安裝過程。
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是否運行正常。
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
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文件,內容以下:
#! /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
而後在終端執行:
-- 添加服務 chkconfig --add uwsgi9090 -- 設置開機啓動 chkconfig uwsgi9090 on
找到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文件,內容以下:
#!/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|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
yum -y install mysql-devel easy_install-2.7 MySQL-python
注意紅色部分,easy_install-2.7,不然它將默認安裝到Python2.6環境內。
------------------------------------------------------------------------------------------------------------------
2014年12月02日添加:
CentOS 7中默認使用Firewalld作防火牆,因此修改iptables後,在重啓系統後,根本無論用。
Firewalld中添加端口方法以下:
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
做者:Xiongpq
出處:http://xiongpq.cnblogs.com/ 本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。