deploying.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. .. MediaGoblin Documentation
  2. Written in 2011, 2012, 2013 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. .. _deploying-chapter:
  11. =====================
  12. Deploying MediaGoblin
  13. =====================
  14. GNU MediaGoblin is fairly new, and so at the time of writing there aren't
  15. easy package-manager-friendly methods to install it. However, doing a basic
  16. install isn't too complex in and of itself. Following this deployment guide
  17. will take you step-by-step through setting up your own instance of MediaGoblin.
  18. Of course, when it comes to setting up web applications like MediaGoblin,
  19. there's an almost infinite way to deploy things, so for now, we'll keep it
  20. simple with some assumptions. We recommend a setup that combines MediaGoblin +
  21. virtualenv + fastcgi + nginx on a .deb- or .rpm-based GNU/Linux distro.
  22. Other deployment options (e.g., deploying on FreeBSD, Arch Linux, using
  23. Apache, etc.) are possible, though! If you'd prefer a different deployment
  24. approach, see our
  25. `Deployment wiki page <http://wiki.mediagoblin.org/Deployment>`_.
  26. .. note::
  27. These tools are for site administrators wanting to deploy a fresh
  28. install. If you want to join in as a contributor, see our
  29. `Hacking HOWTO <http://wiki.mediagoblin.org/HackingHowto>`_ instead.
  30. .. note::
  31. Throughout the documentation we use the ``sudo`` command to indicate that
  32. an instruction requires elevated user privileges to run. You can issue
  33. these commands as the ``root`` user if you prefer.
  34. If you need help configuring ``sudo``, see the
  35. `Debian wiki <https://wiki.debian.org/sudo/>`_ or the
  36. `Fedora Project wiki <https://fedoraproject.org/wiki/Configuring_Sudo/>`_.
  37. Prepare System
  38. --------------
  39. Dependencies
  40. ~~~~~~~~~~~~
  41. MediaGoblin has the following core dependencies:
  42. - Python 2.7 or Python 3.4+
  43. - `python-lxml <http://lxml.de/>`_
  44. - `git <http://git-scm.com/>`_
  45. - `SQLite <http://www.sqlite.org/>`_/`PostgreSQL <http://www.postgresql.org/>`_
  46. - `Python Imaging Library <http://www.pythonware.com/products/pil/>`_ (PIL)
  47. - `virtualenv <http://www.virtualenv.org/>`_
  48. - `nodejs <https://nodejs.org>`_
  49. On a DEB-based system (e.g Debian, gNewSense, Trisquel, *buntu, and
  50. derivatives) issue the following command::
  51. sudo apt-get install git-core python python-dev python-lxml \
  52. python-imaging python-virtualenv npm nodejs-legacy automake \
  53. nginx
  54. On a RPM-based system (e.g. Fedora, RedHat, and derivatives) issue the
  55. following command::
  56. sudo yum install python-paste-deploy python-paste-script \
  57. git-core python python-devel python-lxml python-imaging \
  58. python-virtualenv npm automake nginx
  59. (Note: MediaGoblin now officially supports Python 3. You may instead
  60. substitute from "python" to "python3" for most package names in the
  61. Debian instructions and this should cover dependency installation.
  62. These instructions have not yet been tested on Fedora.)
  63. Configure PostgreSQL
  64. ~~~~~~~~~~~~~~~~~~~~
  65. .. note::
  66. MediaGoblin currently supports PostgreSQL and SQLite. The default is a
  67. local SQLite database. This will "just work" for small deployments.
  68. For medium to large deployments we recommend PostgreSQL.
  69. If you don't want/need postgres, skip this section.
  70. These are the packages needed for Debian Jessie (stable)::
  71. sudo apt-get install postgresql postgresql-client python-psycopg2
  72. These are the packages needed for an RPM-based system::
  73. sudo yum install postgresql postgresql-server python-psycopg2
  74. An rpm-based system also requires that you initialize and start the
  75. PostgresSQL database with a few commands. The following commands are
  76. not needed on a Debian-based platform, however::
  77. sudo /usr/bin/postgresql-setup initdb
  78. sudo systemctl enable postgresql
  79. sudo systemctl start postgresql
  80. The installation process will create a new *system* user named ``postgres``,
  81. which will have privilegies sufficient to manage the database. We will create a
  82. new database user with restricted privilegies and a new database owned by our
  83. restricted database user for our MediaGoblin instance.
  84. In this example, the database user will be ``mediagoblin`` and the database
  85. name will be ``mediagoblin`` too.
  86. We'll add these entities by first switching to the *postgres* account::
  87. sudo su - postgres
  88. This will change your prompt to a shell prompt, such as *-bash-4.2$*. Enter
  89. the following *createuser* and *createdb* commands at that prompt. We'll
  90. create the *mediagoblin* database user first::
  91. # this command and the one that follows are run as the ``postgres`` user:
  92. createuser -A -D mediagoblin
  93. Then we'll create the database where all of our MediaGoblin data will be stored::
  94. createdb -E UNICODE -O mediagoblin mediagoblin
  95. where the first ``mediagoblin`` is the database owner and the second
  96. ``mediagoblin`` is the database name.
  97. Type ``exit`` to exit from the 'postgres' user account.::
  98. exit
  99. .. caution:: Where is the password?
  100. These steps enable you to authenticate to the database in a password-less
  101. manner via local UNIX authentication provided you run the MediaGoblin
  102. application as a user with the same name as the user you created in
  103. PostgreSQL.
  104. More on this in :ref:`Drop Privileges for MediaGoblin <drop-privileges-for-mediagoblin>`.
  105. .. _drop-privileges-for-mediagoblin:
  106. Drop Privileges for MediaGoblin
  107. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  108. MediaGoblin does not require special permissions or elevated
  109. access to run. As such, the preferred way to run MediaGoblin is to
  110. create a dedicated, unprivileged system user for the sole purpose of running
  111. MediaGoblin. Running MediaGoblin processes under an unprivileged system user
  112. helps to keep it more secure.
  113. The following command (entered as root or with sudo) will create a
  114. system account with a username of ``mediagoblin``. You may choose a different
  115. username if you wish.
  116. If you are using a Debian-based system, enter this command::
  117. sudo useradd -c "GNU MediaGoblin system account" -d /var/lib/mediagoblin -m -r -g www-data mediagoblin
  118. If you are using an RPM-based system, enter this command::
  119. sudo useradd -c "GNU MediaGoblin system account" -d /var/lib/mediagoblin -m -r -g nginx mediagoblin
  120. This will create a ``mediagoblin`` user and assign it to a group that is
  121. associated with the web server. This will ensure that the web server can
  122. read the media files (images, videos, etc.) that users upload.
  123. We will also create a ``mediagoblin`` group and associate the mediagoblin
  124. user with that group, as well::
  125. sudo groupadd mediagoblin && sudo usermod --append -G mediagoblin mediagoblin
  126. No password will be assigned to this account, and you will not be able
  127. to log in as this user. To switch to this account, enter::
  128. sudo su mediagoblin -s /bin/bash
  129. To return to your regular user account after using the system account, type
  130. ``exit``.
  131. .. _create-mediagoblin-directory:
  132. Create a MediaGoblin Directory
  133. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  134. You should create a working directory for MediaGoblin. This document
  135. assumes your local git repository will be located at
  136. ``/srv/mediagoblin.example.org/mediagoblin/``.
  137. Substitute your prefered local deployment path as needed.
  138. Setting up the working directory requires that we first create the directory
  139. with elevated priviledges, and then assign ownership of the directory
  140. to the unprivileged system account.
  141. To do this, enter the following command, changing the defaults to suit your
  142. particular requirements. On a Debian-based platform you will enter this::
  143. sudo mkdir -p /srv/mediagoblin.example.org && sudo chown -hR mediagoblin:www-data /srv/mediagoblin.example.org
  144. On an RPM-based distribution, enter this command::
  145. sudo mkdir -p /srv/mediagoblin.example.org && sudo chown -hR mediagoblin:nginx /srv/mediagoblin.example.org
  146. .. note::
  147. Unless otherwise noted, the remainder of this document assumes that all
  148. operations are performed using this unprivileged account.
  149. Install MediaGoblin and Virtualenv
  150. ----------------------------------
  151. We will now switch to our 'mediagoblin' system account, and then set up
  152. our MediaGoblin source code repository and its necessary services.
  153. You should modify these commands to suit your own environment.
  154. Change to the MediaGoblin directory that you just created::
  155. sudo su mediagoblin -s /bin/bash # to change to the 'mediagoblin' account
  156. $ cd /srv/mediagoblin.example.org
  157. Clone the MediaGoblin repository and set up the git submodules::
  158. $ git clone git://git.savannah.gnu.org/mediagoblin.git -b stable
  159. $ cd mediagoblin
  160. $ git submodule init && git submodule update
  161. .. note::
  162. The MediaGoblin repository used to be on gitorious.org, but since
  163. gitorious.org shut down, we had to move. We are presently on
  164. Savannah. You may need to update your git repository location::
  165. $ git remote set-url origin git://git.savannah.gnu.org/mediagoblin.git
  166. Set up the hacking environment::
  167. $ ./bootstrap.sh && ./configure && make
  168. (Note that if you'd prefer to run MediaGoblin with Python 3, pass in
  169. `--with-python3` to the `./configure` command.)
  170. Create and set the proper permissions on the ``user_dev`` directory.
  171. This directory will be used to store uploaded media files::
  172. $ mkdir user_dev && chmod 750 user_dev
  173. Assuming you are going to deploy with FastCGI, you should also install
  174. flup::
  175. $ ./bin/easy_install flup
  176. (Note, if you're running Python 2, which you probably are at this
  177. point in MediaGoblin's development, you'll need to run:)
  178. $ ./bin/easy_install flup==1.0.3.dev-20110405
  179. The above provides an in-package install of ``virtualenv``. While this
  180. is counter to the conventional ``virtualenv`` configuration, it is
  181. more reliable and considerably easier to configure and illustrate. If
  182. you're familiar with Python packaging you may consider deploying with
  183. your preferred method.
  184. .. note::
  185. What if you don't want an in-package ``virtualenv``? Maybe you
  186. have your own ``virtualenv``, or you are building a MediaGoblin
  187. package for a distribution. There's no need necessarily for the
  188. virtualenv produced by ``./configure && make`` by default other
  189. than attempting to simplify work for developers and people
  190. deploying by hiding all the virtualenv and bower complexity.
  191. If you want to install all of MediaGoblin's libraries
  192. independently, that's totally fine! You can pass the flag
  193. ``--without-virtualenv`` which will skip this step.
  194. But you will need to install all those libraries manually and make
  195. sure they are on your ``PYTHONPATH`` yourself! (You can still use
  196. ``python setup.py develop`` to install some of those libraries,
  197. but note that no ``./bin/python`` will be set up for you via this
  198. method, since no virtualenv is set up for you!)
  199. This concludes the initial configuration of the MediaGoblin
  200. environment. In the future, when you update your
  201. codebase, you should also run::
  202. $ git submodule update && ./bin/python setup.py develop --upgrade && ./bin/gmg dbupdate
  203. .. note::
  204. Note: If you are running an active site, depending on your server
  205. configuration, you may need to stop it first or the dbupdate command
  206. may hang (and it's certainly a good idea to restart it after the
  207. update)
  208. Deploy MediaGoblin Services
  209. ---------------------------
  210. Edit site configuration
  211. ~~~~~~~~~~~~~~~~~~~~~~~
  212. A few basic properties must be set before MediaGoblin will work. First
  213. make a copy of ``mediagoblin.ini`` and ``paste.ini`` for editing so the original
  214. config files aren't lost (you likely won't need to edit the paste configuration,
  215. but we'll make a local copy of it just in case)::
  216. $ cp -av mediagoblin.ini mediagoblin_local.ini && cp -av paste.ini paste_local.ini
  217. Then edit mediagoblin_local.ini:
  218. - Set ``email_sender_address`` to the address you wish to be used as
  219. the sender for system-generated emails
  220. - Edit ``direct_remote_path``, ``base_dir``, and ``base_url`` if
  221. your mediagoblin directory is not the root directory of your
  222. vhost.
  223. Configure MediaGoblin to use the PostgreSQL database
  224. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  225. If you are using postgres, edit the ``[mediagoblin]`` section in your
  226. ``mediagoblin_local.ini`` and put in::
  227. sql_engine = postgresql:///mediagoblin
  228. if you are running the MediaGoblin application as the same 'user' as the
  229. database owner.
  230. Update database data structures
  231. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  232. Before you start using the database, you need to run::
  233. $ ./bin/gmg dbupdate
  234. to populate the database with the MediaGoblin data structures.
  235. Test the Server
  236. ~~~~~~~~~~~~~~~
  237. At this point MediaGoblin should be properly installed. You can
  238. test the deployment with the following command::
  239. $ ./lazyserver.sh --server-name=broadcast
  240. You should be able to connect to the machine on port 6543 in your
  241. browser to confirm that the service is operable.
  242. The next series of commands will need to be run as a priviledged user. Type
  243. exit to return to the root/sudo account.::
  244. exit
  245. .. _webserver-config:
  246. FastCGI and nginx
  247. ~~~~~~~~~~~~~~~~~
  248. This configuration example will use nginx, however, you may
  249. use any webserver of your choice as long as it supports the FastCGI
  250. protocol. If you do not already have a web server, consider nginx, as
  251. the configuration files may be more clear than the
  252. alternatives.
  253. Create a configuration file at
  254. ``/srv/mediagoblin.example.org/nginx.conf`` and create a symbolic link
  255. into a directory that will be included in your ``nginx`` configuration
  256. (e.g. "``/etc/nginx/sites-enabled`` or ``/etc/nginx/conf.d``) with
  257. one of the following commands.
  258. On a DEB-based system (e.g Debian, gNewSense, Trisquel, *buntu, and
  259. derivatives) issue the following commands::
  260. sudo ln -s /srv/mediagoblin.example.org/nginx.conf /etc/nginx/sites-enabled/
  261. sudo systemctl enable nginx
  262. On a RPM-based system (e.g. Fedora, RedHat, and derivatives) issue the
  263. following commands::
  264. sudo ln -s /srv/mediagoblin.example.org/nginx.conf /etc/nginx/conf.d/
  265. sudo systemctl enable nginx
  266. You can modify these commands and locations depending on your preferences and
  267. the existing configuration of your nginx instance. The contents of
  268. this ``nginx.conf`` file should be modeled on the following::
  269. server {
  270. #################################################
  271. # Stock useful config options, but ignore them :)
  272. #################################################
  273. include /etc/nginx/mime.types;
  274. autoindex off;
  275. default_type application/octet-stream;
  276. sendfile on;
  277. # Gzip
  278. gzip on;
  279. gzip_min_length 1024;
  280. gzip_buffers 4 32k;
  281. gzip_types text/plain application/x-javascript text/javascript text/xml text/css;
  282. #####################################
  283. # Mounting MediaGoblin stuff
  284. # This is the section you should read
  285. #####################################
  286. # Change this to update the upload size limit for your users
  287. client_max_body_size 8m;
  288. # prevent attacks (someone uploading a .txt file that the browser
  289. # interprets as an HTML file, etc.)
  290. add_header X-Content-Type-Options nosniff;
  291. server_name mediagoblin.example.org www.mediagoblin.example.org;
  292. access_log /var/log/nginx/mediagoblin.example.access.log;
  293. error_log /var/log/nginx/mediagoblin.example.error.log;
  294. # MediaGoblin's stock static files: CSS, JS, etc.
  295. location /mgoblin_static/ {
  296. alias /srv/mediagoblin.example.org/mediagoblin/mediagoblin/static/;
  297. }
  298. # Instance specific media:
  299. location /mgoblin_media/ {
  300. alias /srv/mediagoblin.example.org/mediagoblin/user_dev/media/public/;
  301. }
  302. # Theme static files (usually symlinked in)
  303. location /theme_static/ {
  304. alias /srv/mediagoblin.example.org/mediagoblin/user_dev/theme_static/;
  305. }
  306. # Plugin static files (usually symlinked in)
  307. location /plugin_static/ {
  308. alias /srv/mediagoblin.example.org/mediagoblin/user_dev/plugin_static/;
  309. }
  310. # Mounting MediaGoblin itself via FastCGI.
  311. location / {
  312. fastcgi_pass 127.0.0.1:26543;
  313. include /etc/nginx/fastcgi_params;
  314. # our understanding vs nginx's handling of script_name vs
  315. # path_info don't match :)
  316. fastcgi_param PATH_INFO $fastcgi_script_name;
  317. fastcgi_param SCRIPT_NAME "";
  318. }
  319. }
  320. The first four ``location`` directives instruct Nginx to serve the
  321. static and uploaded files directly rather than through the MediaGoblin
  322. process. This approach is faster and requires less memory.
  323. .. note::
  324. The user who owns the Nginx process, normally ``www-data`` or ``nginx``,
  325. requires execute permission on the directories ``static``,
  326. ``public``, ``theme_static`` and ``plugin_static`` plus all their
  327. parent directories. This user also requires read permission on all
  328. the files within these directories. This is normally the default.
  329. Nginx is now configured to serve the MediaGoblin application. Perform a quick
  330. test to ensure that this configuration works::
  331. nginx -t
  332. If you encounter any errors, review your nginx configuration files, and try to
  333. resolve them. If you do not encounter any errors, you can start your nginx
  334. server with one of the following commands (depending on your environment)::
  335. sudo /etc/init.d/nginx restart
  336. sudo /etc/rc.d/nginx restart
  337. sudo systemctl restart nginx
  338. Now start MediaGoblin. Use the following command sequence as an
  339. example::
  340. cd /srv/mediagoblin.example.org/mediagoblin/
  341. su mediagoblin -s /bin/bash
  342. ./lazyserver.sh --server-name=fcgi fcgi_host=127.0.0.1 fcgi_port=26543
  343. Visit the site you've set up in your browser by visiting
  344. <http://mediagoblin.example.org>. You should see MediaGoblin!
  345. .. note::
  346. The configuration described above is sufficient for development and
  347. smaller deployments. However, for larger production deployments
  348. with larger processing requirements, see the
  349. ":doc:`production-deployments`" documentation.
  350. Apache
  351. ~~~~~~
  352. Instructions and scripts for running MediaGoblin on an Apache server
  353. can be found on the `MediaGoblin wiki <http://wiki.mediagoblin.org/Deployment>`_.
  354. Should I Keep Open Registration Enabled?
  355. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  356. Unfortunately, in this current release of MediaGoblin we are suffering
  357. from spammers registering to public instances en masse. As such, you
  358. may want to either:
  359. a) Disable registration on your instance and just make
  360. accounts for people you know and trust (eg via the `gmg adduser`
  361. command). You can disable registration in your mediagoblin.ini
  362. like so::
  363. [mediagoblin]
  364. allow_registration = false
  365. b) Enable a captcha plugin. But unfortunately, though some captcha
  366. plugins exist, for various reasons we do not have any general
  367. recommendations we can make at this point.
  368. We hope to have a better solution to this situation shortly. We
  369. apologize for the inconvenience in the meanwhile.
  370. Security Considerations
  371. ~~~~~~~~~~~~~~~~~~~~~~~
  372. .. warning::
  373. The directory ``user_dev/crypto/`` contains some very
  374. sensitive files.
  375. Especially the ``itsdangeroussecret.bin`` is very important
  376. for session security. Make sure not to leak its contents anywhere.
  377. If the contents gets leaked nevertheless, delete your file
  378. and restart the server, so that it creates a new secret key.
  379. All previous sessions will be invalidated.
  380. ..
  381. Local variables:
  382. fill-column: 70
  383. End: