Installation
============

-  `Basic Setup <#basic-setup>`_
-  `Configuration <#configuration>`_
-  `Deploying <#deploying>`_
-  `Deploying to PythonAnywhere <#deploying-to-pythonanywhere>`_



Basic Setup
-----------

Virtualenv Setup
~~~~~~~~~~~~~~~~

Before you can start, you need to create a `virtualenv`.
You can install the virtualenvwrapper with your package manager or via pip.
Be sure that pip is installed. If you don't know how to install pip, have a
look at their `documentation <http://www.pip-installer.org/en/latest/installing.html>`_.

For example, on archlinux you can install it with
::

    $ sudo pacman -S python2-virtualenvwrapper

or, if you own a Mac, you can install it with
::

    $ sudo pip install virtualenvwrapper

For more information checkout the  `virtualenvwrapper <http://virtualenvwrapper.readthedocs.org/en/latest/install.html#basic-installation>`_ installation.

After that, you can create your virtualenv with
::

    $ mkvirtualenv -a /path/to/flaskbb -p $(which python2) flaskbb

and you should be switched automatically to your newly created virtualenv.
To deactivate it you just have to type ``deactivate`` and if you want to work
on it again, you need to type ``workon flaskbb``.


Required Dependencies
~~~~~~~~~~~~~~~~~~~~~

Now you can install the required dependencies.
::

    $ pip install -r requirements.txt

Alternatively, you can use the `make` command to install the dependencies.
::

    $ make dependencies


Optional Dependencies
~~~~~~~~~~~~~~~~~~~~~~

We have one optional dependency, redis (the python package is installed automatically).
If you want to use it, be sure that a redis-server is running. If you decide
to use redis, the `online guests` and `online users` are being tracked by redis,
else it will only track the `online users` via a simple SQL query.

**On Archlinux**
::

    # Install redis
    $ sudo pacman -S redis

    # Check if redis is already running.
    $ systemctl status redis

    # If not, start it.
    $ sudo systemctl start redis

    # Optional: Start redis everytime you boot your machine
    $ sudo systemctl enable redis

**On Debian 7.0 (Wheezy)**
::

    # Install redis
    $ sudo apt-get install redis-server

    # Check if redis is already running.
    $ service redis-server status

    # If not, start it
    $ sudo service redis-server start

    # Optional: Start redis everytime you boot your machine
    # I can't remember if this is done automatically..
    $ sudo update-rc.d redis-server defaults


Configuration
-------------

Before you can start, you need to configure `FlaskBB`.


Development
~~~~~~~~~~~

For development, you need to copy ``flaskbb/configs/development.py.example`` to
``flaskbb/configs/development.py``.
::

    cp flaskbb/configs/development.py.example flaskbb/configs/development.py

The reCAPTCHA keys should work fine on localhost.


Production
~~~~~~~~~~

If you plan, to use `FlaskBB` in a production environment (not recommended at
the moment, because it's still in development), you need to copy
``flaskbb/configs/production.py.example`` to ``flaskbb/configs/production.py``.
::

    cp flaskbb/configs/production.py.example flaskbb/configs/production.py

Now open ``flaskbb/configs/production.py`` with your favourite editor and adjust
the config variables to your needs.


Mail Examples
~~~~~~~~~~~~~

Both methods are included in the example configs.

**Google Mail**
::

    MAIL_SERVER = "smtp.gmail.com"
    MAIL_PORT = 465
    MAIL_USE_SSL = True
    MAIL_USERNAME = "your_username@gmail.com"
    MAIL_PASSWORD = "your_password"
    MAIL_DEFAULT_SENDER = ("Your Name", "your_username@gmail.com")

**Local SMTP Server**
::

    MAIL_SERVER = "localhost"
    MAIL_PORT = 25
    MAIL_USE_SSL = False
    MAIL_USERNAME = ""
    MAIL_PASSWORD = ""
    MAIL_DEFAULT_SENDER = "noreply@example.org"


Installation
------------

For a guided install, run
::

    $ make install

or
::

    flaskbb install

During the installation process you are asked about your username,
your email address and the password for your administrator user. Using the
`make install` command is recommended as it checks that the dependencies are also
installed.


Upgrading
---------

If the database models changed after a release, you have to run the ``upgrade``
command
::

    flaskbb db upgrade


Deploying
---------

I prefer to use supervisor, uWSGI and nginx to deploy my apps, but if you have
figured out how to deploy it in another way, please let me know, so I
(or you if you create a pull request) can add it to the documentation.

**NOTE:** I have only used Debian to deploy it, if someone is using a other
distribution, could you let me know if that works too? `Also, if you have better
configurations for uWSGI, supervisor or nginx let me know that too.`


Supervisor
~~~~~~~~~~

`Supervisor is a client/server system that allows its users to monitor and
control a number of processes on UNIX-like operating systems.`

To install `supervisor` on Debian, you need to fire up this command:
::

    $ sudo apt-get install supervisor

There are two ways to configure supervisor. The first one is, you just put
the configuration to the end in the ``/etc/supervisor/supervisord.conf`` file.

The second way would be to create a new file in the ``/etc/supervisor/conf.d/``
directory. For example, such a file could be named ``uwsgi.conf``.

After you have choosen the you way you like, simply put the snippet below in the
configuration file.

::

    [program:uwsgi]
    command=/usr/bin/uwsgi --emperor /etc/uwsgi/apps-enabled
    user=apps
    stopsignal=QUIT
    autostart=true
    autorestart=true
    redirect_stderr=true


uWSGI
~~~~~

`uWSGI is a web application solution with batteries included.`

To get started with uWSGI, you need to install it first.
You'll also need the python plugin to serve python apps.
This can be done with:

::

    $ sudo apt-get install uwsgi uwsgi-plugin-python

For the configuration, you need to create a file in the
``/etc/uwsgi/apps-available`` directory. In this example, I will call the
file ``flaskbb.ini``. After that, you can start with configuring it.
My config looks like this for `flaskbb.org` (see below). As you might have noticed, I'm
using a own user for my apps whose home directory is located at `/var/apps/`.
In this directory there are living all my Flask apps.

::

    [uwsgi]
    base = /var/apps/flaskbb
    home = /var/apps/.virtualenvs/flaskbb/
    pythonpath = %(base)
    socket = 127.0.0.1:30002
    module = wsgi
    callable = flaskbb
    uid = apps
    gid = apps
    logto = /var/apps/flaskbb/logs/uwsgi.log
    plugins = python


===============  ==========================  ===============
**base**         /path/to/flaskbb            The folder where your flaskbb application lives
**home**         /path/to/virtualenv/folder  The virtualenv folder for your flaskbb application
**pythonpath**   /path/to/flaskbb            The same as base
**socket**       socket                      This can be either a ip or the path to a socket (don't forget to change that in your nginx config)
**module**       wsgi.py                     This is the file located in the root directory from flaskbb (where manage.py lives).
**callable**     flaskbb                     The callable is application you have created in the ``wsgi.py`` file
**uid**          your_user                   The user who should be used. **NEVER** use root!
**gid**          your_group                  The group who should be used.
**logto**        /path/to/log/file           The path to your uwsgi logfile
**plugins**      python                      We need the python plugin
===============  ==========================  ===============

Don't forget to create a symlink to ``/etc/uwsgi/apps-enabled``.

::

    ln -s /etc/uwsgi/apps-available/flaskbb /etc/uwsgi/apps-enabled/flaskbb


nginx
~~~~~

`nginx [engine x] is an HTTP and reverse proxy server,
as well as a mail proxy server, written by Igor Sysoev.`

The nginx config is pretty straightforward. Again, this is how I use it for
`FlaskBB`. Just copy the snippet below and paste it to, for example
``/etc/nginx/sites-available/flaskbb``.
The only thing left is, that you need to adjust the ``server_name`` to your
domain and the paths in ``access_log``, ``error_log``. Also, don't forget to
adjust the paths in the ``alias`` es, as well as the socket adress in ``uwsgi_pass``.

::

    server {
        listen 80;
        server_name forums.flaskbb.org;

        access_log /var/log/nginx/access.forums.flaskbb.log;
        error_log /var/log/nginx/error.forums.flaskbb.log;

        location / {
            try_files $uri @flaskbb;
        }

        # Static files
        location /static {
           alias /var/apps/flaskbb/flaskbb/static/;
        }

        location ~ ^/_themes/([^/]+)/(.*)$ {
            alias /var/apps/flaskbb/flaskbb/themes/$1/static/$2;
        }

        # robots.txt
        location /robots.txt {
            alias /var/apps/flaskbb/flaskbb/static/robots.txt;
        }

        location @flaskbb {
            uwsgi_pass 127.0.0.1:30002;
            include uwsgi_params;
        }
    }


Like in the `uWSGI <#uwsgi>`_ chapter, don't forget to create a symlink to
``/etc/nginx/sites-enabled/``.


Deploying to PythonAnywhere
---------------------------

`PythonAnywhere <https://www.pythonanywhere.com/>`_ is a platform-as-a-service, which basically means they have a bunch of servers pre-configured with Python, nginx and uWSGI.  You can run a low-traffic website with them for free, so it's an easy way to get quickly FlaskBB running publicly.

Here's what to do:

* Sign up for a PythonAnywhere account at `https://www.pythonanywhere.com/ <https://www.pythonanywhere.com/>`_.
* On the "Consoles" tab, start a Bash console and install/configure FlaskBB like this:
::

    git clone https://github.com/sh4nks/flaskbb.git
    cd flaskbb
    pip3.5 install --user -r requirements.txt
    python3.5 manage.py initdb

* Click the PythonAnywhere logo to go back to the dashboard, then go to the "Web" tab, and click the "Add a new web app" button.
* Just click "Next" on the first page.
* On the next page, click "Flask"
* On the next page, click "Python 3.5"
* On the next page, just accept the default and click next
* Wait while the website is created.
* Click on the "Source code" link, and in the input that appears, replace the `mysite` at the end with `flaskbb`
* Click on the "WSGI configuration file" filename, and wait for an editor to load.
* Change the line that sets `project_home` to replace `mysite` with `flaskbb` again.
* Change the line that says
::

    from flask_app import app as application
 
...to say
::

    from manage import app as application
         
* Click the green "Save" button near the top right.
* Go back to the "Web" tab.
* Click the green "Reload..." button.
* Click the link to visit the site -- you'll have a new FlaskBB install!

