GNOME – Django, MySQL & PyCharm

preparation

At first update your system.

sudo apt-get update
sudo apt-get upgrade

install PyCharm

sudo snap install pycharm-professional --classic

create project

Create a new django project in PyCharm. In case PyCharm was not able to create the virtual environment, you might need to install the Python package for it. Probably pip is missing then too.

sudo apt install python3-pip
sudo pip install virtualenv

Run the configuration and check:

http://127.0.0.1:8000/

Change now the host in the run/debug configurations to “0.0.0.0”.

In the settings.py add the IP to the allowed hosts.

ALLOWED_HOSTS = ['0.0.0.0']

In case you are using multiple ways to access the website, add the other IP’s too.

open ports

As the website should be visible from outside we need to open the ports.

sudo ufw enable
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 8000

Restart Django server.

router and DNS

It depends a little bit on your provider and your setup at home. Usually you can activate a remote access to your router and forward the port 80 to 8000. To use a URL create an A Record to forward the request from your URL to your personal IP.

Now you should be able to access the website through your own URL.

MySQL database & apache

sudo apt-get install mysql-server
sudo mysql_secure_installation
sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl

Check the status of both.

sudo service mysql status
sudo systemctl status apache2

Add this to your “etc/apache2/apache2.conf”:

Include /etc/phpmyadmin/apache.conf

Change the password of the root user:

sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '[PASSWORD]';

Now phpmyadmin should be reachable through “localhost/phpmyadmin”.

Login and create a database with your projects name.

Add this to your settings.py instead of the existing database configuration:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '[PROJECTNAME]',
        'USER': 'root',
        'PASSWORD': '[PASSWORD]',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        'OPTIONS': {"init_command": "SET foreign_key_checks = 0;"},
    }
}

Do not use localhost as this won’t work.

Now in PyCharm install:

pip install pymysql

And add:

import pymysql
pymysql.install_as_MySQLdb()

to your __init__.py in the project folder.

The database is now used through the root user. You can change this by creating a new user in phpmyadmin and adjusting the database settings in your django project. Also you should protect the root user with a password.

autostart server

As your server should be running all the time it needs to autostart.

Create a file containing the following code and safe it as “autorunserver.sh” in the PycharmProjects folder.

cd /home/[USERNAME]/PycharmProjects/[PROJECTNAME]
source /home/[USERNAME]/PycharmProjects/[PROJECTNAME]/venv/bin/activate
python /home/[USERNAME]/PycharmProjects/[PROJECTNAME]/manage.py runserver 0.0.0.0:8000 &
exit 0

Check if it is working by using:

sudo -u [USERNAME] /home/[USERNAME]/PycharmProjects/autorunserver.sh

Now we add the script to the crontabs to be executed on startup.

sudo crontab -e

Add:

@reboot /home/[USERNAME]/PycharmProjects/autorunserver.sh

to the file.

Important

Watch out that the DEBUG mode is still activated in your Drango project (settings.py).

Useful

Enable SSH:

sudo apt install openssh-server

No further actions needed.