Guest User

Untitled

a guest
Jul 29th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. ======================================
  2. Setting up Nginx, uWSGI and Python3
  3. ======================================
  4.  
  5. First off, I'm traditionally a PHP developer, but am looking at moving across to Python. I really struggled to find decent documentation on how to get a server up and running for deploying Python web applications from the point of view of someone coming from PHP. The main problems I came across with documentation were:
  6.  
  7. 1) Only showed you how to run the server for a single web application.
  8. 2) Only showed you how to configure the app, not the server it was running on.
  9.  
  10. My preferred workflow for development is by setting up a new VM in VMware Fusion and then forwarding through all requests to that VM via /etc/hosts. This might not be the optimal way to get things up and running, but it works for me.
  11.  
  12. SITE_URL refers to the primary domain for the site.
  13. SITE_DIR refers to the location on disk that the site is located.
  14.  
  15. Typical folder structure of an app looks something like:
  16.  
  17. root
  18. app # site code goes in here
  19. config
  20. data
  21. cache
  22. log
  23. libs
  24. vendors # frameworks and 3rd party libraries go in here
  25. public
  26. css
  27. img
  28. js
  29.  
  30. ---------------------------------------
  31. Installing required tools
  32. ---------------------------------------
  33. We'll start by installing the required tools via apt-get.
  34.  
  35. apt-get install uwsgi uwsgi-plugin-python3 nginx-full python-setuptools python-pip
  36.  
  37. ---------------------------------------
  38. Creating the uWSGI upstart
  39. ---------------------------------------
  40. Next we need to create the upstart that will be used start the uWSGI service.
  41.  
  42. vi /etc/init/uwsgi.conf
  43.  
  44. # uWSGI - Manage uWSGI Application Server
  45. description "uWSGI Emperor Mode"
  46. start on (filesystem and net-device-up IFACE=lo)
  47. stop on runlevel [!2345]
  48. respawn
  49. exec /usr/bin/uwsgi --emperor /etc/uwsgi/vassals/sites-enabled/ --logto /var/log/uwsgi.log
  50.  
  51. initctl reload-configuration
  52. update-alternatives --set uwsgi /usr/bin/uwsgi_python32
  53.  
  54. ---------------------------------------
  55. Creating the site configuration file for nginx
  56. ---------------------------------------
  57. Create the configuration file for the site in the sites-available folder:
  58. vi /etc/nginx/sites-available/SITE_URL
  59.  
  60. And then use the following to configure the site:
  61. upstream wsgicluster {
  62. server unix://tmp/SITE_URL.sock
  63. }
  64. server {
  65. listen 80;
  66. server_name SITE_URL;
  67. error_log SITE_DIR/data/log/error.log;
  68. access_log SITE_DIR/data/log/access.log;
  69. location / {
  70. include uwsgi_params;
  71. uwsgi_pass wsgicluster;
  72. }
  73. location -^/(img|js|css)/ {
  74. root SITE_DIR/public;
  75. expires 30d;
  76. }
  77. location = /favicon.ico {
  78. log_not_found off;
  79. }
  80. }
  81.  
  82. Then finally we link the sites-available configuration file to the sites-enabled:
  83. ln -s /etc/nginx/sites-available/SITE_URL /etc/nginx/sites-enabled/SITE_URL
  84.  
  85. ---------------------------------------
  86. Creating the vassal for uWSGI emperor
  87. ---------------------------------------
  88. Create the folders for sites-available and sites-enabled:
  89. mkdir /etc/uwsgi/sites-available
  90. mkdir /etc/uwsgi/sites-enabled
  91.  
  92. Create the configuration vassal file:
  93. vi /etc/uwsgi/sites-available/SITE_URL.yml
  94.  
  95. And then use the following contents (you can tweak these settings):
  96. uwsgi:
  97. master: true
  98. processes: 1
  99. vaccum: true
  100. chmod-socket: 666
  101. uid: www-data
  102. gid: www-data
  103. plugins: python32
  104. socket: /tmp/SITE_URL.sock
  105. chdir: SITE_DIR
  106. pythonpath: SITE_DIR
  107. module: application
  108. touch-reload: SITE_DIR/application.py
  109.  
  110. Finally restart the services:
  111. service uwsgi start
  112. service nginx start
Add Comment
Please, Sign In to add comment