Centos6.5 + Django + mysql + nginx + uwsgi, djangong.pdf
1. Install nginx. Here the nginx-1.6.0 is used, a shell script is created and then executed.
#!/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
Here is the iptables settings
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. Install mysql. Here mysql is 5.6.14. Create a new shell script and paste the above text
#!/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. Start to install django. Here the latest stable version of django is 1.6.5.
Install necessary software packages
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
Install pip Software
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 check whether pip is installed
Install uwsgi
pip install uwsgiuwsgi --version
Create a web. py file with the following content:
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return "Hello World"
Then run on the terminal:
uwsgi --http :8001 --wsgi-file test.py
Enter http: // 127.0.0.1: 8001 in the browser to check whether "Hello World" output exists.
Install django
pip install django
django-admin.py startproject demositecd demosite
python manage.py runserver 0.0.0.0:8002
Enter http: // 127.0.0.1: 8002 in the browser to check if django is running normally.
Configure uwsgi
Create uwsgi9090.ini in the/ect/directory and add the following Configuration:
[Uwsgi] socket = 127.0.0.1: 9090 master = true // master process vhost = true // multi-site mode workers = 2 // number of sub-processes reload-mercy = 10 vacuum = true // clear files when exiting or restarting max-requests = 1000 limit-as = 512buffer-sizi= 30000 pidfile =/var/run/uwsgi9090.pid // pid file, use the following script to start and stop the process daemonize =/website/uwsgi9090.log
Set uwsgi to start up, and create the uwsgi9090 file in the/ect/init. d/directory. The content is as follows:
#! /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
Set uwsgi startup
chkconfig --add uwsgi9090 chkconfig uwsgi9090 on
Modify the nginx conf file
Server {listen 80; server_name localhost; location/{include uwsgi_params; uwsgi_pass 127.0.0.1: 9090; // The setting must be consistent with that in uwsgi. uwsgi_param UWSGI_SCRIPT demosite. wsgi; // The entry file, that is, wsgi. the position of py relative to the project root directory, ". "is equivalent to a level of directory uwsgi_param UWSGI_CHDIR/demosite; // The project root directory index index.html index.htm; client_max_body_size 35 m ;}}
service uwsgi9090 start
Enter http: // 127.0.0.1 in the browser. Congratulations! You can see django's "It work ~
Install mysql-python
pip install mysql-python
Test the connectivity between django and mysql
Run python manage. py shell in the project and
from django.db import connectioncursor=connection.cursor()
Django nginx uwsgi 502 Bad Gateway
Blog.csdn.net/watsy/article/details/9247967
You can take a look at what I wrote.
After uwsgi is added to/etc/rclocal, python's Django does not exist.
The difference between running in shell and automatic running mainly lies in the environment variables such as PATH. Do you want to check?