Install and deploy Nginx + uWSGI + Django1.9.7 in Ubuntu 16.04
Due to the slight difference between the new version and the old version, I searched for a lot of relevant information on the Internet. After three days, I finally completed deployment of Nginx + uWSGI + Django1.9.7 under Ubuntu 16.04, next we will detail the writing steps and the problems encountered. The premise is that the virtual environment pyenv + virtualenv is installed.
1. Install uwsgi
1. Install uwsgi in a Virtual Environment
1 # Start environment 2 pyenv activate your_env_name3 pip install uwsgi
2. test uwsgi and create test. py
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello uwsgi!"]
3. uwsgi run the file
The command: uwsgi -- http: 8088 -- wsgi-file test. py was used at the time. The terminal prompts that the -- wsgi-file option is not available, but it has never been successful. Later, you can switch to -- http-socket. But now I tried-http can even run, and I don't know why it didn't work.
uwsgi --http-socket :8088 --wsgi-file test.py
Ii. Install nginx
1. nginx does not need to be downloaded in the virtual environment. It can be directly installed using commands on the terminal.
sudo apt-get install nginx
2. nginx startup and shutdown commands
Cln @ Aspire :~ $ Service nginx start # start cln @ Aspire :~ $ Service nginx stop # disable cln @ Aspire :~ $ Service nginx restart # restart
Cln @ Aspire :~ $ Nginx-V # view nginx version: nginx/1.10.0 (Ubuntu) built with OpenSSL 1.0.g-fips 1 Mar 2016TLS SNI support enabled
3. Modify the default nginx Port
The nginx1.10.0 configuration file contains/etc/nginxnginx. conf,/etc/nginx/conf. default and nginx under d/* and/etc/nginx/sites-available. conf contains default and conf. the configuration in d and the port number are modified in the default file.
Open the default file in the nginx folder and change the port number to 88.
cln@Aspire:~$ sudo gedit /etc/nginx/sites-available/default
4. Restart the nginx service and access http: // 127.0.0.1: 8088. The nginx welcome page is displayed.
3. Deploy django + uwsgi + nginx
1. create the yourProjectName_uwsgi.ini file in your django project folder. The specific parameter configuration is as follows: the most important thing is to configure pythonpath, which specifies the location of django. At that time, the configuration was not accurate, the error "no moudle django" was reported all the time. It took a long time to know why. Later, I checked a lot of information before knowing how to configure pythonpath. The main parameters are annotated.
At that time, socket =: 8088, I wrote http-socket =: 8088, and always reported an error of 502 Bad Gateway. It took a long time to change this sentence to socket.
2. As mentioned earlier, you can create an nginx. conf file under/etc/nginx/conf. d to associate it with uwsgi.
cln@Aspire:~$ sudo vi /etc/nginx/conf.d/nginx.conf
Add the following content to it:
3. Restart the nginx service, activate the virtual environment, and run the. ini file.
# Activate the virtual environment cln @ Aspire :~ /PycharmProjects/webapp $ pyenv activate webenv # Run the command (webenv) cln @ Aspire: ~ in the parent directory of the. ini file :~ /PycharmProjects/webapp $ uwsgi -- ini web_uwsgi.ini
As shown in the following figure.
Visit http: // 127.0.0.1: 88 to access your django project website through nginx.
Summary: it takes you a lot of time to find the problem. Now I want to write down my own problems and solutions and hope to help you with similar problems.
More references
Nginx + uWSGI + Supervisor deploy the Flask application on Ubuntu
Install Nginx + uWSGI + Django on Ubuntu Server 12.04
Deployment of Django + Nginx + uWSGI
Deployment of Nginx + uWSGI + Django + Python application architecture
Ubuntu Server 14.04.2 LTS configure Nginx + Uwsgi + Django
Flask + uWSGI + Nginx + Ubuntu deployment tutorial
This article permanently updates the link address: