Thursday, October 20, 2022

Run a Python Code by Using service file in linux

Python is a popular general-purpose programming language that can be used for a wide variety of applications. It includes high-level data structures and etc.

Django is a Web Application Framework which is used to develop web applications.

In this tutorial we are going to configure a systemd service file to run python code with the help of webserver Apache, where we will also add few entry of reverse proxy that proxy will help to webserver run the code over the domain without using port number.

Step 1) Create Service File

  • The file must have .service extension under /etc/systemd/system in ubuntu

$ cd /etc/systemd/system
$ sudo vim python_project.service

  • Then Add Below content in this file

[Unit]
Description=python_Demo_project
After=multi-user.target
Conflicts=getty@tty1.service

[Service]
Type=simple
WorkingDirectory=/home/ubuntu/Demo_Project/Demo_Project_new
ExecStart=/home/ubuntu/Demo_Project/venv/bin/python3.10 /home/ubuntu/Demo_Project/Demo_Project_new/manage.py runserver 8000
Restart=always
User=ubuntu
Group=ubuntu

StandardOutput=f
ile:/home/ubuntu/Demo_Project/logs/django-output.log
StandardError=file:/home/ubuntu/Demo_Project/logs/django-error.log

[Install]
WantedBy=multi-user.target


Step 2) Enable Newly Added Service file

  • Your system service has been added to your service. Let’s reload the systemctl daemon to read new file. You need to reload this deamon each time after making any changes in in .service file.

$ sudo systemctl daemon-reload

  • Now enable the service to start on system boot, also start the service using the following commands.

$ sudo systemctl enable python_project.service
$ sudo systemctl start python_project.service
$ sudo systemctl status python_project.service

Step 3) Enable following modules proxy for Apache webserver only

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod rewrite
sudo a2enmod lbmethod_byrequests
sudo systemctl restart apache2

Step 4) Make a Newly Virtual Host File under /etc/apache2/sites-available (For Apache)

$ cd /etc/apache2/sites-available

$ sudo vim python_project.conf

  • Then Add Below content.

 

<VirtualHost *:80>

    ServerName example.com
    ServerAdmin webmaster@localhost
#    DocumentRoot /var/www/html

  ProxyPreserveHost On
  ProxyRequests Off
  ProxyPass / http://127.0.0.1:4022/
  ProxyPassReverse / http://127.0.0.1:4022/

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>


Step 5) Enable that Virtual Host & Restart Apache Service (For Apache)

$ sudo a2ensite /etc/apache2/sites-available/python_project.conf
$ sudo apachectl -t
$ sudo service apache2 restart

 

Step 4) Make a Newly Virtual Host File under /etc/nginx/sites-available (For Nginx)

$ cd /etc/nginx/sites-available

$ sudo vim pythonproject

  • Then Add Below content.

 server {
    server_name example.com;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://127.0.0.1:4022";
    }
}

Step 5) Enable that Virtual Host & Restart nginx Service  (For Nginx)

$ sudo ln -s /etc/nginx/sites-available/pythonproject /etc/nginx/sites-enabled/pythonproject

$ sudo service nginx restart

 

Finally Check in your Browser

127.0.0.1  or http://example.com

No comments:

Post a Comment

testing