Global install
sudo apt-get install apache2 python3-django libapache2-mod-wsgi-py3 virtualenv
### MySQL
sudo apt-get install mysql-server libmysqlclient-dev
mysql_secure_installation
Virtual env setup
Not tested
sudo apt-get install python3-venv (doesn't need. )
sudo apt-get install python3-dev
sudo a2enmod wsgi
sudo systemctl restart apache2
Make Python Virtual Environment
### Install and create venv
sudo apt-get install python3-pip
pip3 install virtualenv
### Go to desired location to create venv
sudo virtualenv venv -p python3
Activate venv
source venv/bin/active
### Examples
(venv) pip install django
(venv) pip install mysqlclient
Install all the requirements
pip install -r requirements.txt
### How to get requirements
pip freeze > requirements.txt
Clone your Git project for deployment
sudo apt-get install git
### config your account
git config --global user.name "your.user.name"
git config --global user.email "your@email.com"
### Go to desired location and clone from Git
git clone https://github.com/your/repository .
Pull your Git project (good for development)
git init
git remote add origin https://github.com/your project.git
git reset --hard origin/master
git pull origin master
# fatal: remote origin already exists
git remote -v
git remote rm origin
# Must restart apache for changes to take effect
sudo systemctl restart apache2
apache 000-default.conf virtualhost
<VirtualHost *:88>
ServerAdmin webmaster@localhost
ServerName hostname.com
# DocumentRoot /var/www/test
Alias /static /var/www/django/test.com/static
<Directory "/var/www/django/test.com/static">
Options FollowSymLinks
Order allow,deny
Allow from all
Require all granted
</Directory>
Alias /media /var/www/django/test.com/media
<Directory "/var/www/django/test.com/media">
Options FollowSymLinks
Order allow,deny
Allow from all
Require all granted
</Directory>
WSGIScriptAlias / /path/to/wsgi.py
WSGIDaemonProcess domain.sg python-home=/venv python-path=/path/to/manage.py/folder
WSGIProcessGroup domain.sg
<Directory /path/to/wsgi.py/folder>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
manage.py checklist
(venv) $ python manage.py check
(venv) $ python manage.migrate
(venv) $ python manage.py createsuperuser
(venv) $ python manage.py runserver 0.0.0.0:8000
(venv) $ python manage.py collectstatic
www-data should have access to the application folder
sudo chown -R www-data:www-data /var/www/test
Reload apache
sudo apachectl configtest
sudo systemctl restart apache2
##### Troubleshooting #####
### Disable 000-default.conf
sudo a2dissite 000-default.conf
### Enable site django.conf
sudo a2ensite django.conf
### Python alternatives
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6
### Set
sudo update-alternatives --set python /usr/bin/python3.6
Gmail smtp
### Turn on Less secure app access
https://myaccount.google.com/lesssecureapps
### DisplayUnlockCaptcha (Did not work for me)
https://accounts.google.com/DisplayUnlockCaptcha
Error: “Could not reliably determine the server’s fully qualified domain name”
### edit
sudo nano /etc/apache2/apache2.conf
### add ServerName
ServerName Django
Environment variable
sudo nano .profile
### Set env variable for ID & Password
export EMAIL_USER='xyz@gmail.com'
export EMAIL_PASSWORD='password'
### Test environment variable
echo $EMAIL_USER
echo $EMAIL_PASSWORD