/doc/install/nginx-uwsgi.rst

https://bitbucket.org/mediacorecommunity/mediacore-community · ReStructuredText · 232 lines · 180 code · 52 blank · 0 comment · 0 complexity · 936ae226d879a453c9e15b58859f473e MD5 · raw file

  1. :orphan:
  2. .. _install_nginx-uwsgi:
  3. ========================
  4. NGINX & uWSGI Deployment
  5. ========================
  6. **NOTE 1:** This tutorial assumes that you already have NGINX and uWSGI installed.
  7. If you're on a shared hosting platform, and don't have the ability to install
  8. uWSGI and NGINX, have no fear. Your hosting provider probably already supports
  9. mod_fastcgi so check out the :ref:`install_apache-fastcgi` tutorial.
  10. **NOTE 2:** If you're administrating your own system and looking for pointers on how
  11. to get uWSGI or NGINX installed, check out the developer's site.
  12. `uWSGI main site
  13. <http://projects.unbit.it/uwsgi/>`_.
  14. You can find example configurations as
  15. well as the official documentation on uWSGI here.
  16. `uWSGI Mailing List
  17. <http://lists.unbit.it/cgi-bin/mailman/listinfo/uwsgi>`_.
  18. uWSGI also has a fantastic mailing list where the author contributes daily, and
  19. is always willing to help out new users try to work through their issues.
  20. `NGINX main site
  21. <http://wiki.nginx.org>`_.
  22. This is the main NGINX Wiki that where you can
  23. find the full documentation for NGINX, all NGINX modules as well as many
  24. recipes and tips for configuring NGINX.
  25. Components
  26. ----------
  27. The following five components are involved in getting web requests through to
  28. MediaCore CE with this setup.
  29. ``NGINX``
  30. the web server
  31. ``uWSGI``
  32. WSGI application container that serves MediaCore CE
  33. ``nginx.conf``
  34. Your nginx configuration file.
  35. ``deployment.ini``
  36. The MediaCore CE deployment file for your production server.
  37. ``mediacore``
  38. the reason we're here!
  39. Instructions
  40. ------------
  41. **NOTE 1:** You should have already created a ``deployment.ini`` file and set
  42. the permissions on the ``data`` subdirectories as outlined in
  43. :ref:`production_deployments`
  44. **NOTE 2:** The following instructions assume that youâ&#x20AC;&#x2122;re deploying MediaCore CE
  45. to your own domain at ``http://site.example`` and that your MediaCore CE
  46. installation is configured like this:
  47. ``MediaCore CE Virtual Environment > /path/to/venv``
  48. ``MediaCore CE Source Code > /path/to/mediacore_install``
  49. uWSGI Configuration
  50. -------------------
  51. The first thing you will want to do is to add a new [uwsgi] section to your
  52. deployment.ini file. This section will contain various parameters for the uWSGI
  53. server and will automatically load your application when NGINX passes requests
  54. over to it. Here is a good base for your uWSGI parameters, and explained in
  55. more detail below:
  56. .. sourcecode:: ini
  57. [uwsgi]
  58. socket = /tmp/uwsgi-mediacore.sock
  59. master = true
  60. processes = 5
  61. home = /path/to/venv
  62. daemonize = /var/log/uwsgi.log
  63. ``socket:`` uWSGI will create a unix socket at this location on your system
  64. that will listen for incoming requests. You can also use a TCP socket, but if
  65. you are running NGINX on the same server as uWSGI, a standard unix socket will
  66. be much faster. This socket name will be used again in your NGINX configuration
  67. to pass requests into uWSGI.
  68. ``master:`` Enables uWSGI master process manager. You should be enabling this
  69. unless you have a good reason not to.
  70. ``processes:`` The number of uWSGI worker processes to spawn.
  71. ``home:`` This defines your apps home / VirtualEnvironment and will allow uWSGI
  72. to correctly find your app. If you get errors about your app not loading,
  73. check that this setting is correct first.
  74. ``daemonize:`` Run uWSGI in daemon mode, and log all data to the file specified.
  75. Now that you have created a [uwsgi] ini block in deployment.ini, you can launch
  76. uWSGI and point to your ini file like this:
  77. .. sourcecode:: bash
  78. # launch uWSGI and serve with settings from your config file
  79. sudo uwsgi --ini-paste /path/to/deployment.ini
  80. If everything went well uWSGI will now be listening on the unix socket you
  81. specified above. Of course you still need to tell NGINX how to talk to uWSGI so
  82. let's configure that now.
  83. NGINX Configuration
  84. -------------------
  85. When configuring NGINX for use with uWSGI to serve MediaCore CE, you need to make
  86. sure that you define how to talk to uWSGI, your static file paths, and also
  87. your XSendfile internal path that MediaCore CE will serve media files from.
  88. At this point, it will probably be easier to just walk through a fully
  89. functional MediaCore CE NGINX configuration file, so here it is. This is a generic
  90. configuration and will probably suit most use cases:
  91. .. sourcecode:: nginx
  92. # Configure our MediaCore CE App for NGINX+UWSGI
  93. server {
  94. # Define server parameters:
  95. # Listen on port 80 for requests to mydomain.com
  96. # log to /path/to/nginx/logs/mydomain.access.log using the main log format.
  97. listen 80;
  98. server_name mydomain.com;
  99. access_log logs/mydomain.access.log main;
  100. # Important: This setting will define maximum upload size, so make
  101. # sure it is sane for your purposes! For example, if you have a
  102. # 300MB upload limit in MediaCore CE, people will say "Yay! I can upload
  103. # my 300MB video!" However, if this setting is set to 10MB, then no
  104. # one will be able to upload videos over 10MB and people will not
  105. # like you very much.
  106. client_max_body_size 1500M;
  107. # Define NGINX Static File Paths
  108. #
  109. # First, define our default document root for static file serving.
  110. # NGINX configuration uses inheritance, so defining our base root here
  111. # will assign it to every other location{} declaration unless an
  112. # alternate path is specified. Also, any files that reside in the root will
  113. # of course not need to be defined as they are included. An example
  114. # would be /crossdomain.xml
  115. #
  116. # * Note: The ~* used in our location block regexes activates
  117. # case insensitive matching on the paths. This may or may not be
  118. # what you are after in your configuration. If you want /path and /Path
  119. # to be different paths, then just use ~ not ~*
  120. #
  121. # See the NGINX docs on Location regex matching for more details:
  122. # http://wiki.nginx.org/HttpCoreModule#location
  123. root /path/to/mediacore_install/mediacore/public;
  124. # And now we define the rest of our static locations below
  125. location ~* ^/(appearance)/ {
  126. root /path/to/data ;
  127. break;
  128. }
  129. # All media and podcast images
  130. location ~* ^(/images\/media|images\/podcasts) {
  131. root /path/to/data ;
  132. break;
  133. }
  134. # Our standard public file paths
  135. location ~* ^/(styles|scripts|images)/ {
  136. expires max;
  137. add_header Cache-Control "public";
  138. break;
  139. }
  140. # Configure NGINX XSendfile.
  141. # We use an alias here instead of root so the path info
  142. # __mediacore_serve__ is stripped off.
  143. # Note: __mediacore_serve__ is defined in MediaCore CE as the path to serve NGINX files from.
  144. # Note: We define this as an "internal" location to prevent it from
  145. # being served directly to end users.
  146. location /__mediacore_serve__ {
  147. alias /path/to/data/media;
  148. internal;
  149. }
  150. # Declare our default location to pass through to our app
  151. # This will match any request not defined above and pass it to uWSGI
  152. # Note: The uwsgi_pass directive must use the same socket that was
  153. # defined in your deployment.ini [uwsgi] block.
  154. # Note: Make sure that you pass in SCRIPT_NAME = '' otherwise uWSGI
  155. # will raise a keyError when loading MediaCore CE.
  156. location / {
  157. uwsgi_pass unix:///tmp/uwsgi-mediacore.sock;
  158. include uwsgi_params;
  159. uwsgi_param SCRIPT_NAME '';
  160. }
  161. }
  162. At this point you can start your NGINX server and test out your app!
  163. Performance Enhancements
  164. ------------------------
  165. By default, all files are served through MediaCore CE. The configuration above
  166. ensures that NGINX will serve all static files (.css, .js, and images) directly,
  167. but MediaCore CE will still check for static files before serving any page. There
  168. are two speedups we can enable here.
  169. First, edit one line in ``/path/to/deployment.ini``.
  170. Find the static_files line, and set it to false.
  171. .. sourcecode:: ini
  172. # disable static file serving with MediaCore CE
  173. static_files = false
  174. The second speedup will allow MediaCore CE to take advantage of NGINX XSendfile
  175. and have NGINX serve all media files (.mp3, .mp4, etc.) directly. To enable
  176. this, edit another line in ``/path/to/deployment.ini``.
  177. Find the files_serve_method line, and set it to nginx_redirect.
  178. .. sourcecode:: ini
  179. # enable NGINX as te default file serve method
  180. files_serve_method = nginx_redirect