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

Source: Internet
Author: User
Tags chmod install django python mysql

Recently tried to migrate the project to the Python environment, especially a clean Debian system, ready to reconfigure the environment, the Internet to find some running the Python Web Environment solution, finalized NGINX+UWSGI combination, Nginx used more, skilled Uwsgi is said to have a good performance and would like to try it.

Most of the online tutorials are required to download the source package to the UWSGI official website, and then install it in a compiled manner, for example, for a new Debian system, you can install it by using the following command:

Apt-get update
apt-get upgrade
apt-get install build-essential psmisc apt-get install Python-dev
Libxml2-dev
apt-get Install python-setuptools
cd/opt/
wget uwsgi-latest.tar.gz
tar-zxvf uwsgi-latest.tar.gz
mv uwsgi-latest/uwsgi/
cd uwsgi/
python setup.py Install

There are also some configuration I do not introduce, you can refer to the Linode Library of the "Deploy Python Code with WSGI using the Uwsgi and Nginx on Debian 6 (squeeze)," described.

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

And then find what we need through the Debian Packages Web site. In the SID, the SID version is still in the development test, not the final stable version, but it's enough for us to test, and in order to use the software in the SID, we 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 you have configured the source list, we can install it by using the following command:

Apt-get Update
apt-get install Uwsgi uwsgi-core Uwsgi-plugin-python

After the installation is complete, the profile is routinely placed inside the/ETC/UWSGI, and service control can be implemented by INVOKE-RC.D Uwsgi followed by the command parameters, but here's an episode I have to mention that the installation may be a mistake to quote:

The following packages have unmet dependencies: libc6-dev:breaks:gcc-4.4 
(< 4.4.6-4) but 4.4.5-8 LEDs. 
E:broken Packages

It looks like the dependency pack gcc version does not match, and I later solved it by following this way:

Apt-get update
apt-get Upgrade # First try to upgrade the System software package
 
Apt-get Install Libc6-dev

The next step is to install Django and Python MySQL support, and continue apt commands:

Apt-get Install Python-django Python-mysqldb

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

Cd/usr/lib
ls-l | grep python

You may find that several versions of Python are installed by default in the system, for example, 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 just go through the following commands:

Python--version

You know Django is installed in the Python directory under the site-packages, but these few Python directories do not site-packages This folder, in fact, we can first use the following command to locate:

Python-c "from distutils.sysconfig import get_python_lib; Print Get_python_lib () "

The above command will print the Python package path on the console, for example, where we may get the Dist-packages directory, switch to this directory and you'll find Django there. Add permissions to the django-admin.py, 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.py
ln-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 the default Python version of Uwsgi by using the following command (generally no need to switch):

Update-alternatives--config Uwsgi-plugin-python

All right, here's the installation section, so here's the configuration section, where we'll start by creating a folder for our project, such as under/home/user/www, and then creating a Django project called MySite with the following command:

Cd/home/user/www
django-admin.py startproject MySite

Well, under the/home/user/www directory should have a subfolder named MySite, we need to lose a WSGI configuration file to/home/user/www/mysite/inside, the configuration file named wsgi.py, the contents are as follows:

The next step is to modify the Uwsgi configuration file, and first set up a profile with the following command:

Cat >/etc/uwsgi/apps-enabled/www.ini << EOF
[UWSGI]
socket =/var/run/uwsgi/app/www/socket
Chmod-socket = 666
Limit-as = 256
processes = 6
max-request = Memory-report =
true
Enable-threads = True
Pythonpath =/home/user/www
chdir =/home/user/www/mysite wsgi-file
=/home/user/ www/mysite/wsgi.py
EOF
 
# restart Uwsgi
invoke-rc.d uwsgi Restart

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

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

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

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

If there is a problem with Uwsgi Error:python application not found, which is mainly a path configuration problem, you can check the log file below/var/log/uwsgi, for example, I've met Importerror:no Module named Django.core.handlers.wsgi This problem, is actually wsgi.py inside the path configuration is incorrect.

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.