centos6.5+Django+mysql+nginx+uwsgi,djangonginx
1、nginx的安裝。這裡採用nginx-1.6.0, 建立一個shell指令碼然後執行。
#!/bin/bashnginx_version="nginx-1.6.0"yum -y install gcc gcc-c++ pcre pcre-devel openssl openssl-develcd softtar zxvf $nginx_version".tar.gz" cd $nginx_version./configure --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_stub_status_module --with-http_ssl_modulemakemake installcp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confecho "/usr/local/nginx/sbin/nginx" >>/etc/rc.localcd ..rm -rf $nginx_version
這裡是iptables的設定
iptables -Fiptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPTiptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPTiptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPTiptables -A INPUT -i lo -j ACCEPTiptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPTiptables -P INPUT DROPservice iptables save
2.mysql的安裝,在這裡mysql使用的是5.6.14,建立一個shell指令碼粘貼上下面的文字
#!/bin/bashmysql_version="mysql-5.6.14"yum -y install vim libevent* libtool* autoconf* libstd* ncurse* bison* openssl*cd softtar zxvf cmake-2.8.12.1.tar.gzcd cmake-2.8.12.1./configure && make && make installcd ..rm -rf cmake-2.8.12.1tar zxvf $mysql_version".tar.gz"cd $mysql_versioncmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_cimake && make installgroupadd mysqluseradd -g mysql mysqlchown -R mysql:mysql /usr/local/mysqlcd /usr/local/mysqlscripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql --ldata=/var/lib/mysqlcp support-files/mysql.server /etc/init.d/mysqlchkconfig mysql onchmod 755 /etc/init.d/mysqlservice mysql startecho "PATH=/usr/local/mysql/bin:$PATH" >> /etc/profileecho "export PATH" >> /etc/profilesource /etc/profilemysqladmin -u root password 123456cd -cd ..rm -rf $mysql_version
3.開始安裝django,這裡使用的是django最新穩定版1.6.5
安裝必要的軟體包
yum groupinstall "Development tools"yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel python-devel libxml2 libxml2-devel python-setuptools zlib-devel wget openssl-devel pcre pcre-devel sudo gcc make autoconf automake
安裝pip軟體
wget http://pypi.python.org/packages/source/p/pip/pip-1.0.2.tar.gz --no-check-certificate tar xvfz pip-1.0.2.tar.gz cd pip-1.0.2 python setup.py install
pip --version 查看pip是否安裝
安裝uwsgi
pip install uwsgiuwsgi --version
建立web.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-admin.py startproject demositecd demosite
python manage.py runserver 0.0.0.0:8002
在瀏覽器內輸入:http://127.0.0.1:8002,檢查django是否運行正常。
配置uwsgi
在/ect/目錄下建立uwsgi9090.ini,添加如下配置:
[uwsgi]socket = 127.0.0.1:9090master = true //主進程vhost = true //多站模式workers = 2 //子進程數reload-mercy = 10 vacuum = true //退出、重啟時清理檔案max-requests = 1000 limit-as = 512buffer-sizi = 30000pidfile = /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/binDESC="uwsgi daemon"NAME=uwsgi9090DAEMON=/usr/bin/uwsgiCONFIGFILE=/etc/$NAME.iniPIDFILE=/var/run/$NAME.pidSCRIPTNAME=/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
設定uwsgi開機啟動
chkconfig --add uwsgi9090 chkconfig uwsgi9090 on
修改nginx的conf檔案
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; } }
service uwsgi9090 start
在瀏覽器輸入:http://127.0.0.1,恭喜你可以看到django的“It work”了~
安裝mysql-python
pip install mysql-python
測試django與mysql的串連性
在項目中運行python manage.py shell然後
from django.db import connectioncursor=connection.cursor()
django nginx uwsgi 502 Bad Gateway
blog.csdn.net/watsy/article/details/9247967
你可以看一下我寫的。
uwsgi加入/etc/rclocal後,python的Django不存在了
在shell裡運行和自動啟動並執行差別主要在於環境變數比如PATH,你檢查一下?