How To Deploy Django on Oracle Cloud Ubuntu 22.04 using Apache PhpMyAdmin and MySQL

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

  1. Sign Up/Login: Go to the Oracle Cloud website and sign up for an account or log in if you already have one.
  2. 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

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:

Set Up Your Django Project Directory

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.

Clone your Django project:

  • Navigate to the directory where you want to store your project

Configure Django to use MySQL:

Edit your settings.py file to configure the database settings:

Step 1: Install virtualenv

First, ensure virtualenv is installed. You can install it using pip:

Activate the virtual environment:

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

You can also install any other dependencies listed in your requirements.txt file:

Adjust Apache Configuration to Use the Virtual Environment

Ensure your Apache configuration file points to the correct virtual environment. Open your Apache configuration file:

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.

Restart Apache

After configuring Apache to use the virtual environment, restart Apache to apply the changes:

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:

Enable the site and necessary modules:

Additional Considerations

Set up a virtual environment for your Django project:

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:


or

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.

Update CSRF_TRUSTED_ORIGINS

Ensure CSRF_TRUSTED_ORIGINS is correctly set in your settings.py:

Step 5: Setup Apache2 with mod_wsgi

  1. Create a WSGI file for your project:

Create /path/to/mywebcode.com/dydevops/wsgi.py:

Secure Your Deployment

Enable SSL:

Consider setting up SSL using Let’s Encrypt for secure HTTPS connections

Firewall Settings:

Permissions:

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.

Share on:

Hello, I am Dharmendra Yadav and I am a Python Developer with experience in web development using Django, Flask, REST API, SQL, MySQL, HTML, CSS, JavaScript, WordPress, Oracle Cloud, AWS and Git. I also write technical articles where I explain web development and Software Engineering. Facebook , Linkedin