Deploying a Django application on Oracle Cloud using an Ubuntu 22.04 instance with Apache, PhpMyAdmin, and MySQL involves several steps. Below is a comprehensive guide to help you through the process:
Step 1: Create an Oracle Cloud Account and Set Up an Instance
- Sign Up/Login: Go to the Oracle Cloud website and sign up for an account or log in if you already have one.
- Create an Instance:
- Navigate to the Oracle Cloud dashboard.
- Go to the Compute section and click on “Instances”.
- Click on “Create Instance”.
- Choose the appropriate configurations (e.g., Ubuntu 22.04 as the OS).
- Configure networking and security settings (e.g., open ports 80, 443 for HTTP/HTTPS). or use given below the commands
1 2 3 4 |
sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 443 -j ACCEPT sudo netfilter-persistent save sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 80 -j ACCEPT sudo netfilter-persistent save |
Step 2: Access the Instance
SSH into the Instance:
-
- Use an SSH client (like PuTTY on Windows or Terminal on macOS/Linux) to connect to your instance using the public IP and the private key generated during instance creation.
- Command:
ssh -i /path/to/private-key opc@your_instance_public_IP
How to LAMP Install on Oracle Cloud Ubuntu 22.04 Server
Install Python and Django dependencies:
1 2 |
sudo apt install python3-pip python3-dev libapache2-mod-wsgi-py3 -y sudo apt install libmysqlclient-dev -y |
Set Up Your Django Project Directory
1 2 |
sudo mkdir /var/www/mywebcode.com sudo nano /var/www/mywebcode.com/index.html |
1 2 3 4 5 6 7 8 |
<html> <head> <title>mywebcode.com</title> </head> <body> <h1>Welcome to www.mywebcode.com website</h1> </body> </html> |
Possible Deployment Issues
To avoid deployment errors, we will take following actions:
Add allowed host, release media folder, run collectstatic and create requirements.txt from the local project.
1 2 3 4 |
ALLOWED_HOSTS = ['SERVER_IP_ADDRESS', 'localhost', '127.0.0.1'] Remove 'media' from .gitignore file python manage.py collectstatic pip freeze > requirements.txt |
Clone your Django project:
- Navigate to the directory where you want to store your project
1 2 |
cd /var/www/mywebcode.com upload Your project using filezilla |
Configure Django to use MySQL:
Edit your settings.py
file to configure the database settings:
1 2 3 4 5 6 7 8 9 10 |
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'mywebcode', 'USER': 'dydevops', 'PASSWORD': 'your_password', 'HOST': 'localhost', 'PORT': '3306', } } |
Step 1: Install virtualenv
First, ensure virtualenv
is installed. You can install it using pip
:
1 2 3 4 5 |
sudo pip3 install virtualenv cd /var/www/mywebcode.com virtualenv gvenv or sundo python3 -m venv gvenv |
Activate the virtual environment:
1 |
source gvenv/bin/activate |
Install Django and Other Dependencies
With the virtual environment activated, install Django and other required packages:
pip install mysqlclient for MySQL Database connections in Django app solve error for mysqlclient
1 2 3 4 5 6 7 |
sudo add-apt-repository universe sudo apt-get install net-tools -y sudo apt-get install python3 -y sudo apt-get install python3-pip -y pip install pkgconfig sudo apt-get install pkg-config -y pip install mysqlclient |
You can also install any other dependencies listed in your requirements.txt
file:
1 |
pip install -r requirements.txt |
Adjust Apache Configuration to Use the Virtual Environment
Ensure your Apache configuration file points to the correct virtual environment. Open your Apache configuration file:
1 |
sudo nano /etc/apache2/sites-available/mywebcode.com.conf |
Ensure the WSGIDaemonProcess
directive points to the virtual environment you created:
Understood. Let’s proceed with the deployment instructions for your Django project named dydevops
but with the project directory named mywebcode.com
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<VirtualHost *:80> ServerAdmin admin@mywebcode.com ServerName mywebcode.com ServerAlias www.mywebcode.com DocumentRoot /var/www/mywebcode.com Alias /media/ /var/www/mywebcode.com/media/ Alias /static /var/www/mywebcode.com/static <Directory /var/www/mywebcode.com/media/> Require all granted </Directory> <Directory /var/www/mywebcode.com/static> Require all granted </Directory> <Directory /var/www/mywebcode.com/dydevops> <Files wsgi.py> Require all granted </Files> </Directory> WSGIDaemonProcess dydevops python-path=/var/www/mywebcode.com python-home=/var/www/mywebcode.com/gvenv WSGIProcessGroup dydevops WSGIScriptAlias / /var/www/mywebcode.com/dydevops/wsgi.py ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> |
Restart Apache
After configuring Apache to use the virtual environment, restart Apache to apply the changes:
1 |
sudo systemctl restart apache2 |
Check TEMPLATES
Setting in settings.py
Ensure that your TEMPLATES
setting in settings.py
is correctly configured to look for templates in the appropriate directories. Here is an example configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] |
Enable the site and necessary modules:
1 2 3 |
sudo a2ensite mywebcode.com.conf sudo a2enmod wsgi sudo systemctl restart apache2 |
Additional Considerations
Set up a virtual environment for your Django project:
1 2 |
source gvenv/bin/activate pip install -r requirements.txt |
Configure Django settings for production (e.g., DEBUG = False
, configure ALLOWED_HOSTS
, set up proper logging, etc.).
Secure your application by enabling HTTPS with a tool like Let’s Encrypt:
1 2 3 4 5 6 7 8 9 |
sudo service apache2 restart sudo iptables -save >/etc/iptables/rules.v4 sudo apt install certbot sudo mkdir /var/www/mywebcode.com/.well-known sudo chown -R ubuntu /var/www/mywebcode.com sudo charp www-data/var/www/mywebcode.com sudo chmod g+s /var/www/mywebcode.com sudo apt install certbot python3-certbot-apache sudo certbot --apache |
or
1 2 |
sudo apt install certbot python3-certbot-apache -y sudo certbot --apache |
Following these steps should help you deploy your Django application named
dydevops
in the mywebcode.com
directory on an Ubuntu server in Oracle Cloud Infrastructure using MySQL, Apache, and PHPMyAdmin.
1 2 |
python manage.py makemigrations python manage.py migrate |
Update
CSRF_TRUSTED_ORIGINS
Ensure CSRF_TRUSTED_ORIGINS
is correctly set in your settings.py
:
1 2 3 4 5 |
# settings.py CSRF_TRUSTED_ORIGINS = [ 'https://www.mywebcode.com', ] |
1 |
ALLOWED_HOSTS = ['www.mywebcode.com','mywebcode.com'] |
Step 5: Setup Apache2 with mod_wsgi
- Create a WSGI file for your project:
Create /path/to/mywebcode.com/dydevops/wsgi.py
:
1 2 3 4 5 |
import os from django.core.wsgi import get_wsgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dydevops.settings') application = get_wsgi_application() |
Secure Your Deployment
Enable SSL:
Consider setting up SSL using Let’s Encrypt for secure HTTPS connections
1 2 |
sudo apt install certbot python3-certbot-apache sudo certbot --apache -d www.mywebcode.com -d mywebcode.com |
Firewall Settings:
1 2 |
sudo ufw allow 'Apache Full' sudo ufw enable |
Permissions:
1 2 3 4 5 6 7 8 |
sudo chown -R ubuntu /var/www/mywebcode.com sudo chmod -R 755 /var/www/mywebcode.com sudo chmod -R 777 /var/www/mywebcode.com sudo chown -R www-data:www-data /var/www/mywebcode.com sudo chmod 664 /var/www/mywebcode.com/media/ sudo chmod 775 /var/www/mywebcode.com/media/ sudo chown www-data:www-data /var/www/mywebcode.com/media/ sudo chown -R www-data:www-data /var/www/mywebcode.com/media/ |
By following these steps, you should have your Django project deployed on an Oracle Ubuntu server using Apache2, MySQL, and phpMyAdmin, with proper CSRF handling and security measures.