Overview
Django is a high-level Python Web framework that encouragesrapid development and clean, pragmatic design. This guide will walk you throughdeploying a Django application on your Virtual Private Server (VPS) hosted byHSJ.HOST, enabling you to build scalable and robust web applications.
Prerequisites
- A VPS account with HSJ.HOST.
- SSH access to your server.
- Python 3 and pip installed on your VPS.
- Basic knowledge of the Linux command line and Python.
Procedure
1. Access Your VPS via SSH
- Use your terminal to SSH into your server: ssh user@your_vps_ip, replacing user with your actual username and your_vps_ip with the IP address of your VPS.
2. Update Your Server's Package List
- Keep your server up-to-date with sudo apt update and optionally sudo apt upgrade.
3. Create a Python Virtual Environment
- Install the python3-venv package if it's not already installed:
code
sudo apt install python3-venv
- Create a new directory for your Django project and navigate into it:
Copy code
mkdir my_django_project && cd my_django_project
- Create a virtual environment within the project directory:
code
python3 -m venv myvenv
- Activate the virtual environment:
bash code
source myvenv/bin/activate
4. Install Django
- With your virtual environment activated, install Django using pip:
code
pip install django
5. Start Your Django Project
- Create a new Django project by running:
code
django-admin startproject myproject .
- Adjust the project settings as necessary, particularly the ALLOWED_HOSTS setting in myproject/settings.py to include your VPS IP address or domain name.
6. Run Migrations
- Apply Django's database migrations with:
code
python manage.py migrate
7. Start the Django Development Server
- Run the Django development server to test your setup:
code
python manage.py runserver 0.0.0.0:8000
- Access your Django application by navigating to http://your_vps_ip:8000 in a web browser. You should see the Django welcome screen.
8. Deploying for Production
- For production environments, it's recommended to serve your Django app with a more robust web server like Gunicorn and use Nginx as a reverse proxy. Install Gunicorn within your virtual environment:
code
pip install gunicorn
- Run Gunicorn:
bashCopy code
gunicorn myproject.wsgi:application --bind 0.0.0.0:8000
- Configure Nginx to proxy requests to Gunicorn. You'll need to install Nginx and set up a server block (virtual host) that points to your Django app running on Gunicorn.
Conclusion
You've successfully deployed a Django application on yourVPS. This setup is suitable for development and testing. Remember, deploying aweb application in a production environment requires additional configurationfor security and performance, including setting up a secure reverse proxy,configuring SSL, and ensuring your application is properly secured againstcommon vulnerabilities.