Nginx+django+uwsgi+mysql Multi-Site Virtual environment setup under Linux

Source: Internet
Author: User
Tags md5 install django virtual environment virtualenv


Environment description

Linux:rhel 5.3
nginx:nginx-1.6.2
python:2.7.9
django:1.6.7
uwsgi:2.0.10

Note that the necessary development kits and Libxml2,python-devel,pcre-devel,mysql-devel for compiling the environment must be installed beforehand

2. Software Download Address

Nginx http://nginx.org/download/nginx-1.6.2.tar.gz
Python https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
Virtualenv https://pypi.python.org/packages/source/v/virtualenv/virtualenv-12.0.7.tar.gz#md5= e08796f79d112f3bfa6653cc10840114
Django Https://www.djangoproject.com/download/1.6.7/tarball
Uwsgi http://projects.unbit.it/downloads/uwsgi-2.0.10.tar.gz
Mysql-python https://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zip#md5= 654f75b302db6ed8dc5a898c625e030c

3. Software Installation

Install Nginx


# yum Install pcre-devel gcc openssl-devel–y
# cd/usr/local/src/django-env
# TAR-ZXVF Nginx-1.6.2.tar.gz
# CD nginx-1.6.2
# sed-i-E ' s/1.6.2//g ' e ' s/nginx\//ws/g '-e ' s/' nginx '/' WS '/g ' src/core/nginx.h
# Useradd WebAdmin
#./configure--user=webadmin--group=webadmin--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ Ssl_module
# Make && make install

Install Python


# cd/usr/local/src/django-env
# TAR-ZXVF Python-2.7.9.tgz
# CD Python-2.7.9
#./configure--prefix=/usr/local/python2.7
# Make && make install

Install Virtualenv

# cd/usr/local/src/django-env
# TAR–ZXVF Virtualenv-12.0.7.tar.gz
# CD virtualenv-12.0.7
#/usr/local/python2.7/bin/python setup.py Install

Building a virtual environment


# Cd/usr/local/python2.7/bin
#./virtualenv/home/webadmin/virtualenv/tools
New Python executable In/home/webadmin/tools/bin/python
Installing Setuptools, Pip...done.

Enter and view the virtual environment

Access to virtual environments in virtual environments (where long prefixes represent operations in a virtual environment), tools such as Easy_install are installed by default.


# source/home/webadmin/virtualenv/tools/bin/activate (Exit execute command Deactivate)
(Tools) [Root@tools bin]#
(Tools) [Root@tools bin]# Python-v
Python 2.7.9

Install Django-1.5.4


(Tools) [Root@tools ~]# cd/usr/local/src/django-env/
(Tools) [Root@tools django-env]# TAR-ZXVF django-1.5.4.tar.gz
(Tools) [Root@tools django-env]# CD Django-1.6.7
(Tools) [Root@tools django-1.6.7]# python setup.py install

Install Uwsgi

(Tools) [Root@tools ~]# cd/usr/local/src/django-env/
(Tools) [Root@tools django-env]# TAR-ZXVF uwsgi-2.0.10.tar.gz
(Tools) [Root@tools django-env]# CD uwsgi-2.0.10
(Tools) [Root@tools uwsgi-2.0.10]# python uwsgiconfig.py--build
(Tools) [Root@tools uwsgi-2.0.10]# python setup.py install

Install Mysql-python

(Tools) [Root@tools ~]# cd/usr/local/src/django-env/
(Tools) [Root@tools django-env]# Unzip Mysql-python-1.2.5.zip
(Tools) [Root@tools django-env]# CD mysql-python-1.2.5
(Tools) [Root@tools mysql-python-1.2.5]# python setup.py install

Note:

If the system does not have the Mysql-devel package installed, the following error will be reported when installing Mysql-python:

Traceback (most recent call last):
File "setup.py", line, in <module>
metadata, Options = Get_config ()
File "/root/lzh/mysql-python-1.2.2/setup_posix.py", line, in Get_config
Libs = Mysql_config ("Libs_r")
File "/root/lzh/mysql-python-1.2.2/setup_posix.py", line, in Mysql_config
Raise EnvironmentError, "%s not found"% Mysql_config.path
Environmenterror:mysql_config not found

4. Software configuration

Configuring Django Projects

Deployment Code


(Tools) [Root@tools ~]# mkdir/home/webadmin/djangoproj/tools/-P
(Tools) [Root@tools tools]# Unzip Tools.zip
(Tools) [Root@tools tools]# ls
DMC logs manage.py plugins src static templates tools Tools.zip

In the project configuration file directory of tools (that is, setting.py directory), check the file wsgi.py

(Tools) [Root@tools tools]# CD Tools
(Tools) [Root@tools tools]# Cat wsgi.py
Import OS
Os.environ.setdefault ("Django_settings_module", "tools.settings")
From Django.core.wsgi import get_wsgi_application
application = Get_wsgi_application ()

XML file (Wsgi.xml) that creates the Uwsgi startup parameters under the sibling directory

<uwsgi>
<socket>127.0.0.1:9090</socket>
<listen>20</listen>
<master>true</master>
<pidfile>/home/webadmin/logs/tools/uwsgi/uwsgi.pid</pidfile>
<processes>4</processes>
<module>wsgi</module> #就是上面那个wsgi. py file
<pythonpath>/home/webadmin/djangoproj/tools/tools</pythonpath>

#建立项目的路径 (this item needs to be noted, in a virtual environment, you need to configure to the directory where WSGI resides; in a non-virtual environment, you only need to configure to the project directory)

<profiler>true</profiler>
<memory-report>true</memory-report>
<enable-threads>true</enable-threads>
<logdate>true</logdate>
<limit-as>6048</limit-as>
<daemonize>/home/webadmin/logs/tools/uwsgi/tools.log</daemonize>
</uwsgi>

Configure Nginx


Location/{
Include Uwsgi_params;
Uwsgi_pass 127.0.0.1:9090;
}
#静态文件的处理 (if you have a static file in Django, you don't want to do this; it is generally recommended that static files be handed over to Nginx for better handling)
location/static/{
alias/home/webadmin/djangoproj/tools/static/;
Expires 30d;
Break
}
#配置django admin needs files
Location ~* ^/media/(CSS|IMG|JS)/.*$ {
Root/usr/local/lib/python2.6/site-packages/django/contrib/admin;
Expires 30d;
Break
}
Location ~* ^.+\. (Mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg) $ {
Access_log off;
Break
}
Location ~* ^.+\. (JPG|JPEG|GIF|PNG|ICO|CSS|BMP|JS) $ {
Expires 30d;
Break
}

Check and modify directory permissions

(Tools) [Root@tools webadmin]# chown webadmin:webadmin/home/webadmin/-R

5. Start Service


(Tools) [Webadmin@tools ~]$ uwsgi-x/home/webadmin/djangoproj/tools/tools/wsgi.xml
#/usr/local/nginx/sbin/ngin

6. Multi-Site Deployment

Repeat the steps from building the virtual environment and then install the actual environment to run in the virtual environment and start the service
Configure the Nginx virtual host
7. Trouble checking Thinking
The logs that are viewed include (nginx log [defined in the Nginx profile],UWSGI log [definition in XML file])
The main reason to view the Uwsgi log is to see if Uwsgi is properly taking over the request task of Nginx issued
Uwsgi logs can view Django execution error messages, or print debugging in development
Nginx error Log/access log view
The difference between the two logs is that the former is for code-level errors, and the latter for network access-level errors
8. A number of recommendations

Sometimes you can't kill Uwsgi when you use Killall Uwsgi, you can uwsgi-s reload to restart Uwsgi or Kill-hup cat/pathto/uwsgi.pid
UWSG the number of processes in the configuration file, which can be assigned according to the actual situation. Do not drive too much, or the machine may consume too much memory. Generally speaking, 4 processes are sufficient for a small community.
In general, you can write a stop.sh script to close the Uwsgi.
Why do you need nginx if you have a Uwsgi? Nginx has excellent static content processing capabilities and then transfers dynamic content to UWSGI servers, which can achieve good client response.
9. Matters needing attention
The log reports similar to the "Mon 23 10:26:49 2015–-no python application found, check your startup logs for errors-" error, you need to check the XML Thing
This file can not have a space in the end of the line, the parameters can not be around the space, this problem pits me one night!!!
The log reports similar to the "Importerror:no module named WSGI" Error, you need to check your XML file Pythonpath options
In a virtual environment, you need to configure to the directory where the UWSGI resides (not in a virtual environment, just the path to the project)
10. Notes

Normal log at startup:
Mon Mar 23 10:59:20 2015–*** starting Uwsgi 2.0.10 (64bit) on [Mon Mar 23 10:59:20 2015] * * *
Mon Mar 23 10:59:20 2015–compiled with version:4.1.2 20080704 (Red Hat 4.1.2-44) on March 2015 22:08:54
Mon Mar 23 10:59:20 2015–os:linux-2.6.18-194.el5 #1 SMP Tue 21:52:39 EDT 2010
Mon Mar 23 10:59:20 2015–nodename:tools.cyou.com
Mon Mar 23 10:59:20 2015–machine:x86_64
Mon Mar 23 10:59:20 2015–clock Source:unix
Mon Mar 23 10:59:20 2015–pcre JIT disabled
Mon Mar 23 10:59:20 2015–detected number of CPU cores:16
Mon Mar 23 10:59:20 2015–current working directory:/home/webadmin/djangoproj/tools
Mon Mar 23 10:59:20 2015–writing Pidfile to/home/webadmin/logs/tools/uwsgi/uwsgi.pid
Mon Mar 23 10:59:20 2015–detected binary path:/home/webadmin/virtualenv/tools/bin/uwsgi
Mon Mar 23 10:59:20 2015–your Processes number limit is 268288
Mon Mar 23 10:59:20 2015–limiting address space of processes ...
Mon Mar 23 10:59:20 2015–your process Address spaces limit is 6341787648 bytes (6048 MB)
Mon Mar 23 10:59:20 2015–your memory page size is 4096 bytes
Mon Mar 23 10:59:20 2015–detected max file descriptor number:1024
Mon Mar 23 10:59:20 2015–lock Engine:pthread Robust mutexes
Mon Mar 23 10:59:20 2015–thunder lock:disabled (You can enable it With–thunder-lock)
Mon Mar 23 10:59:20 2015–uwsgi socket 0 bound to TCP address 127.0.0.1:9090 FD 3
Mon Mar 23 10:59:20 2015–python version:2.7.9 (default, Mar 2015, 10:40:10) [GCC 4.1.2 20080704 (Red Hat 4.1.2-44)]
Mon Mar 23 10:59:20 2015–python main interpreter initialized at 0x161c5580
Mon Mar 23 10:59:20 2015–python threads Support Enabled
Mon Mar 23 10:59:20 2015–your Server Socket Listen backlog is limited to connections
Mon Mar 23 10:59:20 2015–your Mercy for graceful operations on workers is-seconds
Mon Mar 23 10:59:20 2015–mapped 363840 bytes (355 KB) for 4 cores
Mon Mar 23 10:59:20 2015–*** Operational mode:preforking * * *
Mon Mar 23 10:59:20 2015–added/home/webadmin/djangoproj/tools/tools/to Pythonpath.
Mon Mar 23 10:59:20 2015–wsgi app 0 (mountpoint= ") ready in 0 seconds on interpreter 0x161c5580 pid:24588 (default app)
Mon Mar 23 10:59:20 2015–*** Uwsgi is running in multiple interpreter mode * * * * *
Mon Mar 23 10:59:20 2015–spawned UWSGI Master process (pid:24588)
Mon Mar 23 10:59:20 2015–spawned UWSGI worker 1 (pid:24599, cores:1)
Mon Mar 23 10:59:20 2015–spawned UWSGI worker 2 (pid:24600, cores:1)
Mon Mar 23 10:59:20 2015–spawned UWSGI worker 3 (pid:24601, cores:1)
Mon Mar 23 10:59:20 2015–spawned UWSGI worker 4 (pid:24602, cores:1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.