Open file /etc/default/locale to add or change LC_ALL to the following
LC_ALL="en_US.UTF-8"
Then logout and login again. (I've tried to find a better way to do this but failed. If you can, please tell me.)
Update aptitude
$ apt-get update
$ apt-get upgrade
Install necessary packages
$ apt-get install build-essential
Install Python
$ apt-get install python2.7-dev python-setuptools
$ easy_install pip
Install git
$ apt-get install git-core
Generate a new SSH key
$ ssh-keygen -t rsa -C "[email protected]"
Then add the key from ~/.ssh/id_rsa.pub to your git provider website as a deployment key
Install virtualenv
$ pip install virtualenv
- I don't like using virtualenvwrapper in production server because I want to isolate everything for the web under one directory (in this case, all Python libraries).
Install Postgresql Server and Python library
$ apt-get install postgresql python-psycopg2 libpq-dev
Create a new database and database's user
$ sudo -u postgres createdb [database_name]
$ sudo -u postgres createuser -SDRP [database_user_name]
Config pg_hba.conf file (you may need to change postgresql version number)
$ vim /etc/postgresql/9.5/main/pg_hba.conf
Then add the following to pg_hba.conf file let database user authenticated using password
local [database_name] [database_user_name] md5
Reload Postgresql server
$ service postgresql reload
Install nginx
$ apt-get install nginx
Install PCRE library (necessary for rewrite module, if you need it)
$ apt-get install libpcre3-dev
Install uWSGI
$ apt-get install uwsgi uwsgi-plugin-python
Example website is example.com
Create folders
$ mkdir -p /web/example.com/
$ cd /web/example.com/
$ mkdir logs public_html source run
$ cd /web/example.com/source/
$ git clone [repositoty clone url]
Set owner to www-data
$ chown -R www-data:www-data /web
$ cd /web
$ virtualenv example.com
$ cd /etc/nginx/sites-available
$ vim example.com
Add the following configuration to the file
server {
listen 80;
server_name $hostname;
access_log /web/example.com/logs/access.log;
error_log /web/example.com/logs/error.log;
location / {
uwsgi_pass unix:///web/example.com/run/example.sock;
include uwsgi_params;
uwsgi_param UWSGI_SCHEME $scheme;
}
location /static {
root /web/example.com/public_html/;
}
}
Then create a symlink to enable website
$ ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
Then you may want to delete default site
$ rm /etc/nginx/sites-enabled/default
It's also a good practice to enable Gzip compression on Nginx.
$ vim /etc/nginx/nginx.conf
Search for Gzip settings within the file and remove comment on the following settings
gzip_vary
gzip_types
$ vim /etc/uwsgi/apps-available/example.com.ini
Add the following configuration to the file
[uwsgi]
plugin = python
vhost = True
pythonpath = /web/example.com/source/example_project/
chdir = /web/example.com/source/example_project/
virtualenv = /web/example.com
wsgi-file = /web/example.com/source/example_project/config/wsgi.py
processes = 10
socket = /web/example.com/run/example.sock
chmod-socket = 664
chown-socket = www-data
vacuum = true
daemonize = /web/example.com/logs/uwsgi.log
logto2 = /web/example.com/logs/uwsgi.log
Then create a symlink
$ ln -s /etc/uwsgi/apps-available/example.com.ini /etc/uwsgi/apps-enabled/example.com.ini
Activate virtualenv
$ cd /web/example.com/
$ . bin/activate
Install python packages from your requirements.txt file
$ pip install -r requirements.txt
-
Set necessary Django settings such as
ALLOWED_HOSTS SECRET_KEY DATABASES
-
(If applicable) Set DJANGO_SETTINGS_MODULE environment variable to point to production settings
export DJANGO_SETTINGS_MODULE='config.settings.production'
-
Migrate database
$ python manage.py migrate
-
Collect static and symlink to public_html
$ python manage.py collectstatic $ ln -s [path_to_staticfiles] /web/example.com/public_html/static
If you're using django-compressor with offline mode on, you have to manually static files.
$ python manage.py compress
$ service uwsgi restart
$ service nginx restart