This guide provides step-by-step instructions to successfully deploy your Django application on Azure Cloud.
The Django file hierarchy is crucial for organizing static files. In this guide, you will learn the necessary steps to ensure the server-side processing of static files.
-
Specify
STATIC_ROOT
in thesettings.py
file of your project.# settings.py STATIC_ROOT = os.path.join(BASE_DIR, 'static')
-
Comment out the
STATIC_DIRS
variable in thesettings.py
file of your project.# settings.py # STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
-
Run the following command in your project directory to collect all static files:
$ python manage.py collectstatic
-
Transfer the library requirements used to the
requirements.txt
file:$ python -m pip freeze > requirements.txt
To introduce and make your website accessible, you should choose a domain address. This guide recommends using the 'site.ac' website to obtain a free domain.
- Go to site.ac and create an account.
- Follow the relevant steps to choose a free domain name.
Create an Azure account to deploy your Django application on Azure Cloud.
- Go to the Azure portal and sign in or create a new account.
- Choose the package that suits you and make the purchase. Free credits worth $100 are available for students.
- Fill in the necessary information, create your account by specifying a username and password.
- Complete authentication processes for security purposes.
Create a virtual machine on Azure portal.
- Give your virtual machine a name and specify the region, traffic intensity, disk, and RAM sizes.
- Choose the Linux operating system (Ubuntu 20.04 LTS is recommended).
- Create an SSH key and connect to your virtual machine.
Configure your Django application to work with Gunicorn and Nginx.
-
Install the necessary packages on your virtual machine:
$ sudo apt-get update $ sudo apt-get install python3-pip python3-dev libpq-dev nginx
-
Clone the Django project from GitHub and create a virtual environment:
$ git clone https://github.com/serkanyasr/Django-DashBoard.git $ cd Django-DashBoard $ virtualenv venv $ source venv/bin/activate $ pip install -r requirements.txt
-
Install Gunicorn and run the Django project:
$ pip install gunicorn $ gunicorn --bind 0.0.0.0:8000 DashBoard.wsgi
-
Create the Nginx configuration file:
server { listen 80; server_name your_domain.com www.your_domain.com your_virtual_machine_IP; root /path/to/your/project; location /static/ { alias /path/to/your/project/static/; } location /media/ { alias /path/to/your/project/media/; } location / { include proxy_params; proxy_pass http://unix:/path/to/your/project/DashBoard.sock; } }
-
Start Nginx and register Gunicorn as a service:
$ sudo systemctl start nginx $ sudo systemctl enable nginx
$ cd /etc/systemd/system $ sudo nano gunicorn.service
After opening the service file, edit the content as follows:
[Unit] Description=gunicorn daemon for Django project After=network.target [Service] User=your_username Group=your_username WorkingDirectory=/path/to/your/project ExecStart=/path/to/your/project/venv/bin/gunicorn --workers=3 --bind unix:/path/to/your/project/DashBoard.sock DashBoard.wsgi:application [Install] WantedBy=multi-user.target
Start and enable the service:
$ sudo systemctl start gunicorn $ sudo systemctl enable gunicorn
Your Django application is now successfully running on Azure Cloud! If you encounter any issues, carefully review the steps and correct any errors. Best of luck!