installation.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. Installation
  2. ============
  3. - `Basic Setup <#basic-setup>`_
  4. - `Configuration <#configuration>`_
  5. - `Deploying <#deploying>`_
  6. - `Deploying to PythonAnywhere <#deploying-to-pythonanywhere>`_
  7. Basic Setup
  8. -----------
  9. We recommend installing FlaskBB in an isolated Python environment using `virtualenv`_. In our little guide we will use a wrapper around
  10. virtualenv - the `virtualenvwrapper`_. In addition to virtualenv, we will also
  11. use the package manager `pip`_ to install the dependencies for FlaskBB.
  12. Virtualenv Setup
  13. ~~~~~~~~~~~~~~~~
  14. **Linux:** The easiest way to install `virtualenv`_ and
  15. `virtualenvwrapper`_ is, to use the package manager on your system (if you
  16. are running Linux) to install them.
  17. **Windows:** Take a look at the `flask`_ documentation (then skip ahead to dependencies).
  18. For example, on archlinux you can install them with::
  19. $ sudo pacman -S python-virtualenvwrapper
  20. or, on macOS, you can install them with::
  21. $ sudo pip install virtualenvwrapper
  22. It's sufficient to just install the virtualenvwrapper because it depends on
  23. virtualenv and the package manager will resolve all the dependencies for you.
  24. After that, you can create your virtualenv with::
  25. $ mkvirtualenv -a /path/to/flaskbb -p $(which python) flaskbb
  26. This will create a virtualenv named ``flaskbb`` using the python interpreter in
  27. version 2 and it will set your project directory to ``/path/to/flaskbb``.
  28. This comes handy when typing ``workon flaskbb`` as it will change your
  29. current directory automatically to ``/path/to/flaskbb``.
  30. To deactivate it, you just have to type ``deactivate`` and if you want to work
  31. on it again, just type ``workon flaskbb``.
  32. It is also possible to use ``virtualenv`` without the ``virtualenvwrapper``.
  33. For this you have to use the ``virtualenv`` command and pass the name
  34. of the virtualenv as an argument. In our example, the name of
  35. the virtualenv is ``.venv``.
  36. ::
  37. $ virtualenv .venv
  38. and finally activate it
  39. ::
  40. $ source .venv/bin/activate
  41. If you want to know more about those isolated python environments, checkout
  42. the `virtualenv`_ and `virtualenvwrapper`_ docs.
  43. Dependencies
  44. ~~~~~~~~~~~~
  45. Now that you have set up your environment, you are ready to install the
  46. dependencies.
  47. ::
  48. $ pip install -r requirements.txt
  49. Alternatively, you can use the `make` command to install the dependencies.
  50. ::
  51. $ make dependencies
  52. The development process requires a few extra dependencies which can be
  53. installed with the provided ``requirements-dev.txt`` file.
  54. ::
  55. $ pip install -r requirements-dev.txt
  56. Optional Dependencies
  57. ~~~~~~~~~~~~~~~~~~~~~
  58. We have one optional dependency, redis (the python package is installed
  59. automatically).
  60. If you want to use it, make sure that a redis-server is running.
  61. Redis will be used as the default result and caching backend for
  62. celery (celery is a task queue which FlaskBB uses to send non blocking emails).
  63. The feature for tracking the `online guests` and `online users` do also
  64. require redis (although `online users` works without redis as well).
  65. To install redis, just use your distributions package manager. For Arch Linux
  66. this is `pacman` and for Debian/Ubuntu based systems this is `apt-get`.
  67. ::
  68. # Installing redis using 'pacman':
  69. $ sudo pacman -S redis
  70. # Installing redis using 'apt-get':
  71. $ sudo apt-get install redis-server
  72. # Check if redis is already running.
  73. $ systemctl status redis
  74. # If not, start it.
  75. $ sudo systemctl start redis
  76. # Optional: Lets start redis everytime you boot your machine
  77. $ sudo systemctl enable redis
  78. Configuration
  79. -------------
  80. Production
  81. ~~~~~~~~~~
  82. FlaskBB already sets some sane defaults, so you shouldn't have to change much.
  83. To make this whole process a little bit easier for you, we have created
  84. a little wizard which will ask you some questions and based on the answers that
  85. you provide, it will generate a configuration for you. You can of course
  86. further adjust the generated configuration.
  87. The setup wizard can be started with::
  88. flaskbb makeconfig
  89. To be able to run FlaskBB in production, the only settings
  90. that you need to modify are the following:
  91. - ``SERVER_NAME = "example.org"``
  92. - ``PREFERRED_URL_SCHEME = "https"``
  93. - ``SQLALCHEMY_DATABASE_URI = 'sqlite:///path/to/flaskbb.sqlite'``
  94. - ``SECRET_KEY = "secret key"``
  95. - ``WTF_CSRF_SECRET_KEY = "secret key"``
  96. By default it will try to save the configuration file with the name flaskbb.cfg in FlaskBB’s root folder.
  97. Finally to get going – fire up FlaskBB!
  98. ::
  99. flaskbb --config flaskbb.cfg run
  100. [+] Using config from: /path/to/flaskbb/flaskbb.cfg
  101. * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
  102. Development
  103. ~~~~~~~~~~~
  104. To get started with development you have to generate a development
  105. configuration first. You can use the CLI for this,
  106. as explained in `Configuration <#configuration>`_::
  107. flaskbb makeconfig -d
  108. or::
  109. flaskbb makeconfig --development
  110. Now you can either use ``make`` to run the development server::
  111. make run
  112. or if you like to type a little bit more, the CLI::
  113. flaskbb --config flaskbb.cfg run
  114. You can either pass an import string to the path to the (python) config file you’ve just created, or a default config object. (Most users will follow the example above, which uses the generated file).
  115. This is how you do it by using an import string. Be sure that it is importable from within FlaskBB:
  116. flaskbb --config flaskbb.configs.default.DefaultConfig run
  117. Redis
  118. ~~~~~
  119. If you have decided to use redis as well, which we highly recommend, then
  120. the following services and features can be enabled and configured to use redis.
  121. Before you can start using redis, you have to enable and configure it.
  122. This is quite easy just set ``REDIS_ENABLE`` to ``True`` and adjust the
  123. ``REDIS_URL`` if needed.
  124. ::
  125. REDIS_ENABLED = True
  126. REDIS_URL = "redis://localhost:6379" # or with a password: "redis://:password@localhost:6379"
  127. REDIS_DATABASE = 0
  128. The other services are already configured to use the ``REDIS_URL`` configuration
  129. variable.
  130. **Celery**
  131. ::
  132. CELERY_BROKER_URL = REDIS_URL
  133. CELERY_RESULT_BACKEND = REDIS_URL
  134. **Caching**
  135. ::
  136. CACHE_TYPE = "redis"
  137. CACHE_REDIS_URL = REDIS_URL
  138. **Rate Limiting**
  139. ::
  140. RATELIMIT_ENABLED = True
  141. RATELIMIT_STORAGE_URL = REDIS_URL
  142. Mail Examples
  143. ~~~~~~~~~~~~~
  144. Both methods are included in the example configs.
  145. **Google Mail**
  146. ::
  147. MAIL_SERVER = "smtp.gmail.com"
  148. MAIL_PORT = 465
  149. MAIL_USE_SSL = True
  150. MAIL_USERNAME = "your_username@gmail.com"
  151. MAIL_PASSWORD = "your_password"
  152. MAIL_DEFAULT_SENDER = ("Your Name", "your_username@gmail.com")
  153. **Local SMTP Server**
  154. ::
  155. MAIL_SERVER = "localhost"
  156. MAIL_PORT = 25
  157. MAIL_USE_SSL = False
  158. MAIL_USERNAME = ""
  159. MAIL_PASSWORD = ""
  160. MAIL_DEFAULT_SENDER = "noreply@example.org"
  161. Installation
  162. ------------
  163. **MySQL users:** Make sure that you create the database using the
  164. ``utf8`` charset::
  165. CREATE DATABASE flaskbb CHARACTER SET utf8;
  166. Even though the ``utf8mb4`` charset is prefered today
  167. (see `this <https://dba.stackexchange.com/a/152383>`_ SO answer), we have to
  168. create our database using the ``utf8`` charset. A good explanation about this
  169. issue can be found `here <https://stackoverflow.com/a/31474509>`_.
  170. For a guided install, run::
  171. $ make install
  172. or::
  173. flaskbb install
  174. During the installation process, you will be asked to provide a username, email adddress
  175. and password for your administrator user. Using the
  176. ``make install`` command is recommended as it checks that the dependencies
  177. are also installed.
  178. Upgrading
  179. ---------
  180. If the database models changed after a release, you have to run the ``upgrade``
  181. command::
  182. flaskbb db upgrade
  183. Deploying
  184. ---------
  185. This chapter will describe how to set up Supervisor + uWSGI + nginx for
  186. FlaskBB as well as document how to use the built-in WSGI server (gunicorn)
  187. that can be used in a production environment.
  188. Supervisor
  189. ~~~~~~~~~~
  190. `Supervisor is a client/server system that allows its users to monitor and
  191. control a number of processes on UNIX-like operating systems.`
  192. To install `supervisor` on Debian, you need to fire up this command:
  193. ::
  194. $ sudo apt-get install supervisor
  195. There are two ways to configure supervisor. The first one is, you just put
  196. the configuration to the end in the ``/etc/supervisor/supervisord.conf`` file.
  197. The second way would be to create a new file in the ``/etc/supervisor/conf.d/``
  198. directory. For example, such a file could be named ``uwsgi.conf``.
  199. After you have chosen the way you like, simply put the snippet below in the
  200. configuration file.
  201. ::
  202. [program:uwsgi]
  203. command=/usr/bin/uwsgi --emperor /etc/uwsgi/apps-enabled
  204. user=apps
  205. stopsignal=QUIT
  206. autostart=true
  207. autorestart=true
  208. redirect_stderr=true
  209. uWSGI
  210. ~~~~~
  211. `uWSGI is a web application solution with batteries included.`
  212. To get started with uWSGI, you need to install it first.
  213. You'll also need the python plugin to serve python apps.
  214. This can be done with::
  215. $ sudo apt-get install uwsgi uwsgi-plugin-python
  216. For the configuration, you need to create a file in the
  217. ``/etc/uwsgi/apps-available`` directory. In this example, I will call the
  218. file ``flaskbb.ini``. After that, you can start with configuring it.
  219. My config looks like this for `flaskbb.org` (see below). As you might have noticed, I'm
  220. using a own user for my apps whose home directory is located at `/var/apps/`.
  221. All my flask apps live in this directory.
  222. ::
  223. [uwsgi]
  224. base = /var/apps/flaskbb
  225. home = /var/apps/.virtualenvs/flaskbb/
  226. pythonpath = %(base)
  227. socket = 127.0.0.1:30002
  228. module = wsgi
  229. callable = flaskbb
  230. uid = apps
  231. gid = apps
  232. logto = /var/apps/flaskbb/logs/uwsgi.log
  233. plugins = python
  234. =============== ========================== ===============
  235. **base** /path/to/flaskbb The folder where your flaskbb application lives
  236. **home** /path/to/virtualenv/folder The virtualenv folder for your flaskbb application
  237. **pythonpath** /path/to/flaskbb The same as base
  238. **socket** socket This can be either a ip or the path to a socket (don't forget to change that in your nginx config)
  239. **module** wsgi.py This is the file located in the root directory from flaskbb (where manage.py lives).
  240. **callable** flaskbb The callable is application you have created in the ``wsgi.py`` file
  241. **uid** your_user The user who should be used. **NEVER** use root!
  242. **gid** your_group The group who should be used.
  243. **logto** /path/to/log/file The path to your uwsgi logfile
  244. **plugins** python We need the python plugin
  245. =============== ========================== ===============
  246. Don't forget to create a symlink to ``/etc/uwsgi/apps-enabled``.
  247. ::
  248. ln -s /etc/uwsgi/apps-available/flaskbb /etc/uwsgi/apps-enabled/flaskbb
  249. gunicorn
  250. ~~~~~~~~
  251. `Gunicorn 'Green Unicorn' is a Python WSGI HTTP Server for UNIX.`
  252. It's a pre-fork worker model ported from Ruby's Unicorn project.
  253. The Gunicorn server is broadly compatible with various web frameworks,
  254. simply implemented, light on server resources, and fairly speedy.
  255. This is probably the easiest way to run a FlaskBB instance.
  256. Just install gunicorn via pip inside your virtualenv::
  257. pip install gunicorn
  258. and run FlaskBB using the ``gunicorn`` command::
  259. gunicorn wsgi:flaskbb --log-file logs/gunicorn.log --pid gunicorn.pid -w 4
  260. nginx
  261. ~~~~~
  262. `nginx [engine x] is an HTTP and reverse proxy server,
  263. as well as a mail proxy server, written by Igor Sysoev.`
  264. The nginx config is pretty straightforward. Again, this is how I use it for
  265. `FlaskBB`. Just copy the snippet below and paste it to, for example
  266. ``/etc/nginx/sites-available/flaskbb``.
  267. The only thing left is, that you need to adjust the ``server_name`` to your
  268. domain and the paths in ``access_log``, ``error_log``. Also, don't forget to
  269. adjust the paths in the ``alias`` es, as well as the socket address in ``uwsgi_pass``.
  270. ::
  271. server {
  272. listen 80;
  273. server_name forums.flaskbb.org;
  274. access_log /var/log/nginx/access.forums.flaskbb.log;
  275. error_log /var/log/nginx/error.forums.flaskbb.log;
  276. location / {
  277. try_files $uri @flaskbb;
  278. }
  279. # Static files
  280. location /static {
  281. alias /var/apps/flaskbb/flaskbb/static/;
  282. }
  283. location ~ ^/_themes/([^/]+)/(.*)$ {
  284. alias /var/apps/flaskbb/flaskbb/themes/$1/static/$2;
  285. }
  286. # robots.txt
  287. location /robots.txt {
  288. alias /var/apps/flaskbb/flaskbb/static/robots.txt;
  289. }
  290. location @flaskbb {
  291. uwsgi_pass 127.0.0.1:30002;
  292. include uwsgi_params;
  293. }
  294. }
  295. If you wish to use gunicorn instead of uwsgi just replace the ``location @flaskbb``
  296. with this::
  297. location @flaskbb {
  298. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  299. proxy_set_header X-Forwarded-Proto $scheme;
  300. proxy_set_header Host $http_host;
  301. #proxy_set_header SCRIPT_NAME /forums; # This line will make flaskbb available on /forums;
  302. proxy_redirect off;
  303. proxy_buffering off;
  304. proxy_pass http://127.0.0.1:8000;
  305. }
  306. Don't forget to adjust the ``proxy_pass`` address to your socket address.
  307. Like in the `uWSGI <#uwsgi>`_ chapter, don't forget to create a symlink to
  308. ``/etc/nginx/sites-enabled/``.
  309. User Contributed Deployment Guides
  310. ----------------------------------
  311. We do not maintain these deployment guides. They have been submitted by users
  312. and we thought it is nice to include them in docs. If something is missing,
  313. or doesn't work - please open a new pull request on GitHub.
  314. Deploying to PythonAnywhere
  315. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  316. `PythonAnywhere <https://www.pythonanywhere.com/>`_ is a
  317. platform-as-a-service, which basically means they have a bunch of servers
  318. pre-configured with Python, nginx and uWSGI.
  319. You can run a low-traffic website with them for free,
  320. so it's an easy way to get quickly FlaskBB running publicly.
  321. Here's what to do:
  322. * Sign up for a PythonAnywhere account at
  323. `https://www.pythonanywhere.com/ <https://www.pythonanywhere.com/>`_.
  324. * On the "Consoles" tab, start a Bash console and install/configure
  325. FlaskBB like this
  326. ::
  327. git clone https://github.com/sh4nks/flaskbb.git
  328. cd flaskbb
  329. Before continuing the installation it is advised to create a virtualenv as is
  330. described in section `Virtualenv Setup <#virtualenv-setup>`_.
  331. Finish the installation of FlaskBB by executing following commands::
  332. pip3.5 install --user -r requirements.txt
  333. pip3.5 install --user -e .
  334. flaskbb makeconfig
  335. flaskbb install
  336. * Click the PythonAnywhere logo to go back to the dashboard,
  337. then go to the "Web" tab, and click the "Add a new web app" button.
  338. * Just click "Next" on the first page.
  339. * On the next page, click "Flask"
  340. * On the next page, click "Python 3.5"
  341. * On the next page, just accept the default and click next
  342. * Wait while the website is created.
  343. * Click on the "Source code" link, and in the input that appears,
  344. replace the `mysite` at the end with `flaskbb`
  345. * Click on the "WSGI configuration file" filename,
  346. and wait for an editor to load.
  347. * Change the line that sets `project_home` to replace `mysite` with `flaskbb`
  348. again.
  349. * Change the line that says
  350. ::
  351. from flask_app import app as application
  352. to say
  353. ::
  354. from flaskbb import create_app
  355. application = create_app("/path/to/your/configuration/file")
  356. * Click the green "Save" button near the top right.
  357. * Go back to the "Web" tab.
  358. * Click the green "Reload..." button.
  359. * Click the link to visit the site -- you'll have a new FlaskBB install!
  360. .. _virtualenv: https://virtualenv.pypa.io/en/latest/installation.html
  361. .. _virtualenvwrapper: http://virtualenvwrapper.readthedocs.org/en/latest/install.html#basic-installation
  362. .. _pip: http://www.pip-installer.org/en/latest/installing.html
  363. .. _flask: http://flask.pocoo.org/docs/0.12/installation/