Linode is a server hosting company.(Online Deployment) They offer virtual private server (VPS) at budget-friendly cost which is good if you are just trying to experiment with setting up your own server, installing applications on the server, running your own website, hosting a small to medium sized web application. As your app/website grow, you can expand your plans which might make it ideal for startups, freelancers or students.
Creating Linode account and setting up server
Create an account with Linode using https://rathank.com/linode/ When you sign up to Linode using my referral link, you’ll receive a $100, 60-day credit once you add a valid payment method to your new account. Using the free credits you can setup your server and host your website on Linode, you will not be charged until 60 days or $100 spends. If you keep your website up and running for over 60 days, you will be charged $5 + tax ($5 Nanode as shown in the course)
Install backports.zoneinfo only if the server python version is less than 3.9
backports.zoneinfo==0.2.1;python_version<"3.9"
Add gunicorn to requirements.txt
Setting up Git to push the code from local server to remote server
Remote server
While you are logged in to the server via SSH, create a project directory as foodonline-dir inside /home/foodonline/ folder
cd /home/foodonline/
mkdir foodonline-dir
cd foodonline-dir
mkdir site.git
cd site.git
git init --bare
--bare means that our folder will have no source files, just the version control.
Run ls to see the files and folders inside the current directory, in our case we are inside site.git, you will see ‘hooks‘ folder
ls
Let’s get into ‘hooks’ folder
cd hooks
Now, create the file ‘post-receive’ by typing:
sudo nano post-receive
Type below code inside post-receive file
#!/bin/sh
git --work-tree=/home/foodonline/foodonline-dir --git-dir=/home/foodonline/foodonline-dir/site.git checkout -f main
Save post-receive file by pressing CTRL + X, Y, Enter
In order to execute the file, we need to set the proper permissions using:
sudo chmod +x post-receive
Local Machine
In FoodOnline project, we have already initialized git on the root folder of our project. Now, we need to configure the remote path of our repository. Tell Git to add a remote called ‘live’:
git remote add live ssh://foodonline@SERVER_IP_ADDRESS/home/foodonline/foodonline-dir/site.git
Now, whenever we make changes in the local project and want to push the changes to live server, we simply run this command
git add -A
git commit -m "new changes to go live"
git push live main
Now check the remote directory, our project is pushed to the server. That’s all, we have set up Git to push the code from local machine to remote server.
Install & Configure PostgreSQL
Install
While you are logged into the server, run following commands to install, start, enable and see the status of postgresql database: sudo apt-get install postgresql postgresql-contrib sudo systemctl start postgresql.service sudo systemctl enable postgresql.service sudo systemctl status postgresql.service
Configure
Set new password for default postgres user
sudo passwd postgres
sudo su - postgres
If you want to reset postgres user’s password, run the following command
psql -d postgres -c "ALTER USER postgres WITH PASSWORD 'NEW PASSWORD';"
Login to postgres shell
psql postgres
Create new database
CREATE DATABASE foodonline_db;
exit
exit
Setup Virtual Environment, configure .env file and run the server
In the foodonline-dir folder, create virtual environment and activate it virtualenv env source env/bin/activate
Create .env file and provide all the required secret information sudo nano .env
Optionally, refresh the virtual environement by running deactivate and source env/bin/activate
Install the packages from requirements.txt file pip install -r requirements.txt
Run makemigrations and migrate commands python manage.py migrate
You will probably get GDAL and PostGis error as they are necessary to run our project
Install GDAL library and PostGis extension on live server
Run the GDAL path configuration in settings.py only when the site is on windows local server.
Run the server python manage.py runserver 0.0.0.0:8000
Backup data from local machine and restore in to live database
Local machine
python manage.py dumpdata > backup.json Comment out post_save signal post_save_create_profile_receiver() function to prevent creating a UserProfile which essentially throws “IntegrityError – duplicate key value violates unique constraint” error git add -A git commit -m "database backup" git push live main
Remote server
python manage.py shell
from django.contrib.contenttypes.models import ContentType
ContentType.objects.all().delete()
quit()
python manage.py loaddata backup.json
Once the loaddata is successful, go to local project and uncomment post_save_create_profile_receiver() function and then push the code to live.
Configuring Gunicorn
Install Gunicorn on our server sudo apt install gunicorn
Tell gunicorn to bind to our Django application and start running gunicorn --bind 0.0.0.0:8000 foodOnline_main.wsgi
Test the link and make sure the site is working
exit
Go to this location and paste below code: sudo nano /etc/systemd/system/gunicorn.socket
This post considers that you have already installed PostgreSQL & pgAdmin on your windows computer. So, you should already have the Application Stack Builder software on your system. Application Stack Builder is actually used to download and install add-ons to our Postgres databases.
Goto Application Stack Builder and install PostGIS extension.
Go to Pgadmin 4 and create a PostGIS extension for your database
Click on the Query tool and run the below query
CREATE EXTENSION postgis;
Install GDAL on Windows
Download GDAL wheel file that is supported for your platform from here
Cut the wheel file and paste it inside your project’s root directory. Make sure your virtual environment is activated.
Install the wheel using command pip install name_of_the_file
You will see osgeo folder has been created in the location ‘..envLibsite-packages’ .
Go to osgeo folder and copy the path of your gdalxxx.dll file and add it to the settings.py file as gdal library path.
To be sure that GDAL is installed in your system, just type:
gdal-config --version
Now we have GDAL installed, we proceed to setup a GDAL Python binding. Just upgrade your version of pip before to install the library in python environment.
pip3 install — upgrade pip
Once updated, just type:
pip3 install gdal==x.x.x
Replace x.x.x with the gdal version, it must be the same as the one obtained in the step above using gdal-config. To avoid conflicts or any further issues, it’s important to install and use the exact versions that any component will require. Sometimes you could experiment ModuleNotFoundError trying to install the GDAL python binding, you can solve this issue by installing the needed modules. The most required module is Numpy, and you can install it just by typing:
pip3 install numpy # only if you getNumpy ModuleNotFoundError
Check out your GDAL python binding
python3 -c "from osgeo import gdal"
You won’t see anything if everything is well installed.