production-deployments.rst 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. .. MediaGoblin Documentation
  2. Written in 2011, 2012, 2013, 2014, 2015 by MediaGoblin contributors
  3. To the extent possible under law, the author(s) have dedicated all
  4. copyright and related and neighboring rights to this software to
  5. the public domain worldwide. This software is distributed without
  6. any warranty.
  7. You should have received a copy of the CC0 Public Domain
  8. Dedication along with this software. If not, see
  9. <http://creativecommons.org/publicdomain/zero/1.0/>.
  10. =========================================
  11. Considerations for Production Deployments
  12. =========================================
  13. This document contains a number of suggestions for deploying
  14. MediaGoblin in actual production environments. Consider
  15. ":doc:`deploying`" for a basic overview of how to deploy MediaGoblin.
  16. Deploy with paste
  17. -----------------
  18. The MediaGoblin WSGI application instance you get with ``./lazyserver.sh`` is
  19. not ideal for a production MediaGoblin deployment. Ideally, you should be able
  20. to use a systemd service file or an init script to launch and restart the
  21. MediaGoblin process.
  22. We will explore setting up MediaGoblin systemd service files and init scripts,
  23. but first we need to create the directory that will store the MediaGoblin logs.
  24. .. _create-log-file-dir:
  25. Create the directory for your log file:
  26. ---------------------------------------
  27. Production logs for the MediaGoblin application are kept in the
  28. ``/var/log/mediagoblin`` directory. Create the directory and give it the
  29. proper permissions::
  30. sudo mkdir -p /var/log/mediagoblin && sudo chown -hR mediagoblin:mediagoblin /var/log/mediagoblin
  31. .. _systemd-service-files:
  32. Use systemd service files
  33. -------------------------
  34. If your operating system uses systemd, you can use systemd ``service files``
  35. to manage both the Celery and Paste processes. Place the following service
  36. files in the ``/etc/systemd/system/`` directory.
  37. The first file should be named ``mediagoblin-celeryd.service``. Be sure to
  38. modify it to suit your environment's setup:
  39. .. code-block:: bash
  40. # Set the WorkingDirectory, Environment and ExecStart values to match your environment.
  41. # If using Debian/*buntu, mkdir and chown are located in /bin/mkdir and /bin/chown, respectively.
  42. # If using Fedora/CentOS/Red Hat, mkdir and chown are located in /usr/bin/mkdir and /usr/bin/chown, respectively.
  43. [Unit]
  44. Description=Mediagoblin Celeryd
  45. [Service]
  46. User=mediagoblin
  47. Group=mediagoblin
  48. Type=simple
  49. WorkingDirectory=/srv/mediagoblin.example.org/mediagoblin
  50. # Create directory for PID (if needed) and set ownership
  51. ExecStartPre=/bin/mkdir -p /run/mediagoblin
  52. ExecStartPre=/bin/chown -hR mediagoblin:mediagoblin /run/mediagoblin
  53. # Celery process will run as the `mediagoblin` user after start.
  54. Environment=MEDIAGOBLIN_CONFIG=/srv/mediagoblin.example.org/mediagoblin/mediagoblin_local.ini \
  55. CELERY_CONFIG_MODULE=mediagoblin.init.celery.from_celery
  56. ExecStart=/srv/mediagoblin.example.org/mediagoblin/bin/celery worker \
  57. --logfile=/var/log/mediagoblin/celery.log \
  58. --loglevel=INFO
  59. PIDFile=/run/mediagoblin/mediagoblin-celeryd.pid
  60. [Install]
  61. WantedBy=multi-user.target
  62. The second file should be named ``mediagoblin-paster.service``:
  63. .. code-block:: bash
  64. # Set the WorkingDirectory, Environment and ExecStart values to match your environment.
  65. # If using Debian/*buntu, mkdir and chown are located in /bin/mkdir and /bin/chown, respectively.
  66. # If using Fedora/CentOS/Red Hat, mkdir and chown are located in /usr/bin/mkdir and /usr/bin/chown, respectively.
  67. [Unit]
  68. Description=Mediagoblin
  69. [Service]
  70. Type=forking
  71. User=mediagoblin
  72. Group=mediagoblin
  73. Environment=CELERY_ALWAYS_EAGER=false
  74. WorkingDirectory=/srv/mediagoblin.example.org/mediagoblin
  75. # Start mg-paster process as root, then switch to mediagoblin user/group
  76. PermissionsStartOnly=true
  77. ExecStartPre=-/bin/mkdir -p /run/mediagoblin
  78. ExecStartPre=/bin/chown -hR mediagoblin:mediagoblin /run/mediagoblin
  79. ExecStart=/srv/mediagoblin.example.org/mediagoblin/bin/paster serve \
  80. /srv/mediagoblin.example.org/mediagoblin/paste_local.ini \
  81. --pid-file=/var/run/mediagoblin/mediagoblin.pid \
  82. --log-file=/var/log/mediagoblin/mediagoblin.log \
  83. --daemon \
  84. --server-name=fcgi fcgi_host=127.0.0.1 fcgi_port=26543
  85. ExecStop=/srv/mediagoblin.example.org/mediagoblin/bin/paster serve \
  86. --pid-file=/var/run/mediagoblin/mediagoblin.pid \
  87. /srv/mediagoblin.example.org/mediagoblin/paste_local.ini stop
  88. PIDFile=/var/run/mediagoblin/mediagoblin.pid
  89. [Install]
  90. WantedBy=multi-user.target
  91. Enable these processes to start at boot by entering::
  92. sudo systemctl enable mediagoblin-celeryd.service && sudo systemctl enable mediagoblin-paster.service
  93. Start the processes for the current session with::
  94. sudo systemctl start mediagoblin-paster.service
  95. sudo systemctl start mediagoblin-celeryd.service
  96. If either command above gives you an error, you can investigate the cause of
  97. the error by entering::
  98. sudo systemctl status mediagoblin-celeryd.service or
  99. sudo systemctl status mediagoblin-paster.service
  100. The above ``systemctl status`` command is also useful if you ever want to
  101. confirm that a process is still running. If you make any changes to the service
  102. files, you can reload the service files by entering::
  103. sudo systemctl daemon-reload
  104. After entering that command, you can attempt to start the Celery or Paste
  105. processes again.
  106. .. _init-script:
  107. Use an init script
  108. ------------------
  109. If your system does not use systemd, you can use the following command as the
  110. basis for an init script:
  111. .. code-block:: bash
  112. CELERY_ALWAYS_EAGER=true \
  113. /srv/mediagoblin.example.org/mediagoblin/bin/paster serve \
  114. /srv/mediagoblin.example.org/mediagoblin/paste.ini \
  115. --pid-file=/var/run/mediagoblin.pid \
  116. --server-name=fcgi fcgi_host=127.0.0.1 fcgi_port=26543
  117. The above configuration places MediaGoblin in "always eager" mode
  118. with Celery, this means that submissions of content will be processed
  119. synchronously, and the user will advance to the next page only after
  120. processing is complete. If we take Celery out of "always eager mode,"
  121. the user will be able to immediately return to the MediaGoblin site
  122. while processing is ongoing. In these cases, use the following command
  123. as the basis for your script:
  124. .. code-block:: bash
  125. CELERY_ALWAYS_EAGER=false \
  126. /srv/mediagoblin.example.org/mediagoblin/bin/paster serve \
  127. /srv/mediagoblin.example.org/mediagoblin/paste.ini \
  128. --pid-file=/var/run/mediagoblin.pid \
  129. --server-name=fcgi fcgi_host=127.0.0.1 fcgi_port=26543
  130. Members of the MediaGoblin community have provided init scripts for the
  131. following GNU/Linux distributions:
  132. Debian
  133. * `GNU MediaGoblin init scripts
  134. <https://github.com/joar/mediagoblin-init-scripts>`_
  135. by `Joar Wandborg <http://wandborg.se>`_
  136. Arch Linux
  137. * `MediaGoblin - ArchLinux rc.d scripts
  138. <http://whird.jpope.org/2012/04/14/mediagoblin-archlinux-rcd-scripts>`_
  139. by `Jeremy Pope <http://jpope.org/>`_
  140. * `Mediagoblin init script on Archlinux
  141. <http://chimo.chromic.org/2012/03/01/mediagoblin-init-script-on-archlinux/>`_
  142. by `Chimo <http://chimo.chromic.org/>`_
  143. You can reference these scripts to create an init script for your own operating
  144. system. Similar scripts will be in your system's ``/etc/init.d/``
  145. or ``/etc/rc.d/`` directory, but the specifics of an init script will vary from
  146. one distribution to the next.
  147. Separate celery
  148. ---------------
  149. MediaGoblin uses `Celery`_ to handle heavy and long-running tasks. Celery can
  150. be launched in two ways:
  151. 1. Embedded in the MediaGoblin WSGI application [#f-mediagoblin-wsgi-app]_.
  152. This is the way ``./lazyserver.sh`` does it for you. It's simple as you
  153. only have to run one process. The only bad thing with this is that the
  154. heavy and long-running tasks will run *in* the webserver, keeping the user
  155. waiting each time some heavy lifting is needed as in for example processing
  156. a video. This could lead to problems as an aborted connection will halt any
  157. processing and since most front-end web servers *will* terminate your
  158. connection if it doesn't get any response from the MediaGoblin WSGI
  159. application in a while.
  160. 2. As a separate process communicating with the MediaGoblin WSGI application
  161. via a `broker`_. This offloads the heavy lifting from the MediaGoblin WSGI
  162. application and users will be able to continue to browse the site while the
  163. media is being processed in the background.
  164. .. _`broker`: http://docs.celeryproject.org/en/latest/getting-started/brokers/
  165. .. _`celery`: http://www.celeryproject.org/
  166. .. [#f-mediagoblin-wsgi-app] The MediaGoblin WSGI application is the part that
  167. of MediaGoblin that processes HTTP requests.
  168. To launch Celery separately from the MediaGoblin WSGI application:
  169. 1. Make sure that the ``CELERY_ALWAYS_EAGER`` environment variable is unset or
  170. set to ``false`` when launching the MediaGoblin WSGI application.
  171. 2. Start the ``celeryd`` main process with
  172. .. code-block:: bash
  173. CELERY_CONFIG_MODULE=mediagoblin.init.celery.from_celery ./bin/celeryd
  174. If you use our example systemd ``service files``, Celery will be set to the
  175. "CELERY_ALWAYS_EAGER=false" value by default. This will provide your users
  176. with the best user experience, as all media processing will be done in the
  177. background.
  178. .. _sentry:
  179. Set up sentry to monitor exceptions
  180. -----------------------------------
  181. We have a plugin for `raven`_ integration, see the ":doc:`/plugindocs/raven`"
  182. documentation.
  183. .. _`raven`: http://raven.readthedocs.org
  184. .. TODO insert init script here
  185. .. TODO are additional concerns ?
  186. .. Other Concerns
  187. .. --------------