CodingLad
python

How to Host a Python app on Cloudways (Step-By-Step Guide)

How to Host a Python app on Cloudways (Step-By-Step Guide)
0 views
4 min read
#python

How to Host a Python app on Cloudways (Step-By-Step Guide)

In this article, we will guide you through the process of how to host a Python app on Cloudways (Step-By-Step Guide). Cloudways is a managed hosting platform that simplifies the deployment and management of web applications, including Python apps.

Log in on Cloudways

The first and foremost step is to sign in with your credentials on the Cloudways Platform. If you’re not a Cloudways user, sign up to get your 3-day FREE trial, without even entering your credit card details.

Launch a PHP Application

Launch a PHP Application
  1. To set up your Python application, start by installing a PHP Application on the Cloudways Platform with a single -click setup.
  2. Add your application’s name and other details and then click Launch Now.
  3. Once the application is installed, you can access it using SSH/SFTP to upload your application data. Access the Python environment via the SSH terminal provided on the Cloudways platform. For SFTP, you can use any SFTP client, such as FileZilla, PuTTY, etc. You can also use Git to manage your Python application. This method lets you pull changes directly from your GitHub repository, simplifying the process. The steps of deploying via GitHub and server setup will be discussed next.

Deploying the Application via Git

Cloudways offers a built-in Git deployment feature, eliminating the need for manual cloning via SSH.

Cloudways Git Deployment

Follow these steps to deploy your Python application:

  1. Log in to your Cloudways account.
  2. Navigate to Applications > Deployment via Git.
  3. Generate an SSH Key and add it to your Git repository (e.g., GitHub, GitLab).
  4. Enter your repository SSH URL and authenticate.
  5. Choose the deployment branch and start the deployment process.

Access the Server via SSH

Once your server is up, access it using SSH:

  • Obtain SSH Credentials: Find these in the Cloudways platform under Server Management > Master Credentials.

  • Connect via Terminal:

    ssh master_username@server_ip_address

Set Up Your Python Application

Navigate to the appropriate directory to set up your application:

  • Application Directory:

Applications are typically located at /applications/your_app_name/public_html/. Your application code is typically located at public_html. You can access it if the code is already on the server, uploaded via GitHub, or you can upload it to the server via git clone.

Setting Up Python Virtual Environment (python3-venv)

Since the Cloudways master user does not have sudo privileges, you need to manually set up Python and a virtual environment. Follow these steps:

Create a Python Directory and Install Python 3.9.8

cd ~ && mkdir python
cd python && wget https://www.python.org/ftp/python/3.9.8/Python-3.9.8.tgz
tar xzf Python-3.9.8.tgz && cd Python-3.9.8/
./configure --with-zlib --enable-optimizations && make

Create a Virtual Environment and Activate It

pip install virtualenv
.local/bin/virtualenv --python=python/Python-3.9.8/python venv
source venv/bin/activate

Installing Dependencies

Once the virtual environment is activated, install the necessary dependencies from your requirements.txt file:

pip install -r requirements.txt
  • Request Assistance: If you lack sudo privileges, contact Cloudways support to install system-wide packages.

Testing the Application Before PM2 Setup

Before setting up PM2, test if your Python application is running correctly:

For FastAPI (Uvicorn)

source venv/bin/activate
uvicorn app:app --host 0.0.0.0 --port 8000

For Flask

source venv/bin/activate
python app.py

Visit http://your-server-ip:8000 (for FastAPI) or http://your-server-ip:5000 (for Flask) to check if it's running correctly.

Configuring the Web Server

To route requests correctly, modify the .htaccess file inside the public_html folder:

If .htaccess doesn’t exist:

  • Linux/macOS/WSL:

    touch .htaccess
    vim .htaccess
  • Windows (Git Bash/PowerShell):

    Use:

    notepad .htaccess

    or if using VS Code:

    code .htaccess

Paste the following rewrite rules:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ http://127.0.0.1:8000/$1 [P,L]

Save and close the file.

Installing and Configuring PM2 for Process Management

PM2 is useful for keeping your Python application running in the background. However, since the master user does not have sudo privileges, installing PM2 might require assistance from the Cloudways support team. If you face issues installing PM2, contact Cloudways support and ask them to install it for you.

If You Can Install PM2 Manually

npm install pm2@latest -g

Running a Python Application with PM2

Once installed, start your Python app with PM2:

For FastAPI

pm2 start "uvicorn app:app --host 0.0.0.0 --port 8000" --name my-python-app

For Flask

pm2 start "python app.py" --name my-python-app

Ensure PM2 Runs After Reboot

pm2 save
pm2 startup

Check Running Processes and Logs

pm2 list
pm2 logs my-python-app

Automating Deployment (Optional)

For smoother deployments, consider setting up Git webhooks or using tools like DeployBot to trigger automatic updates.

Conclusion

By following these steps, you can successfully host a Python application on Cloudways with Git deployment, virtual environments, and PM2 process management. If you encounter issues installing PM2 due to permission restrictions, Cloudways support can assist in the installation. Cloudways' managed hosting makes it easier to deploy and scale your applications without dealing with server configurations. Happy coding!