-
Install packages from repositories:
# apt install python-pip python-MySQLdb python-dev libmysqlclient-dev supervisor
-
Install
virtualenv
usingpip
:# pip install virtualenv
Project files will be under folder /srv/users/{username}/private/{project-name}/
-
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
-
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
-
Configure project settings:
-
Copy settings file:
$ cp {project_folder}/settings-dist.py {project_folder}/settings.py
-
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/')
-
Create folder for static files:
$ mkdir static $ chmod 777 static
-
-
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)
-
Configure apache for application:
-
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
-
Create symbolic link for static files:
$ ln -s /srv/users/{username}/private/{project-name}/static /srv/users/{username}/apps/{app-name}/public/static
-
-
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
-
Restart supervisor:
# service supervisor restart
# dpkg-reconfigure tzdata
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?