Python Shared server domain setup
Log in to the cPanel.
Click the Setup Python App icon in the Software section.
Click on the Create Application button.
. Set the Python Version
. The Application Root is the folder where the Python application will be created in your hosting. This will be the folder where you will start or upload your Django project.
. In the Application URL, select the domain/subdomain and sub-directory where you want your Django application to be live. This will be the web address where you will see your Django app.
. In the Application startup file, type
- In the Application entry point, type
You can also see the settings in the screenshot below. After you are done, click on Create.
Open the URL where you set up your Python app. You should see a page like this. Do not worry about the error yet. It will disappear once we create our Django app.
Install Django
After your python app is set up, you will see a page as shown in the screenshot below. Click the command to enter the virtual environment to copy it to the clipboard.
Make sure you have command line access to your server. Try to find “Terminal” options in your Cpanel, If not Enable it.
Enter the command into the online terminal by Right Click / Paste and press enter. You will enter the Python virtual environment. In my case, the command is
source /home/hhsupportwhm/virtualenv/django_test/3.3/bin/activate && cd /home/hhsupportwhm/django_test
Note: It will be not the same for you.
Now you have to install Django. I have tested Django 4.0.4 on shared hosting and it works fine with the deployment method I am describing in this post. You can install Django 4.0.4 using the following command:
If any other modules are required for your project, install them here using pip.
Confirm the Django installation by running the following command.
Start a Django project
Create a new project
Go to the terminal in your Cpanel and enter the following command
django-admin startproject myapp ~/django_test
Make sure to replace “myapp” with your application name and “django_test” at the end with the directory name where you set up your Python app. The directory name should be the same as the one used while setting up the Python app.
Now open the file manager in CPanel (You can also use FTP) and go the folder where you set up your Django app. In my case, it is the “django_test” folder. You will see a folder with your app’s name and the manage.py file here.
Edit the passenger_wsgi.py file.
Delete everything in the file and add just one line.
from myapp.wsgi import application
Do not forget to replace “myapp” with your application name. Click on Save Changes to save the changes to the file.
Now open your application folder_ in my case, it is “myapp”.
Edit the settings.py file.
In the ALLOWED_HOSTS list, add the URL where our application will be running (which you provided while setting up the python app). In my case, it is “hostinghome.in”. If your site has a www variant, make sure to add that here too if you want your site to be working in www as well.
Now go to Set up Python app in your CPanel and Restart your application.
Now open the URL where you have set up your application and you should see the Django welcome page.
Set up the database
In this part, we will set up a MySQL database from the hosting with Django. Go to the MySQL Databases option in CPanel.
Create a new Database
Create a new User. Copy the password of this user somewhere because we will need it in the upcoming steps.
Add the user to the database
Allow all the privileges to this user.
Go to the terminal again. Enter the virtual environment and run the following command to install mysql with pip.
pip install pymysql
Edit your settings.py file. Replace the default database code with this.
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’,
‘NAME’: ‘database name’,
‘USER’: ‘database username’,
‘PASSWORD’: ‘databasepassword’,
‘HOST’: ‘localhost’,
‘PORT’: ‘3306’,
}
Make sure to replace the database name with the database name, the database username with the user name, and the database password with the password for the user.
Now edit the __init__.py file in the app directory.
Add the following code to the file and save it.
import pymysql
pymysql.install_as_MySQLdb()
Apply migrations to the database.
python manage.py makemigrations
python manage.py migrate
Set up static files
Edit your settings.py file and add the following two lines in the static files section. Make sure to replace the username by your CPanel username and the domain root with the path of the directory which is ser as the domain root.
STATIC_URL = ‘/static/’
STATIC_ROOT = ‘/home/username/domainroot/static’
Now open the terminal and run the following command.
python manage.py collectstatic
Go to the domain root folder and check if the static folder has been successfully copied.
Go to “Setup Python App” in CPanel and Restart the app.