Next, start the Gunicorn service and enable it to start at system reboot: systemctl start gunicorn.socket Next, reload the systemd daemon with the following command: systemctl daemon-reload Save and close the file then set proper permission to the Django project directory: chown -R www-data:root ~/django_project Unix:/run/gunicorn.sock django_gi:application Save and close the file then create a service file for Gunicorn: nano /etc/systemd/system/rviceĪdd the following lines that match your Django project path: ĮxecStart=/root/django_project/djangoenv/bin/gunicorn -access-logfile -workers 3 -bind To do so, create a socket file with the following command: nano /etc/systemd/system/gunicorn.socket It is a good idea to create a systemd service file for the Gunicorn to start and stop the Django application server. Next, deactivate the Python virtual environment with the following command: deactivate Create a Systemd Service File for Gunicorn Press CTRL C to stop the Gunicorn server. If everything is fine, you should get the following output: Starting gunicorn 20.1.0 You can start the Gunicorn server with the following command: gunicorn -bind 0.0.0.0:8000 django_gi Next, you will need to test whether the Gunicorn can serve the Django or not. Now, go back to your terminal and press CTRL C to stop the Django development server. You should see the Django dashboard on the following page: #Aws postgresql apache django passwordProvide your admin username, password and click on the Login. You will be redirected to the Django login page: Now, open your web browser and access your Django app using the URL. System check identified no issues (0 silenced).ĭjango version 3.2.4, using settings 'django_ttings' You should see the following output: Watching for file changes with StatReloader Now, start the Django development server using the following command. manage.py collectstatic Test the Django Development Server Next, gather all the static content into the directory. Set your admin username and password as shown below: Username (leave blank to use 'root'): admin Next, create an admin user with the following command. Save and close the file then migrate the initial database schema to the PostgreSQL database. STATIC_ROOT = os.path.join(BASE_DIR, 'static/') 'ENGINE': 'django.db.backends.postgresql_psycopg2', Next, edit the settings.py and define your database settings: nano ~/django_project/django_project/settings.pyįind and change the following lines: ALLOWED_HOSTS = Next, run the following command to create a Django project: django-admin.py startproject django_project ~/django_project #Aws postgresql apache django installNext, install the Django, Gunicorn, and other packages with the following command: pip install django gunicorn psycopg2-binary Install and Configure Django Next, activate the Django virtual environment: source djangoenv/bin/activate Next, change the directory to django_project and create a Django virtual environment: cd ~/django_project Next, create a directory for the Django project using the command below: mkdir ~/django_project Next, install the virtualenv package using the following command: pip3 install virtualenv #Aws postgresql apache django upgradeNext, you will need to create a Python virtual environment for the Django project.įirst, upgrade the PIP package to the latest version: pip3 install -upgrade pip \qexit Create a Python Virtual Environment Next, exit from the PostgreSQL shell using the following command: Next, grant some required roles with the following command: ALTER ROLE djangouser Next, create a database and user for Django with the following command: CREATE DATABASE You can install it with the following command:Īpt-get install postgresql postgresql-contrib -yĪfter the installation, log in to PostgreSQL shell with the following command: su - postgres Next, you will need to install the PostgreSQL server on your server. Systemctl enable nginx Install and Configure PostgreSQL Once all the packages are installed, start the Nginx service and enable it to start at system reboot: You can install all the packages with the following command:Īpt-get install python3-pip python3-dev libpq-dev curl nginx -y A root password is configured on your server.įirst, you will need to install Nginx and other Python dependencies on your server.A valid domain name pointed with your server IP.In this case, you can use Gunicorn as a WSGI HTTP server and Nginx as a proxy server to serve your application securely with robust performance. If you want to deploy a Python application on the production environment then you will need a powerful and more secure web server. #Aws postgresql apache django codeIt comes with a development server to test your Python code in the local system. Django is an open-source Python framework that can be used for deploying Python applications.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |