Skip to content

Instantly share code, notes, and snippets.

@rubenrubiob
Last active January 29, 2020 14:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rubenrubiob/f9f8d42d79be3f3ab1791c7a11482149 to your computer and use it in GitHub Desktop.
Save rubenrubiob/f9f8d42d79be3f3ab1791c7a11482149 to your computer and use it in GitHub Desktop.
Guide to serve Django apps in a Serverpilot server

Configure Django project in a Serverpilot server

Install dependencies (as root)

  1. Install packages from repositories:

    # apt install python-pip python-MySQLdb python-dev libmysqlclient-dev supervisor
  2. Install virtualenv using pip:

    # pip install virtualenv

Install project

Project files will be under folder /srv/users/{username}/private/{project-name}/

  1. Clone project:

    $ cd /srv/users/{username}/private/  # Create private folder if it does not exist
    $ git clone {clone-url} {project-name}  # Clone project source code
  2. Install project dependencies: within project folder, execute:

    $ virtualenv {project-name}-env  # Create virtualenv
    $ source {project-name}-env/bin/activate  # Activate virtualenv
    $ pip install django MySQL-python gunicorn  # Install project dependencies
  3. Configure project settings:

    1. Copy settings file:

      $ cp {project_folder}/settings-dist.py {project_folder}/settings.py
    2. Ensure that the following pieces of code are configured within the settings.py file:

      SECRET_KEY = 'your-secret-key-here'
      …
      DEBUG = False
      ALLOWED_HOSTS = ['*']
      …
      DATABASES = {
          'default': {
              'ENGINE': 'django.db.backends.mysql',
              'NAME': 'db-name',
              'USER': 'db-user',
              'PASSWORD': 'db-password',
              'HOST': '127.0.0.1',
          }
      }
      …
      EMAIL_HOST = 'host'  # smtp.gmail.com
      EMAIL_HOST_USER = 'user'  # user@gmail.com
      EMAIL_HOST_PASSWORD = 'password'
      EMAIL_PORT = 587
      EMAIL_USE_TLS = True
      …
      STATIC_URL = '/static/'
      STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
      
    3. Create folder for static files:

      $ mkdir static
      $ chmod 777 static	
  4. Execute Django commands:

    $ python manage.py migrate  # Execute database migration
    $ python manage.py collectstatic  # Copy static files to its folder
    $ python manage.py createsuperuser  # Create superuser (if needed)
  5. Configure apache for application:

    1. Create file /srv/users/{username}/apps/{app-name}/public/.htaccess and add the following content:

      Options +FollowSymLinks
      
      RewriteCond %{REQUEST_URI} ^/static/(.*)
      RewriteRule . - [L]
      
      RewriteRule index.html http://127.0.0.1:{port}/ [P]
      RewriteRule (.*) http://127.0.0.1:{port}/$1 [P]  # Port will be used for gunicorn
    2. Create symbolic link for static files:

      $ ln -s /srv/users/{username}/private/{project-name}/static /srv/users/{username}/apps/{app-name}/public/static

Configure gunicorn (as root)

  1. Create file /etc/supervisor/conf.d/{project-name} and add the following content:

    [program:gunicorn:{project-name}]
    command=/srv/users/{username}/private/{project-name}/{project-name}-env/bin/gunicorn -b 127.0.0.1:{port} {project_folder}.wsgi
    directory=/srv/users/{username}/private/{project-name}/
    user={username}
    autostart=true
    autorestart=true
    redirect_stderr=true
    
  2. Restart supervisor:

    # service supervisor restart

Extra

Configure server time

# dpkg-reconfigure tzdata
@rehmatworks
Copy link

In instructions above, you have asked to create project-name-env directory in private directory and in gunicorn config, you are referencing /srv/users/{username}/private/{project-name}/{project-name}-env/, is that okay or there is a typo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment