<commit_before>.. _webservers:

###########
Web servers
###########

.. _apache2:

*******
Apache2
*******

First, make sure that both *apache2* and *mod_python* are installed on
your server.

Create a new virtualhost in your Apache configuration and put the
following inside::

  <VirtualHost *:80>
    <Location "/">
      SetHandler mod_python
      PythonHandler django.core.handlers.modpython
      PythonPath "['<path to directory that contains modoboa dir>'] + sys.path"
      SetEnv DJANGO_SETTINGS_MODULE modoboa.settings
    </Location>
    Alias "/sitestatic" "<path to modoboa dir>/sitestatic"
    <Location "/sitestatic/">
      SetHandler None
    </Location>
    Alias "/media" "<path to modoboa dir>/media"
    <Location "/media/">
      SetHandler None
    </Location>
  </VirtualHost>

This is one possible configuration. Note that you will certainly need more
configuration in order to launch Apache.

.. _nginx-label:

*****
Nginx
*****

`Nginx <http://nginx.org/>`_ is a really fast HTTP server. Associated
with `Green Unicorn <http://gunicorn.org/>`_, it gives you one of the
best setup to serve python/Django applications. Modoboa's
performances are really good with this configuration.

To use this setup, first download and install those softwares
(`Install gunicorn <http://gunicorn.org/install.html>`_ and `install
nginx <http://wiki.nginx.org/Install>`_).

Then, use the following sample *gunicorn* configuration (create a new
file named *gunicorn.conf.py* inside Modoboa's root dir)::

  backlog = 2048
  bind = "unix:/var/run/gunicorn/modoboa.sock"
  pidfile = "/var/run/gunicorn/modoboa.pid"
  daemon = True
  debug = False
  workers = 2
  logfile = "/var/log/gunicorn/modoboa.log"
  loglevel = "info"

To start gunicorn, execute the following commands::

  $ cd <modoboa dir>
  $ gunicorn_django -c gunicorn.conf.py

Now the *nginx* part. Just create a new virtual host and use the
following configuration::

  upstream modoboa {
	server      unix:/var/run/gunicorn/modoboa.sock fail_timeout=0;
  }

  server {
        listen 443;
        server_name <host fqdn>;
        root <modoboa's root dir>;

        access_log  /var/log/nginx/<host fqdn>.access.log;
        error_log /var/log/nginx/<host fqdn>.error.log;

        location /sitestatic/ {
                autoindex on;
        }

        location /media/ {
                autoindex on;
        }

        location / {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                proxy_set_header X-Forwarded-Protocol ssl;

                if (!-f $request_filename) {
			proxy_pass http://modoboa;
                        break;
                }
        }
  }

Paste this content to your configuration (replace values between
``<>`` with yours), restart *nginx* and enjoy a really fast
application!<commit_msg>Fixed security issue with nginx configuration.<commit_after>.. _webservers:

###########
Web servers
###########

.. _apache2:

*******
Apache2
*******

First, make sure that both *apache2* and *mod_python* are installed on
your server.

Create a new virtualhost in your Apache configuration and put the
following inside::

  <VirtualHost *:80>
    <Location "/">
      SetHandler mod_python
      PythonHandler django.core.handlers.modpython
      PythonPath "['<path to directory that contains modoboa dir>'] + sys.path"
      SetEnv DJANGO_SETTINGS_MODULE modoboa.settings
    </Location>
    Alias "/sitestatic" "<path to modoboa dir>/sitestatic"
    <Location "/sitestatic/">
      SetHandler None
    </Location>
    Alias "/media" "<path to modoboa dir>/media"
    <Location "/media/">
      SetHandler None
    </Location>
  </VirtualHost>

This is one possible configuration. Note that you will certainly need more
configuration in order to launch Apache.

.. _nginx-label:

*****
Nginx
*****

`Nginx <http://nginx.org/>`_ is a really fast HTTP server. Associated
with `Green Unicorn <http://gunicorn.org/>`_, it gives you one of the
best setup to serve python/Django applications. Modoboa's
performances are really good with this configuration.

To use this setup, first download and install those softwares
(`Install gunicorn <http://gunicorn.org/install.html>`_ and `install
nginx <http://wiki.nginx.org/Install>`_).

Then, use the following sample *gunicorn* configuration (create a new
file named *gunicorn.conf.py* inside Modoboa's root dir)::

  backlog = 2048
  bind = "unix:/var/run/gunicorn/modoboa.sock"
  pidfile = "/var/run/gunicorn/modoboa.pid"
  daemon = True
  debug = False
  workers = 2
  logfile = "/var/log/gunicorn/modoboa.log"
  loglevel = "info"

To start gunicorn, execute the following commands::

  $ cd <modoboa dir>
  $ gunicorn_django -c gunicorn.conf.py

Now the *nginx* part. Just create a new virtual host and use the
following configuration::

  upstream modoboa {
	server      unix:/var/run/gunicorn/modoboa.sock fail_timeout=0;
  }

  server {
        listen 443;
        server_name <host fqdn>;
        root <modoboa's root dir>;

        access_log  /var/log/nginx/<host fqdn>.access.log;
        error_log /var/log/nginx/<host fqdn>.error.log;

        location /sitestatic/ {
                autoindex on;
        }

        location /media/ {
                autoindex on;
        }

        location / {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                proxy_set_header X-Forwarded-Protocol ssl;
		proxy_pass http://modoboa;
        }
  }

Paste this content to your configuration (replace values between
``<>`` with yours), restart *nginx* and enjoy a really fast
application!
{'fraction_non_alphanumeric': 0.11091884409067312, 'fraction_numerical': 0.004845128915037204, 'mean_word_length': 2.848202396804261, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4799833', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1741, 'n_words': 544}