a892bbd42fdc79372d2676736c542966a5decdae
[mediagoblin.git] / docs / source / siteadmin / deploying.rst
1 .. MediaGoblin Documentation
2
3 Written in 2011, 2012, 2013 by MediaGoblin contributors
4
5 To the extent possible under law, the author(s) have dedicated all
6 copyright and related and neighboring rights to this software to
7 the public domain worldwide. This software is distributed without
8 any warranty.
9
10 You should have received a copy of the CC0 Public Domain
11 Dedication along with this software. If not, see
12 <http://creativecommons.org/publicdomain/zero/1.0/>.
13
14 .. _deploying-chapter:
15
16 =====================
17 Deploying MediaGoblin
18 =====================
19
20 GNU MediaGoblin is fairly new and so at the time of writing, there
21 aren't easy package-manager-friendly methods to install MediaGoblin.
22 However, doing a basic install isn't too complex in and of itself.
23
24 There's an almost infinite way to deploy things... for now, we'll keep
25 it simple with some assumptions and use a setup that combines
26 mediagoblin + virtualenv + fastcgi + nginx on a .deb or .rpm based
27 GNU/Linux distro.
28
29 .. note::
30
31 These tools are for site administrators wanting to deploy a fresh
32 install. If instead you want to join in as a contributor, see our
33 `Hacking HOWTO <http://wiki.mediagoblin.org/HackingHowto>`_ instead.
34
35 There are also many ways to install servers... for the sake of
36 simplicity, our instructions below describe installing with nginx.
37 For more recipes, including Apache, see
38 `our wiki <http://wiki.mediagoblin.org/Deployment>`_.
39
40 Prepare System
41 --------------
42
43 Dependencies
44 ~~~~~~~~~~~~
45
46 MediaGoblin has the following core dependencies:
47
48 - Python 2.6 or 2.7
49 - `python-lxml <http://lxml.de/>`_
50 - `git <http://git-scm.com/>`_
51 - `SQLite <http://www.sqlite.org/>`_/`PostgreSQL <http://www.postgresql.org/>`_
52 - `Python Imaging Library <http://www.pythonware.com/products/pil/>`_ (PIL)
53 - `virtualenv <http://www.virtualenv.org/>`_
54
55 On a DEB-based system (e.g Debian, gNewSense, Trisquel, Ubuntu, and
56 derivatives) issue the following command::
57
58 sudo apt-get install git-core python python-dev python-lxml \
59 python-imaging python-virtualenv
60
61 On a RPM-based system (e.g. Fedora, RedHat, and derivatives) issue the
62 following command::
63
64 yum install python-paste-deploy python-paste-script \
65 git-core python python-devel python-lxml python-imaging \
66 python-virtualenv
67
68 Configure PostgreSQL
69 ~~~~~~~~~~~~~~~~~~~~
70
71 .. note::
72
73 MediaGoblin currently supports PostgreSQL and SQLite. The default is a
74 local SQLite database. This will "just work" for small deployments.
75
76 For medium to large deployments we recommend PostgreSQL.
77
78 If you don't want/need postgres, skip this section.
79
80 These are the packages needed for Debian Wheezy (stable)::
81
82 sudo apt-get install postgresql postgresql-client python-psycopg2
83
84 The installation process will create a new *system* user named ``postgres``,
85 it will have privilegies sufficient to manage the database. We will create a
86 new database user with restricted privilegies and a new database owned by our
87 restricted database user for our MediaGoblin instance.
88
89 In this example, the database user will be ``mediagoblin`` and the database
90 name will be ``mediagoblin`` too.
91
92 To create our new user, run::
93
94 sudo -u postgres createuser mediagoblin
95
96 then answer NO to *all* the questions::
97
98 Shall the new role be a superuser? (y/n) n
99 Shall the new role be allowed to create databases? (y/n) n
100 Shall the new role be allowed to create more new roles? (y/n) n
101
102 then create the database all our MediaGoblin data should be stored in::
103
104 sudo -u postgres createdb -E UNICODE -O mediagoblin mediagoblin
105
106 where the first ``mediagoblin`` is the database owner and the second
107 ``mediagoblin`` is the database name.
108
109 .. caution:: Where is the password?
110
111 These steps enable you to authenticate to the database in a password-less
112 manner via local UNIX authentication provided you run the MediaGoblin
113 application as a user with the same name as the user you created in
114 PostgreSQL.
115
116 More on this in :ref:`Drop Privileges for MediaGoblin <drop-privileges-for-mediagoblin>`.
117
118
119 .. _drop-privileges-for-mediagoblin:
120
121 Drop Privileges for MediaGoblin
122 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123
124 MediaGoblin does not require special permissions or elevated
125 access to run. As such, the prefered way to run MediaGoblin is to
126 create a dedicated, unpriviledged system user for sole the purpose of running
127 MediaGoblin. Running MediaGoblin processes under an unpriviledged system user
128 helps to keep it more secure.
129
130 The following command (entered as root or with sudo) will create a
131 system account with a username of ``mediagoblin``. You may choose a different
132 username if you wish.::
133
134 ``# adduser --system mediagoblin``
135
136 No password will be assigned to this account, and you will not be able
137 to log in as this user. To switch to this account, enter either::
138
139 ``sudo su - mediagoblin`` (if you have sudo permissions)
140
141 or
142
143 ``su - mediagoblin`` (if you have to use root permissions)
144
145 You may get a warning similar to this when entering these commands::
146
147 ``warning: cannot change directory to /home/mediagoblin: No such file or directory``
148
149 You can disregard this warning. To return to your regular user account after
150 using the system account, just enter ``exit``.
151
152 .. note::
153
154 Unless otherwise noted, the remainder of this document assumes that all
155 operations are performed using this unpriviledged account.
156
157 .. _create-mediagoblin-directory:
158
159 Create a MediaGoblin Directory
160 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
161
162 You should create a working directory for MediaGoblin. This document
163 assumes your local git repository will be located at
164 ``/srv/mediagoblin.example.org/mediagoblin/``.
165 Substitute your prefered local deployment path as needed.
166
167 Setting up the working directory requires that we first create the directory
168 with elevated priviledges, and then assign ownership of the directory
169 to the unpriviledged system account.
170
171 To do this, enter either of the following commands, changing the defaults
172 to suit your particular requirements::
173
174 ``sudo mkdir -p /srv/mediagoblin.example.org && sudo chown -hR mediagoblin:mediagoblin /srv/mediagobin.example.org``
175
176 or (as the root user)
177
178 ``# mkdir -p /srv/mediagoblin.example.org && chown -hR mediagoblin:mediagoblin /srv/mediagobin.example.org``
179
180
181 Install MediaGoblin and Virtualenv
182 ----------------------------------
183
184 .. note::
185
186 MediaGoblin is still developing rapidly. As a result
187 the following instructions recommend installing from the ``master``
188 branch of the git repository. Eventually production deployments will
189 want to transition to running from more consistent releases.
190
191 We will now clone the MediaGoblin source code repository and setup and
192 configure the necessary services. Modify these commands to
193 suit your own environment. As a reminder, you should enter these
194 commands using your unpriviledged system account.
195
196 Change to the MediaGoblin directory that you just created::
197
198 cd /srv/mediagoblin.example.org
199
200 Clone the MediaGoblin repository and set up the git submodules::
201
202 git clone git://gitorious.org/mediagoblin/mediagoblin.git
203 cd mediagoblin
204 git submodule init && git submodule update
205
206 Set up the in-package virtualenv::
207
208 (virtualenv --system-site-packages . || virtualenv .) && ./bin/python setup.py develop
209
210 .. note::
211
212 If you have problems here, consider trying to install virtualenv
213 with the ``--distribute`` or ``--no-site-packages`` options. If
214 your system's default Python is in the 3.x series you may need to
215 run ``virtualenv`` with the ``--python=python2.7`` or
216 ``--python=python2.6`` options.
217
218 The above provides an in-package install of ``virtualenv``. While this
219 is counter to the conventional ``virtualenv`` configuration, it is
220 more reliable and considerably easier to configure and illustrate. If
221 you're familiar with Python packaging you may consider deploying with
222 your preferred method.
223
224 Assuming you are going to deploy with FastCGI, you should also install
225 flup::
226
227 ./bin/easy_install flup
228
229 (Sometimes this breaks because flup's site is flakey. If it does for
230 you, try)::
231
232 ./bin/easy_install https://pypi.python.org/pypi/flup/1.0.3.dev-20110405
233
234 This concludes the initial configuration of the development
235 environment. In the future, when you update your
236 codebase, you should also run::
237
238 ./bin/python setup.py develop --upgrade && ./bin/gmg dbupdate && git submodule fetch
239
240 Note: If you are running an active site, depending on your server
241 configuration, you may need to stop it first or the dbupdate command
242 may hang (and it's certainly a good idea to restart it after the
243 update)
244
245
246 Deploy MediaGoblin Services
247 ---------------------------
248
249 Edit site configuration
250 ~~~~~~~~~~~~~~~~~~~~~~~
251
252 A few basic properties must be set before MediaGoblin will work. First
253 make a copy of ``mediagoblin.ini`` for editing so the original config
254 file isn't lost::
255
256 cp mediagoblin.ini mediagoblin_local.ini
257
258 Then:
259 - Set ``email_sender_address`` to the address you wish to be used as
260 the sender for system-generated emails
261 - Edit ``direct_remote_path``, ``base_dir``, and ``base_url`` if
262 your mediagoblin directory is not the root directory of your
263 vhost.
264
265
266 Configure MediaGoblin to use the PostgreSQL database
267 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
268
269 If you are using postgres, edit the ``[mediagoblin]`` section in your
270 ``mediagoblin_local.ini`` and put in::
271
272 sql_engine = postgresql:///mediagoblin
273
274 if you are running the MediaGoblin application as the same 'user' as the
275 database owner.
276
277
278 Update database data structures
279 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
280
281 Before you start using the database, you need to run::
282
283 ./bin/gmg dbupdate
284
285 to populate the database with the MediaGoblin data structures.
286
287
288 Test the Server
289 ~~~~~~~~~~~~~~~
290
291 At this point MediaGoblin should be properly installed. You can
292 test the deployment with the following command::
293
294 ./lazyserver.sh --server-name=broadcast
295
296 You should be able to connect to the machine on port 6543 in your
297 browser to confirm that the service is operable.
298
299 .. _webserver-config:
300
301
302 FastCGI and nginx
303 ~~~~~~~~~~~~~~~~~
304
305 This configuration example will use nginx, however, you may
306 use any webserver of your choice as long as it supports the FastCGI
307 protocol. If you do not already have a web server, consider nginx, as
308 the configuration files may be more clear than the
309 alternatives.
310
311 Create a configuration file at
312 ``/srv/mediagoblin.example.org/nginx.conf`` and create a symbolic link
313 into a directory that will be included in your ``nginx`` configuration
314 (e.g. "``/etc/nginx/sites-enabled`` or ``/etc/nginx/conf.d``) with
315 one of the following commands (as the root user)::
316
317 ln -s /srv/mediagoblin.example.org/nginx.conf /etc/nginx/conf.d/
318 ln -s /srv/mediagoblin.example.org/nginx.conf /etc/nginx/sites-enabled/
319
320 Modify these commands and locations depending on your preferences and
321 the existing configuration of your nginx instance. The contents of
322 this ``nginx.conf`` file should be modeled on the following::
323
324 server {
325 #################################################
326 # Stock useful config options, but ignore them :)
327 #################################################
328 include /etc/nginx/mime.types;
329
330 autoindex off;
331 default_type application/octet-stream;
332 sendfile on;
333
334 # Gzip
335 gzip on;
336 gzip_min_length 1024;
337 gzip_buffers 4 32k;
338 gzip_types text/plain text/html application/x-javascript text/javascript text/xml text/css;
339
340 #####################################
341 # Mounting MediaGoblin stuff
342 # This is the section you should read
343 #####################################
344
345 # Change this to update the upload size limit for your users
346 client_max_body_size 8m;
347
348 # prevent attacks (someone uploading a .txt file that the browser
349 # interprets as an HTML file, etc.)
350 add_header X-Content-Type-Options nosniff;
351
352 server_name mediagoblin.example.org www.mediagoblin.example.org;
353 access_log /var/log/nginx/mediagoblin.example.access.log;
354 error_log /var/log/nginx/mediagoblin.example.error.log;
355
356 # MediaGoblin's stock static files: CSS, JS, etc.
357 location /mgoblin_static/ {
358 alias /srv/mediagoblin.example.org/mediagoblin/mediagoblin/static/;
359 }
360
361 # Instance specific media:
362 location /mgoblin_media/ {
363 alias /srv/mediagoblin.example.org/mediagoblin/user_dev/media/public/;
364 }
365
366 # Theme static files (usually symlinked in)
367 location /theme_static/ {
368 alias /srv/mediagoblin.example.org/mediagoblin/user_dev/theme_static/;
369 }
370
371 # Plugin static files (usually symlinked in)
372 location /plugin_static/ {
373 alias /srv/mediagoblin.example.org/mediagoblin/user_dev/plugin_static/;
374 }
375
376 # Mounting MediaGoblin itself via FastCGI.
377 location / {
378 fastcgi_pass 127.0.0.1:26543;
379 include /etc/nginx/fastcgi_params;
380
381 # our understanding vs nginx's handling of script_name vs
382 # path_info don't match :)
383 fastcgi_param PATH_INFO $fastcgi_script_name;
384 fastcgi_param SCRIPT_NAME "";
385 }
386 }
387
388 Now, nginx instance is configured to serve the MediaGoblin
389 application. Perform a quick test to ensure that this configuration
390 works. Restart nginx so it picks up your changes, with a command that
391 resembles one of the following (as the root user)::
392
393 sudo /etc/init.d/nginx restart
394 sudo /etc/rc.d/nginx restart
395
396 Now start MediaGoblin. Use the following command sequence as an
397 example::
398
399 cd /srv/mediagoblin.example.org/mediagoblin/
400 ./lazyserver.sh --server-name=fcgi fcgi_host=127.0.0.1 fcgi_port=26543
401
402 Visit the site you've set up in your browser by visiting
403 <http://mediagoblin.example.org>. You should see MediaGoblin!
404
405 .. note::
406
407 The configuration described above is sufficient for development and
408 smaller deployments. However, for larger production deployments
409 with larger processing requirements, see the
410 ":doc:`production-deployments`" documentation.
411
412
413 Apache
414 ~~~~~~
415
416 Instructions and scripts for running MediaGoblin on an Apache server
417 can be found on the `MediaGoblin wiki <http://wiki.mediagoblin.org/Deployment>`_.
418
419
420 Security Considerations
421 ~~~~~~~~~~~~~~~~~~~~~~~
422
423 .. warning::
424
425 The directory ``user_dev/crypto/`` contains some very
426 sensitive files.
427 Especially the ``itsdangeroussecret.bin`` is very important
428 for session security. Make sure not to leak its contents anywhere.
429 If the contents gets leaked nevertheless, delete your file
430 and restart the server, so that it creates a new secret key.
431 All previous sessions will be invalidated.
432