tutorial on configuring Python+django+nginx+uwsgi+mysql under Debian

Source: Internet
Author: User
Recently tried to move the project into a Python environment, especially installed a clean Debian system, ready to reconfigure the environment, the Internet to find some of the environment to run the Python Web solution, finalize the NGINX+UWSGI combination, Nginx used more, skilled ; Uwsgi is said to be a good performance, want to try.

Most of the online tutorials are required to download the source package to the official website of Uwsgi, and then install it by compiling it, such as a new Debian system, which can be installed by the following command:

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

There are some configurations I will not introduce, you can refer to the Linode Library of the "Deploy Python Code with WSGI using Uwsgi and Nginx" on Debian 6 (Squeeze) "Introduction.

Today is to introduce the use of apt source directly apt-get install installation configuration We need the environment, first of all, according to the Convention first install MySQL and nginx:

Then through the Debian packages website to find what we need Uwsgi in SID, Sid version of the things that are still in the development test, not the final stable version, but for our test is enough, in order to use the SID inside the package, 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 this line, or enter the command directly:

echo "Deb Http://ftp.tw.debian.org/debian SID main Non-free contrib" >>/etc/apt/sources.list

Once we have configured the source list, we can install it by using the following command:

Apt-get updateapt-get Install Uwsgi uwsgi-core Uwsgi-plugin-python

After the installation is complete, the configuration file is used in/etc/uwsgi, the service control can be implemented by INVOKE-RC.D Uwsgi followed by command parameters, but here's an episode I have to mention that the installation may report such errors:

The following packages has unmet dependencies:libc6-dev:breaks:gcc-4.4 (< 4.4.6-4) but 4.4.5-8 are to be installed. E:broken Packages

It seems that the dependency package GCC version mismatch, I later resolved by the following way:

Apt-get updateapt-get Upgrade # First try upgrading the System Package Apt-get install Libc6-dev

The next step is to install the Django and Python support for MySQL and continue with the APT command:

Apt-get Install Python-django Python-mysqldb

With the above steps, the Python environment should be installed in your system and switched to/usr/lib:

Cd/usr/libls-l | grep python

You may find that several versions of Python are installed in the system by default, such as I have 3 versions installed here:

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

How do I determine the current Python default version? It's easy to go straight through the following command:

Python--version

You know that Django is installed under the Python directory under the site-packages, but these Python directories are not site-packages This folder, in fact, we can first navigate through the following command:

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, we might get the Dist-packages directory, switch to this directory and you'll find that Django is there. Add permissions to django-admin.py, and then make a symbolic connection so that we can operate later (I'm Django here 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

Make a note of the Python version of Django, and then switch to the default Python version of Uwsgi with the following command (typically, you do not need to switch):

Update-alternatives--config Uwsgi-plugin-python

OK, here is basically the installation part of the introduction, the following configuration section, first set up our project folder, for example, put under/home/user/www, and then create a Django project named MySite with the following command:

cd/home/user/wwwdjango-admin.py Startproject MySite

OK, this next/home/user/www directory should have a sub-folder name MySite, we need to drop a WSGI configuration file to/home/user/www/mysite/inside, the configuration file named wsgi.py, the content is as follows:

The next step is to modify the Uwsgi configuration file by first setting up a configuration file with the following command:

Cat >/etc/uwsgi/apps-enabled/www.ini << eof[uwsgi]socket =/var/run/uwsgi/app/www/socketchmod-socket = 666limit-as = 256processes = 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

Note The above socket configuration, this is the path on my machine, you can cd/var/run inside to find, and then configure the correct, then set up the Nginx configuration file:

Cat >/etc/nginx/sites-enabled/www << eofserver {    listen  ;    server_name localhost;    Location/{        include uwsgi_params;        Uwsgi_pass unix:///var/run/uwsgi/app/www/socket;    }} Eof

Here Uwsgi_pass is still the above we find the socket, note server_name, this is the domain we bind, through nginx-s reload reload Nginx.

OK, now you can visit to see if it's successful? If 502 bad Gateway appears, check to see if UWSGI is booting properly via PS aux | grep Uwsgi looks at the list of processes and finally checks to see if the socket path is correct.

If there is Uwsgi Error:python application not found this problem, the main problem is the path configuration, which you can check/var/log/uwsgi the following log files, such as I have encountered Importerror:no Module named Django.core.handlers.wsgi This problem, is actually wsgi.py inside the path is not configured correctly.

  • 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.