Tutorial on configuring Python + Django + Nginx + uWSGI + MySQL in Debian

Source: Internet
Author: User
This article mainly introduces how to configure Python + Django + Nginx + uWSGI + MySQL in Debian. Debian and Nginx servers are both high-performance options, if you need a new Debian system, you can refer to the latest attempt to migrate the project to the Python environment, I found some environment solutions for running Python Web on the Internet, and finally finalized the Nginx + uWSGI combination. Nginx is used more and more skillful. uWSGI is said to have good performance. I 'd like to try it out.

Most of the tutorials on the Internet require you to download the source code package from the uWSGI official website and then install it by compiling. for example, for a new Debian system, you can run the following command to install it:

apt-get updateapt-get upgradeapt-get install build-essential psmiscapt-get install python-dev libxml2 libxml2-devapt-get install python-setuptoolscd /opt/wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gztar -zxvf uwsgi-latest.tar.gzmv uwsgi-latest/ uwsgi/cd uwsgi/python setup.py install

I will not introduce some other configurations. you can refer to Deploy Python Code with WSGI using uWSGI and nginx on Debian 6 (Squeeze) in Linode Library.

Today, we will introduce how to directly install and configure the environment we need using APT source apt-get install. first, install MySQL and Nginx as usual:

Then, on the Debian Packages website, find the required uWSGI in sid. the content in sid indicates that it is still under development and testing, not the final stable version, however, this is enough for our tests. to use the software package in sid, we also need to modify our/etc/apt/sources. list source, add the deb http://ftp.tw.debian.org/debian sid main non-free contrib line, or directly enter the command:

echo "deb http://ftp.tw.debian.org/debian sid main non-free contrib" >> /etc/apt/sources.list

After configuring the source list, we can install it using the following command:

apt-get updateapt-get install uwsgi uwsgi-core uwsgi-plugin-python

After the installation is complete, the configuration file is put in the/etc/uwsgi by convention, service control can be achieved through the invoke-rc.d uwsgi followed by command parameters, but here there is an episode I have to mention, the following error may be reported during installation:

The following packages have unmet dependencies: libc6-dev : Breaks: gcc-4.4 (< 4.4.6-4) but 4.4.5-8 is to be installed. E: Broken packages

It seems that the gcc version of the dependent package does not match. I solved it in the following way:

Apt-get updateapt-get upgrade # first try to upgrade the system package apt-get install libc6-dev

Next, install MySQL support for django and Python and continue the APT command:

apt-get install python-django python-mysqldb

Through the above steps, the Python environment should be installed in your system, switch to/usr/lib:

cd /usr/libls -l | grep python

You may find that several versions of Python are installed by default in the system. for example, three versions are installed here:

/usr/lib/python2.6/usr/lib/python2.7/usr/lib/python3

How to determine the current default Python version? It is easy to directly use the following command:

python --version

As you know, django is installed under site-packages in the python directory, but none of these python directories have the site-packages folder, in fact, we can use the following command to locate the problem:

python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"

The above command prints the Python package path on the console. for example, here we may obtain the dist-packages directory. switch to this directory and you will find django there. Add permissions to the django-admin.py, and then make a symbolic connection so that we can operate later (I am here Django under Python2.7 ):

chmod 777 /usr/lib/python2.7/dist-packages/django/bin/django-admin.pyln -s /usr/lib/python2.7/dist-packages/django/bin/django-admin.py /usr/local/bin

Write down the Python version of Django, and then use the following command to switch the default Python version of uwsgi (generally, you do not need to switch ):

update-alternatives --config uwsgi-plugin-python

Now, the installation section is basically complete. the configuration section is explained below. First, create a project folder, for example, put it under/home/user/www, run the following command to create a Django project named mysite:

cd /home/user/wwwdjango-admin.py startproject mysite

Now, the/home/user/www directory should have a sub-folder named mysite. we need to drop a wsgi configuration file to/home/user/www/mysite, the configuration file is named wsgi. py, the content is as follows:

Next, modify the uwsgi configuration file. run the following command to create a configuration file according to the preceding settings:

Cat>/etc/uwsgi/apps-enabled/www. ini <EOF [uwsgi] socket =/var/run/uwsgi/app/www/socketchmod-socket = 666limit-as = 256 processes = 6max-request = 2000memory-report = trueenable-threads = truepythonpath =/ home/user/wwwchdir =/home/user/www/mysitewsgi-file =/home/user/www/mysite/wsgi. pyEOF # restart uwsgiinvoke-rc.d uwsgi restart

Pay attention to the socket configuration above. This path is used on our machine. you can find it in cd/var/run and configure it correctly. Next, create the Nginx configuration file:

cat > /etc/nginx/sites-enabled/www << EOFserver {    listen  80;    server_name localhost;    location / {        include uwsgi_params;        uwsgi_pass unix:///var/run/uwsgi/app/www/socket;    }}EOF

Here, the uwsgi_pass is still the socket we found above. pay attention to server_name, which is the domain name we bound and reload nginx through nginx-s reload.

Okay. now you can access it to see if it is successful? If the 502 Bad Gateway appears, check whether uwsgi is started properly. check the process list through ps aux | grep uwsgi, and check whether the socket path is correct.

If the uWSGI Error: Python application not found problem occurs, it is mainly about path configuration. you can check the log files under/var/log/uwsgi. for example, I have encountered ImportError: no module named django. core. handlers. the wsgi problem is actually wsgi. the path configuration in py is incorrect.

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.