Centos + nginx + uwsgi + virtualenv + flask multi-site environment setup, centosng.pdf
Environment:
Centos x64 6.6
Nginx 1.6.2
Python 2.7.9
Uwsgi 2.0.9
Virtualenv 12.0.5
Flask 0.10.1
Body:
1. Install nginx
Related website http://nginx.com
1.1 configure the yum third-party source
The nginx installation package is not available in the default centos source, so let's modify the third-party source.
#cd ~#wget http://www.atomicorp.com/installers/atomic#sh ./atomic#yum check-update
1.2 install nginx
#yum install nginx#service nginx start
#chkconfig nginx on
2. Install the class library
#cd ~
#yum groupinstall "Development tools"#yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
3. Install Python
Related Websites:
Python http://www.python.org
3.1 install the latest version
Centos 6.6 is installed with python2.6.6 by default. We have installed the latest version of python2.7.9.
#cd ~#wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz#tar xvf Python-2.7.9.tgz#cd Python-2.7.9.tgz#./configure --prefix=/usr/local#make && make altinstall
3.2 back up the old version and link to the new version
Because the yum program of centos uses the python2.6.6 version that comes with the previous system, to solve the version conflict problem, we need to distinguish and relink the python of the new and old versions first.
#mv /usr/bin/python /usr/bin/python2.6.6#ln -s /usr/local/bin/python2.7 /usr/bin/python
3.3 modify the yum configuration file
Re-specify the path of the python program in use:
#vi /usr/bin/yum
Found:
#!/usr/bin/python
To:
#!/usr/bin/python2.6.6
After modification, you can use the "python" command to enter the python2.7.9 environment.
Run the "exit ()" command to exit the environment.
4. Install the Python package management tool
Related Websites:
Distribute https://pypi.python.org/pypi/distribute
#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#python setup.py install
#distribute --version
5. Install pip package management tools
Related Websites:
Pip https://pypi.python.org/pypi/pip
#easy_install pip
#pip --version
6. Install uWSGI
Related Websites:
Uwsgi https://pypi.python.org/pypi/uWSGI
Detailed description of uwsgi parameters http://uwsgi-docs.readthedocs.org/en/latest/Options.html
#pip install uwsgi#uwsgi --version
7. Install virtualenv
Related Websites:
Virtualenv https://pypi.python.org/pypi/virtualenv
#pip install virtualenv#virtualenv --version
8. Install flask
Related Websites:
Flask http://flask.pocoo.org/
# Mkdir/usr/share/nginx/www // create a project library folder
# Mkdir/usr/share/nginx/www.a.com // create a project root directory folder
# Cd/usr/share/nginx/www/www.a.com
# Vietualenv env // create a virtual environment
#. Env/bin/activate // enter the Virtual Environment
# Pip install flask // install flask
# Deactivate // exit the Virtual Environment
9. Environment Configuration
9.1 configuration Declaration
First, I want to declare the absolute location of several fixed directories. (This section strictly follows the steps below for configuration & if you need to modify the path, Please modify the relevant configuration file at the same time)
Nginx configuration file:/etc/nginx/vhosts/www.a.com. conf
# Mkdir/etc/nginx/vhosts // create an nginx configuration folder
Project root directory:/usr/share/nginx/www/www.a.com
Log File directory:/usr/share/nginx/log
# Mkdir/usr/share/nginx/log // create a log folder
UWSGI configuration file:/etc/uwsgi/www.a.com. ini
# Mkdir/etc/uwsgi // create a uwsgi configuration folder
9.2 configure uWSGI
9.2.1 create a uWSGI configuration file
# Vi/etc/uwsgi/uwsgi_www.a.com.ini // create a uwsgi configuration file
// Enter the following blue content into the wusgi_www.a.com.ini File
[Uwsgi]
Master = true
Vhost = true
Workers = 2
Reload-mercy = 10
Vacuum = true
Max-requests = 1000
Limit-as = 256
Chmod-socket = 666
Socket =/tmp/uwsgi_www.a.com.sock
Venv =/usr/share/nginx/www/www.a.com/env
Chdir =/usr/share/nginx/www/www.a.com
Module = myapp
Callable = app
Touch-reload =/usr/share/nginx/www/www.a.com
Pidfile =/var/run/uwsgi_www.a.com.pid
Daemonize =/usr/share/nginx/log/uwsgi_www.a.com.log
9.2.2 Add a boot script for uwsgi
# Vi/etc/init. d/uwsgi_www.a.com
// Enter the following blue content into the uwsgi_www.a.com File
#! /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 path =/usr/local/sbin:/usr/local/bin:/sbin:/bin: /usr/sbin:/usr/binDESC = "uwsgi daemon" NAME = uwsgi_www.a.comDAEMON =/usr/local/bin/uwsgiCONFIGFILE =/etc/uwsgi/$ 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
# Chkconfig -- add uwsgi_www.a.com // add a service
# Chkconfig uwsgi_www.a.com on // set startup
9.2.3 modify the nginx configuration file
First open the nginx configuration file:
#vi /etc/nginx/nginx.conf
Found:
include /etc/nginx/conf.d/*.conf;
To:
# Include/etc/nginx/conf. d/*. conf; // Add # At the beginning of the line to comment out this sentence
Include/etc/nginx/vhosts/*. conf; // reference the application configuration file
9.2.4 create a project configuration file
# Vi/etc/nginx/vhosts/www.a.com. conf
// Enter the following blue content into the uwsgi_www.a.com File
Server {listen 80; server_name www.a.com;
Index index.htm index.html; location/{include uwsgi_params; uwsgi_pass unix: // tmp/uwsgi_www.a.com.sock;
}}
10. Test
10.1 enable the Service
#service nginx start#service uwsgi_www.a.com start
10.2 create an entry component File
# Vi/usr/share/nginx/www/www.a.com/myapp.py//insert the content marked with blue into the uwsgi_www.a.comfile from flask import Flaskapp = Flask (_ name _) @ app. route ('/') def hello_world (): return 'Hello World! 'If _ name _ = '_ main _': app. run ()
"Hello World! "Indicates that the environment has been set up successfully.
PS:
1. Too many code words will inevitably cause errors. If you find any errors or have any questions, please leave a message and I will reply to you immediately.
2. You are welcome to forward this blog, but please keep the following information:
By: EchoYYUrl: http://www.cnblogs.com/echoyyDate: January 1, January 14, 2015