WSGI using uWSGI and nginx on Ubuntu 12.04 (Precise Pangolin)
2014-07-07 10:56
399 查看
ContentsPrerequisites
Installing uWSGI and Configuring Nginx
Configuring uWSGI
Additional Application Servers
More Information
Prerequisites
Before you begin installing and configuring the components described in this guide, please make sure you've followed our instructions for setting your hostname. Issue the following commands to make sure it is set properly:hostnamehostname -f
The first command should show your short hostname, and the second should show yourfully qualified domain name (FQDN).
You should also make sure your system is up to date. Enter the following commands, one by one, and install any available updates:apt-get update
apt-get upgrade
Installing uWSGI and Configuring Nginx
To get started, you'll install uWSGI and other packages, and then configure nginx. Here's how:Begin by installing uWSGI and nginx from the Ubuntu repository:apt-get install nginx-full uwsgi uwsgi-plugin-python
Create a virtual host file by entering the following command, replacing example.com with your domain name:nano /etc/nginx/sites-available/example.com
Using the virtual host configuration below as a guide, create your configuration file.File:/etc/nginx/sites-available/example.com
server { listen 80; server_name $hostname; access_log /srv/www/example.com/logs/access.log; error_log /srv/www/example.com/logs/error.log; location / { #uwsgi_pass 127.0.0.1:9001; uwsgi_pass unix:///run/uwsgi/app/example.com/example.com.socket; include uwsgi_params; uwsgi_param UWSGI_SCHEME $scheme; uwsgi_param SERVER_SOFTWARE nginx/$nginx_version; } location /static { root /srv/www/example.com/public_html/static/; index index.html index.htm; } }
Link the virtual host file to sites-enabled by entering the following command, replacing example.com with your domain name:ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
Depending on your configuration, you may wish to remove the link to the default virtual host by entering the following command:rm /etc/nginx/sites-enabled/default
Create directories for your website's data by entering the following commands, one by one, replacing example.com with your domain name:mkdir -p /srv/www/example.com/public_html/static
mkdir /srv/www/example.com/application
mkdir /srv/www/example.com/logs
All requests to URLs ending in /static will be served directly from the /srv/www/example.com/public_html/static directory.
Configuring uWSGI
Now we need to configure uWSGI. Here's how:Create the uWSGI config file by entering the following command, replacing example.com with your domain name:nano /etc/uwsgi/apps-available/example.com.xml
Using the configuration below as a guide, create your configuration file.File:/etc/uwsgi/apps-available/example.com.xml
<uwsgi> <plugin>python</plugin> <socket>/run/uwsgi/app/example.com/example.com.socket</socket> <pythonpath>/srv/www/example.com/application/</pythonpath> <app mountpoint="/"> <script>wsgi_configuration_module</script> </app> <master/> <processes>4</processes> <harakiri>60</harakiri> <reload-mercy>8</reload-mercy> <cpu-affinity>1</cpu-affinity> <stats>/tmp/stats.socket</stats> <max-requests>2000</max-requests> <limit-as>512</limit-as> <reload-on-as>256</reload-on-as> <reload-on-rss>192</reload-on-rss> <no-orphans/> <vacuum/> </uwsgi>
Link the configuration to apps-enabled by entering the following command, replacing example.com with your domain name:ln -s /etc/uwsgi/apps-available/example.com.xml /etc/uwsgi/apps-enabled/example.com.xml
If you want to deploy a "Hello World" application, insert the following code into the /srv/www/example.com/application/wsgi_configuration_module.py file:File:/srv/www/example.com/application/wsgi_configuration_module.py
import os import sys sys.path.append('/srv/www/example.com/application') os.environ['PYTHON_EGG_CACHE'] = '/srv/www/example.com/.python-egg' def application(environ, start_response): status = '200 OK' output = 'Hello World!' response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output]
Restart uWSGI with the command:service uwsgi restart
Restart the web server by issuing the following command:service nginx restart
You can test by pointing a web browser to your domian. If you see Hello World!than you have succesfully configured your Linode for uWSGI with Nginx!
Additional Application Servers
If the Python application you've deployed requires more applicationresources than a single Linode instance can provide, all of themethods for deploying a uWSGI application server are easily scaled torely on multiple uSWGI instances that run on additional Linodes withthe request load balanced using nginx's upstreamcapability. See our documentation of proxy and software loadbalancing with nginxfor more information. For a basic example configuration, see thefollowing example:File excerpt:nginx configurationupstream uwsgicluster { server 127.0.0.1:9001; server 192.168.100.101:9001; server 192.168.100.102:9001; server 192.168.100.103:9001; server 192.168.100.104:9001; } server { listen 80; server_name www.example.com example.com; access_log /srv/www/example.com/logs/access.log; error_log /srv/www/example.com/logs/error.log; location / { include uwsgi_params; uwsgi_pass uwsgicluster; } location /static { root /srv/www/example.com/public_html/static/; index index.html index.htm; } }In this example, we create the uwsgicluster upstream, which hasfive components. One runs on the local interface, and four runon the local network interface of distinct Linodes (the192.168. addresses or the private "back end" network). Theapplication servers that run on those dedicated application serversare identical to the application servers described above. However, theapplication server process must be configured to bind to theappropriate network interface to be capable of responding to requests.
原文链接:https://library.linode.com/web-servers/nginx/python-uwsgi/ubuntu-12.04-precise-pangolin
相关文章推荐
- [django] Deploy Django Applications Using uWSGI and Nginx on Ubuntu 14.04
- Flask、uWSGI and Nginx on Ubuntu 14.04
- Installing Nginx With PHP5 (And PHP-FPM) And MySQL Support (LEMP) On Ubuntu 12.04 LTS [repost]
- Ubuntu_Installing Apache2 With PHP5 And MySQL Support On Ubuntu 12.04 LTS (LAMP)
- Ubuntu 12.04 下部署 Nginx+uWSGI+Flask
- uwsgi+web.py+nginx on Ubuntu14.04 LTS
- How to deploy Node.js Applications with pm2 and Nginx on Ubuntu
- How to display Computer,Home,Network,Trash and Mounted volumes icons on ubuntu 12.04 (Precise) deskt
- using and configure emule (amule) on ubuntu
- How To Install and Secure phpMyAdmin on Ubuntu 12.04(MySQL图形管理)
- Install Scala and SBT using apt-get on Ubuntu 14.04 or any Debian derivative using apt-get
- Build Laravel Blog PigJian by PHP7 and Nginx on Ubuntu
- Ubuntu 12.04 下部署 Nginx+uWSGI+Flask
- Wireshark – No interfaces to capture on using Ubuntu 12.04 - See more at: http://www.networkingnut.n
- Installing Resin using the .deb package on Ubuntu and Debian
- EC2 ubuntu 12.04 nginx uwsgi mysql
- How To Install Wordpress with nginx on Ubuntu 12.04
- How to install Maven 3 on Ubuntu 12.04/12.10/13.04 by using apt-get
- Setting Up OpenERP (Odoo) 9 with Nginx on RHEL/CentOS and Debian/Ubuntu
- Install and test nginx on Ubuntu 14.04