Prerequisites:
- Ensure you have SSH access to your cPanel account.
- Have your SSH credentials ready.
1. Access Your cPanel Account via SSH
- Open your terminal (Linux/Mac) or Command Prompt/PowerShell (Windows).
- Connect to your cPanel account using SSH:
ssh username@yourdomain.com
Replace username with your cPanel username and yourdomain.com with your domain name.
2. Set Up Python Application
- Navigate to the Application Directory:
- If you haven’t created a directory for your application, create one and navigate to it:
bash mkdir -p ~/myapp cd ~/myapp
- Create a Virtual Environment:
- Create a Python virtual environment:
bash python3 -m venv venv
- Activate the Virtual Environment:
- Activate the virtual environment:
bash source venv/bin/activate
- Upload Your Project Files:
- If you haven’t uploaded your project files, you can use
scp(from your local machine) to copy them over:bash scp -r /path/to/your/project/files username@yourdomain.com:~/myapp - Alternatively, use an FTP client or the cPanel file manager to upload your files.
- Install Required Packages:
- Ensure you have a
requirements.txtfile in your project directory and install the required packages:bash pip install -r requirements.txt
3. Configure the Application
- Environment Variables:
- Create a
.envfile in your project directory with the necessary environment variables:bash touch .env nano .env - Add your environment variables in the
.envfile:GRAPH_API_TOKEN=your_graph_api_token WEBHOOK_VERIFY_TOKEN=your_webhook_verify_token
- Database Configuration:
- Ensure your Flask app configuration is set to point to your MySQL database. For example, in your
app.py:python app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://username:password@host/database'
- Initialize the Database:
- Run the following commands to create the database tables:
bash python from app import db, app with app.app_context(): db.create_all() exit()
4. Deploy and Run the Application
- Run the Flask Application:
- For development purposes, you can run the Flask app directly:
bash export FLASK_APP=app.py flask run - To keep the application running in the background, use a process manager like
screenortmux, or use a production server likegunicorn.
- Using Gunicorn:
- Install Gunicorn:
bash pip install gunicorn - Run the application with Gunicorn:
bash gunicorn -w 4 -b 0.0.0.0:8000 app:app
5. Set Up Webhook URL in WhatsApp Business API
- Set the Webhook URL:
- Set the webhook URL in the WhatsApp Business API settings to point to your webhook endpoint (e.g.,
https://yourdomain.com/webhook).
- Verify the Webhook:
- Ensure the webhook verification token matches the one set in your
.envfile.
6. Optional: Set Up a Reverse Proxy with Apache or Nginx
- Apache Configuration:
- Open your Apache configuration file and add a reverse proxy configuration:
<VirtualHost *:80> ServerName yourdomain.com ProxyPass / http://127.0.0.1:8000/ ProxyPassReverse / http://127.0.0.1:8000/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> - Restart Apache:
bash sudo systemctl restart httpd
- Nginx Configuration:
- Open your Nginx configuration file and add a reverse proxy configuration:
server { listen 80; server_name yourdomain.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; } - Restart Nginx:
bash sudo systemctl restart nginx
By following these steps, you should be able to set up and run your Positive Chat AI WhatsApp application on cPanel using SSH successfully.