Deploying a complex Django project with Redis, Celery, MySQL, InfluxDB, and Nginx on an Ubuntu server involves several steps. Below is a comprehensive guide to help you through the process:
# list all installed python vertions:
ls /usr/bin/python*
# not recomended - uninstalling python will dammage system files:
# try setting bash command to execute desired .ver or specify in your commands
# like -> 'python3.10 -m venv ... (.etc)'
apt-get purge --auto-remove python3.12
# these will enable multiple versions of python available:
sudo apt-get update
sudo apt-get install -y software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install -y python3.10 python3.10-venv python3.10-dev
# to make python3 execute version 3.10:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
# to make python command exeute python 3.10:
sudo ln -s /usr/bin/python3.10 /usr/bin/python
-
Update the system:
sudo apt-get update && sudo apt-get upgrade -y
-
Install essential system packages:
sudo apt-get install -y python3 python3-pip python3-venv python3-dev libmysqlclient-dev build-essential nginx unixodbc-dev
-
C dependent python pakages require libffi. so mind installing that via command provided on your system:
sudo apt-get install libffi-dev
-
some packages need this packge-config as well - so mind installing and updating it as well on your ubuntu server:
sudo apt-get install pkg-config
-
Install Redis (required for Channels and Celery):
sudo apt-get install -y redis-server sudo systemctl start redis sudo systemctl enable redis
-
Install MySQL (if not already installed):
sudo apt-get install -y mysql-server sudo mysql_secure_installation
-
Install InfluxDB (for logs):
sudo apt-get install -y influxdb sudo systemctl start influxdb sudo systemctl enable influxdb
-
Add Microsoft's repository and install ODBC drivers:
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add - curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list sudo apt-get update sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18 sudo ACCEPT_EULA=Y apt-get install -y mssql-tools18
-
Add
mssql-tools
to your PATH:echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc source ~/.bashrc
-
Install
unixodbc-dev
(if not already installed):sudo apt-get install -y unixodbc-dev
-
Create a virtual environment:
python3 -m venv /path/to/your/venv
-
Activate the virtual environment:
source /path/to/your/venv/bin/activate
-
Install the project using
pip
(if you have asetup.py
orsetup.cfg
):pip install .
-
Alternatively, install from
requirements.txt
(if available):pip install -r requirements.txt
-
Install Hypercorn (ASGI server):
pip install hypercorn
-
Create a
.env
file:touch /path/to/your/project/.env
-
Fill in the
.env
file with necessary environment variables. Example:DJANGO_SECRET_KEY=your_secret_key DB_NAME=your_db_name DB_USER=your_db_user DB_PASSWORD=your_db_password DB_HOST=your_db_host DB_PORT=your_db_port REDIS_URL=redis://your_redis_host:6379/0 INFLUXDB_HOST=your_influxdb_host INFLUXDB_PORT=your_influxdb_port INFLUXDB_USER=your_influxdb_user INFLUXDB_PASSWORD=your_influxdb_password MSSQL_DSN=your_mssql_dsn
or you can use a json file to fill in information in there. then you can write a .py script that converts data from json file to .env file
python3 env_creator.py
-
Run the
env_gen
script (if available) to generate environment variables from a JSON file:python /path/to/your/project/env_gen.py
-
Log in to MySQL:
sudo mysql -u root -p
-
Create a database and user:
CREATE DATABASE your_db_name; CREATE USER 'your_db_user'@'localhost' IDENTIFIED BY 'your_db_password'; GRANT ALL PRIVILEGES ON your_db_name.* TO 'your_db_user'@'localhost'; FLUSH PRIVILEGES;
-
Run Django migrations:
python manage.py migrate
-
make sure you have the influxDB client installed:
sudo apt install influxdb-client
-
Log in to InfluxDB:
influx
-
Create a database and user:
CREATE DATABASE your_influxdb_name; CREATE USER your_influxdb_user WITH PASSWORD 'your_influxdb_password' WITH ALL PRIVILEGES;
-
Use the project's Nginx configuration script (if available):
python /path/to/your/project/nginx.py
-
Alternatively, manually configure Nginx:
- Create an Nginx configuration file:
sudo nano /etc/nginx/sites-available/your_project
- Example configuration for ASGI and WebSockets:
server { listen 80; server_name your_domain_or_ip; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } location /ws/ { proxy_pass http://127.0.0.1:8000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } location /static/ { alias /path/to/your/project/static/; } location /media/ { alias /path/to/your/project/media/; } }
- Enable the configuration:
sudo ln -s /etc/nginx/sites-available/your_project /etc/nginx/sites-enabled/
- Test and restart Nginx:
sudo nginx -t sudo systemctl restart nginx
- Create an Nginx configuration file:
-
Run Hypercorn (ASGI server):
hypercorn your_project.asgi:application --bind 127.0.0.1:8000
-
Use the project's
run.sh
script (if available):bash /path/to/your/project/run.sh
-
Create a Celery service file:
sudo nano /etc/systemd/system/celery.service
-
Example configuration:
[Unit] Description=Celery Service After=network.target [Service] User=your_user Group=your_group WorkingDirectory=/path/to/your/project ExecStart=/path/to/your/venv/bin/celery -A your_project worker --loglevel=info Restart=always [Install] WantedBy=multi-user.target
-
Start and enable Celery:
sudo systemctl start celery sudo systemctl enable celery
- Check logs for Nginx, Hypercorn, and Celery.
- Set up monitoring tools like
supervisord
for process management.
[Unit]
Description=Celery Service
After=network.target
[Service]
Type=forking
User=your_user
Group=your_group
WorkingDirectory=/path/to/your/django/project
ExecStart=/path/to/your/virtualenv/bin/celery -A your_project worker --loglevel=info
Restart=always
[Install]
WantedBy=multi-user.target
```bash
```bash
sudo systemctl enable celery.service
sudo systemctl start celery.service
By following these steps, you should be able to deploy your ASGI-based Django project with Channels, WebSockets, and Hypercorn on an Ubuntu server, including support for Microsoft SQL Server.