Hello community! Today, I want to show you, at a beginner level, how to make your Django app live on production using an Ubuntu server/desktop. We will use server-side software, specifically Apache. In this tutorial, we will set up the server to run on HTTP ports 80 and 8000. Later, I will provide a tutorial on running over HTTPS port 443 with secure SSL.
- Install Ubuntu: Make sure you have Ubuntu installed on your server or desktop.
- Ensure you have a Django app: Your Django project should be ready for deployment.
- Internet connection: Required for installing necessary packages and libraries.
To get started, you'll need to transfer your Django project folder from a Windows machine to your Ubuntu server using a USB drive.
-
Copy your Django project folder from Windows to a USB drive: Ensure your Django project is saved on the USB drive.
-
Connect your USB drive to Ubuntu OS: Plug the USB drive into your Ubuntu system.
-
Open your USB drive: Navigate to the USB drive in your file manager.
-
Open Terminal: Right-click inside the USB drive and select 'Terminal' to open a terminal session in the USB drive's directory.
-
Copy the Django project folder: Run the following command to copy the Django folder and all its contents to the desired location on your Ubuntu server.
sudo cp -r SUPERMARKET /home/management/
- SUPERMARKET: This is the name of your Django app folder.
- /home/management/: This is the path where you are copying the folder to on your Ubuntu system.
Once you've copied the project, navigate to the project directory on your Ubuntu server:
-
Navigate to your Django app directory:
cd /home/management/SUPERMARKET/project/
-
List files to ensure they are present:
ls -l
Verify that you see these files:
app
,db.sqlite3
,manage.py
,project
,static
.
A virtual environment is essential for managing dependencies and isolating your Django project from other Python projects.
-
Update packages:
sudo apt update
-
Install Python 3 and
venv
:sudo apt install python3-venv
-
Create a virtual environment:
sudo python3 -m venv venv
-
Activate the virtual environment:
source venv/bin/activate
-
Enable write permissions to install libraries in
venv
:sudo chmod -R 777 venv
Ensure that all necessary Python libraries are installed within your virtual environment.
-
Install Django and other libraries:
pip install django pip install pandas pip install plotly pip install scikit-learn pip install numpy pip install django-admin-menu pip install django-import-export
Before configuring Apache, ensure your Django app runs correctly.
-
Run your Django application:
python manage.py runserver
Your app should now be accessible at
http://127.0.0.1:8000/
by default.
You'll need to know your server's IP address for configuring Apache.
-
Install
net-tools
:sudo apt install net-tools
-
Check your IP address:
ifconfig
OR
ip addr show
Note down the server IP address. In my case, it is
10.0.2.15/24
.
Configure your Django settings to ensure compatibility with Apache.
-
Install
gedit
for editing files:sudo apt install gedit
-
Open and edit Django settings:
sudo gedit /home/management/SUPERMARKET/project/settings.py
-
Update settings:
ALLOWED_HOSTS = ["*"] STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
- ALLOWED_HOSTS: Set to
["*"]
to allow all hosts (for development; specify allowed hosts in production). - STATIC_URL: URL prefix for static files.
- STATIC_ROOT: Directory where static files will be collected.
Remove this line from
settings.py
:STATICFILES_DIRS = [BASE_DIR / 'static']
- ALLOWED_HOSTS: Set to
-
Make write permissions on 'static' directory:
sudo chmod -R 777 static/
- Configure Apache to serve your Django app.
sudo apt update
sudo apt install apache2 libapache2-mod-wsgi-py3 python3-pip
-
Create a new Apache configuration file:
sudo nano /etc/apache2/sites-available/django_project.conf
-
Add the following configuration:
<VirtualHost *:80> ServerAdmin admin@management ServerName 10.0.2.15 DocumentRoot /home/management/SUPERMARKET/project Alias /static /home/management/SUPERMARKET/project/static <Directory /home/management/SUPERMARKET/project/static> Require all granted </Directory> <Directory /home/management/SUPERMARKET/project> <Files wsgi.py> Require all granted </Files> </Directory> WSGIDaemonProcess supermarket python-path=/home/management/SUPERMARKET/project python-home=/home/management/SUPERMARKET/project/venv WSGIProcessGroup supermarket WSGIScriptAlias / /home/management/SUPERMARKET/project/project/wsgi.py ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
- ServerAdmin: Email address of the server administrator.
- ServerName: Your server's IP address or domain name.
- DocumentRoot: Directory where your Django project is located.
- Alias /static: Maps the URL
/static
to the directory where static files are stored. - Directory: Permissions for static files and the WSGI script.
- WSGIDaemonProcess: Configures the WSGI daemon process for your Django project.
- WSGIScriptAlias: Path to the WSGI script for your Django project.
Activate the new Apache site configuration and mod_wsgi module.
-
Enable the new site and mod_wsgi module:
sudo a2ensite django_project.conf sudo a2enmod wsgi
-
Restart Apache:
sudo systemctl restart apache2
Ensure Apache has the necessary permissions to read and execute files.
-
Ensure Apache has read and execute permissions:
sudo chown -R www-data:www-data /home/management/SUPERMARKET/project sudo chmod -R 755 /home/management/SUPERMARKET/project/ sudo chown -R www-data:www-data /home/management/SUPERMARKET/project/static/ sudo chmod -R 755 /home/management/SUPERMARKET/project/static/
-
Enable the site configuration and reload Apache:
sudo a2ensite django_project.conf sudo a2dissite 000-default.conf sudo systemctl reload apache2
-
Ensure Apache has read permission on parent directories:
sudo chmod 755 /home sudo chmod 755 /home/management sudo systemctl restart apache2
-
Run your project: Open a web browser and navigate to
http://10.0.2.15
to see your Django app live.
If you prefer to use port 8000 instead of the default port 80, follow these additional steps.
Configure Apache to listen on port 8000.
-
Open the ports configuration file:
sudo gedit /etc/apache2/ports.conf
-
Change the port from 80 to 8000:
Listen 8000
Modify your virtual host configuration to use port 8000.
-
Open the virtual host configuration file:
sudo gedit /etc/apache2/sites-available/django_project.conf
-
Change the port from 80 to 8000:
<VirtualHost *:8000>
-
Restart Apache:
sudo systemctl restart apache2
-
Run your site: Open a web browser and navigate to
http://10.0.2.15:8000
to access your Django app on port 8000.
In this tutorial, you learned how to deploy a Django application on an Ubuntu server using Apache. We covered the following:
- Copying Your Django Project: Transferring your project from Windows to Ubuntu.
- Creating and Configuring a Virtual Environment: Isolating your Python environment.
- Installing Required Libraries: Ensuring all necessary libraries are available.
- Running Your Django App: Testing the application before configuring Apache.
- Configuring Apache for HTTP Ports 80 and 8000: Setting up Apache to serve your Django application.
This setup allows you to deploy and serve your Django application using Apache on your Ubuntu server. For a more secure deployment, consider setting up HTTPS with SSL certificates, which will be covered in a future tutorial.
My Contacts
WhatsApp
+255675839840
+255656848274
YouTube
Visit my YouTube Channel
Telegram
+255656848274
+255738144353
PlayStore
Visit my PlayStore Developer Page
GitHub
Visit my GitHub