How to to deploy python app sepratly on same server (previous one ) /root/demo - Deploy Python app on Ubuntu Server using ssh Terminal - by Vasu Birla
hello friends , am Vasu Birla and Today we will deploy Python App..
in previous post we have deployed NODE js app on 0.0.0.0 (your server IP) and
acess it using http://0.0.0.0/
Now If you want to deploy a Python app separately on the same server,
in the /root/demo directory, and access it using http://0.0.0.0/demo,
you can use Apache2 and mod_wsgi to host the Python app.
Additionally, you can configure Apache2 to proxy the requests for the
/demo path to your Python app without affecting the Node.js app hosted at the root.
First of all put your python project folder on root location of server ,
using putty with cloning git repo or using FTP server and direct paste folder
(in previous post we aleady discussed it)
lets suppose your folder name is demo .. location would be /root/demo
install necessery things to support your python app . like pip install or django ..etc
as well install in virtual enviorment also .. -> python3 -m venv venv
-> source venv/bin/activate
-> pip install django
-> pip install -r requirements.txt
-> deactivate
Step 1: Install mod_wsgi
First, make sure mod_wsgi is installed on your server:
-> sudo apt update
-> sudo apt install libapache2-mod-wsgi-py3
Step 2: Set Up the Python App
Place your Python app code inside the /root/demo directory. Since you mentioned that there is a manage.py file, I assume you are using Django. Make sure you have installed Django and other required dependencies.
Step 3: there are two ways
----------------------(i) . with Unique Port ---------------------------------
in settings.py you have to allow host either with server IP or with "*" for
all Internal Hosts
opeen settings.py and put this ->
ALLOWED_HOSTS = ['*']
after doint this verify it using
Verify the Setup
in browser access -> http://YourServerIP:8000/ in my case (http://62.72.5.123:8000/)
and here we go ..you can see your project Live ..
//------------------------------------------------------------------------------
---------------------- (ii). With Apache2 ---------------------------------
Modify Apache2 Configuration for Node.js App
Edit the virtual host configuration for the Node.js app (puno):
run
-> sudo nano /etc/apache2/sites-available/puno.conf
note - puno.conf is already made for node js app .. but we have to add seprate host for /demo
fot following code in puno.conf
#puno.conf
<VirtualHost *:80>
ServerName 62.72.5.123
# Proxy /demo to Python app
ProxyPass /demo http://127.0.0.1:8000/demo
ProxyPassReverse /demo http://127.0.0.1:8000/demo
# Exclude all sub-routes of /demo from being proxied
<LocationMatch "^/demo($|/)">
ProxyPass "!"
</LocationMatch>
# Proxy other requests to Node.js app
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>
Enable the Apache2 Configuration and Restart Apache2
Enable the virtual host configuration and restart Apache2:
-> sudo a2ensite puno.conf
-> sudo service apache2 restart
: Verify the Setup
Now, accessing http://0.0.0.0/demo/ should route requests to your Python app (/demo), and accessing http://0.0.0.0/ should route requests to your Node.js app (kilvishNodeapp).
//---------------------------------------------------------------------------------
Now for keeping alive this Python App without terminal process
we will use same pm2 pakage (out node app is already running with pm2 in previous post)
Step 1: Ensure the Virtual Environment and Install Django
Make sure you have set up the Python virtual environment for your Django app (demo) and
installed Django within the virtual environment as described before.
if you didnt install require pakages in virtual env yet .. follow below commands
go to puroject folder location (in my case its demo) run following command to make virtual env
-> cd /root/demo
-> python3 -m venv venv
-> source venv/bin/activate
-> pip install django
-> pip install -r requirements.txt
-> deactivate
Step 2: Start the Python App with PM2
Start the Python app (demo) using PM2's process file format:
Note - here /root/demo/venv/bin/python is my virtual env path .. you have to put yours
run following
-> pm2 start manage.py --name "demo" --interpreter /root/demo/venv/bin/python -- runserver 0.0.0.0:8000
-> pm2 save
-> sudo service apache2 restart
---------------------- Completed -------------------------
Comments
Post a Comment