When I add project in python manager for my django website displaying me : Project startup failed, please check the project Logs
When I add project in python manager for my django website displaying me : Project startup failed, please check the project Logs and the status is suspended as you can see in the image below.enter image description here I followed these steps :
The "logs" that are mentioned do not seem to be working but I do see these errors while executing:
The dependencies needed to install the project, please wait...
ERROR: Could not find a version that satisfies the requirement uvicom (from versions: none)
ERROR: No matching distribution found for uvicom 2024-11-06 15:16:38
Start installing dependencies Looking in indexes: pypi.org
ERROR: Could not find a version that satisfies the requirement asgiref=3.8.1 (from versions: none)
ERROR: No matching distribution found for asgiret-381
and
(myenv) root@vps-X:/www/wwwroot/myproject# gunicorn myproject.wsgi:application
[2024-11-06 14:07:13 +0100] [203739] [INFO] Starting gunicorn 23.0.0
[2024-11-06 14:07:13 +0100] [203739] [ERROR] Connection in use: ('0.0.0.0', 8000)
[2024-11-06 14:07:13 +0100] [203739] [ERROR] connection to ('0.0.0.0', 8000) failed: [Errno 98]
Here are the steps I followed:
Step 1: Connect to Your Server Use SSH to connect to your server securely. Replace <your-server-ip> with the IP of your server. ssh root@<your-server-ip>
Step 2: Set Up Your Project Directory and Python Environment Create the Project Directory: Organize your project files by creating a dedicated folder. mkdir -p /www/wwwroot/myproject cd /www/wwwroot/myproject Create and Activate a Virtual Environment: Virtual environments help manage dependencies for your project. python3 -m venv myenv source myenv/bin/activate Install Required Packages: After activating the environment, install Django, Gunicorn, and other necessary packages. pip install django gunicorn whitenoise python-dotenv
Step 3: Set Up Your Django Project Clone Your Project or Start a New One: If you have your Django project on GitHub or GitLab, clone it. If not, create a new project. git clone <your-repository-url> . Or, to start a new project: django-admin startproject myproject . Install Project Dependencies: If you have a requirements.txt file, install all the listed packages. pip install -r requirements.txt
Step 4: Configure Environment Variables with a .env File Create and Edit the .env File: Environment variables are used to securely store sensitive data. nano .env Add Your Environment Variables: Add your project’s secret keys and settings. DJANGO_SECRET_KEY='your-secret-key' DJANGO_DEBUG=False EMAIL_HOST_PASSWORD='your-email-password'
Step 5: Update Django settings.py Edit settings.py: Load the environment variables and configure settings for production. from pathlib import Path import os from dotenv import load_dotenv load_dotenv() BASE_DIR = Path(_file_).resolve().parent.parent SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY') DEBUG = False ALLOWED_HOSTS = ['<your-server-ip>', 'example.dz'] # Add your IP and domain # Static files settings STATIC_ROOT = BASE_DIR / 'staticfiles' STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' # Middleware MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', # other middleware ]
Step 6: Prepare Static Files and Migrate the Database Collect Static Files: Django uses this command to gather all static files in a single directory. python manage.py collectstatic Migrate the Database: Apply any pending migrations to your database. python manage.py migrate
Step 7: Set Up Gunicorn as the Application Server Create a Gunicorn Service File: This configures Gunicorn to start automatically on boot. sudo nano /etc/systemd/system/gunicorn.service Add the Service Configuration: Paste the following content, adjusting paths if needed. [Unit] Description=gunicorn daemon After=network.target [Service] User=www-data Group=www-data WorkingDirectory=/www/wwwroot/myproject ExecStart=/www/wwwroot/myproject/myenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/www/wwwroot/myproject/myproject.sock myproject.wsgi:application [Install] WantedBy=multi-user.target Start and Enable Gunicorn: sudo systemctl start gunicorn sudo systemctl enable gunicorn
Step 8: Configure the Website in aaPanel Login to aaPanel: Open https://<your-server-ip>:8888 in your browser and log in to aaPanel. Add a New Website: Go to the Website section. Click Add Site and fill in the details: Domain: example.dz Type: Python Project Project Path: /www/wwwroot/myproject Python Version: Select your version (e.g., 3.x) Framework: Django WSGI: Gunicorn
Step 9: Configure SSL for Your Website Apply for SSL in aaPanel: Go to Website List in aaPanel. Click on your domain. Navigate to SSL and apply for a Let's Encrypt Certificate. Enable Force HTTPS for secure browsing.
Step 10: Check Logs and Test Your Setup Check Gunicorn Logs: Review logs to identify any potential issues with Gunicorn. sudo journalctl -u gunicorn Check aaPanel Logs: Monitor aaPanel logs to ensure there are no server errors. tail -f /www/wwwlogs/example.dz.log
Step 11: Final Security and Permission Checks Set Correct Permissions: Proper permissions help secure your files and ensure your app runs smoothly. sudo chown -R www-data:www-data /www/wwwroot/myproject sudo chmod -R 755 /www/wwwroot/myproject Create Backup Directory: This folder can store backups for your project. mkdir /www/backup/django chmod 755 /www/backup/django Step 12: Set Up Automatic Backups in aaPanel Go to the Backup Section:`your text` Choose Add Backup Task. Select Website and Database to back up. Set a schedule, e.g., daily, to ensure regular backups. Step 13: Monitor Your Site Monitor Resources in aaPanel: Regularly check Monitoring to keep track of your server’s health. Set Up Error Notifications: If supported, configure notifications to stay updated on potential issues.enter image description here