Deploying a Flask application on CentOS involves several critical steps, including setting up the server environment, configuring the Flask application, deploying it with a WSGI server like Gunicorn, setting up a reverse proxy with Nginx, and configuring DNS settings to make your application accessible via a custom domain. This guide provides a step-by-step approach to achieve a secure and efficient deployment.
Ensure that your server packages are up to date.
sudo yum update -y
Install Python, pip, and other necessary packages.
sudo yum install -y python3 python3-pip nginx
The Extra Packages for Enterprise Linux (EPEL) repository provides additional packages.
sudo yum install -y epel-release
Virtual environments help manage dependencies for your project.
sudo pip3 install virtualenv
Create and navigate to your project directory.
mkdir ~/myflaskapp
cd ~/myflaskapp
Create a virtual environment named venv and activate it.
python3 -m venv venv
source venv/bin/activate
Within the virtual environment, install Flask and Gunicorn.
pip install flask gunicorn
Create a file named app.py with the following content:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, CentOS!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
Run the Flask app locally to ensure it's working.
python app.py
Visit http://your_server_ip:5000 in your browser. You should see "Hello, CentOS!".
Press Ctrl+C to stop the server.
Run Gunicorn to serve your Flask app.
gunicorn --bind 0.0.0.0:8000 app:app
Visit http://your_server_ip:8000 to confirm it's working. Press Ctrl+C to stop.
Creating a systemd service allows Gunicorn to run in the background and start on boot.
sudo nano /etc/systemd/system/myflaskapp.service
Add the following content to the service file:
[Unit]
Description=Gunicorn instance to serve myflaskapp
After=network.target
[Service]
User=your_username
Group=nginx
WorkingDirectory=/home/your_username/myflaskapp
Environment="PATH=/home/your_username/myflaskapp/venv/bin"
ExecStart=/home/your_username/myflaskapp/venv/bin/gunicorn --workers 3 --bind unix:myflaskapp.sock -m 007 app:app
[Install]
WantedBy=multi-user.target
Replace your_username with your actual system username.
Start and enable the Gunicorn service:
sudo systemctl start myflaskapp
sudo systemctl enable myflaskapp
Check the service status:
sudo systemctl status myflaskapp
Remove the default Nginx configuration and create a new one for your Flask app.
sudo rm /etc/nginx/nginx.conf
sudo nano /etc/nginx/nginx.conf
Add the following content to the Nginx configuration:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream myflaskapp {
server unix:/home/your_username/myflaskapp/myflaskapp.sock;
}
server {
listen 80;
server_name your_domain.com www.your_domain.com;
location / {
proxy_pass http://myflaskapp;
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;
}
}
}
Replace your_username with your actual system username and your_domain.com with your registered domain.
Test the Nginx configuration for syntax errors.
sudo nginx -t
If the test is successful, restart Nginx:
sudo systemctl restart nginx
sudo systemctl enable nginx
Allow HTTP and HTTPS traffic through the firewall.
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Access the DNS management section for your domain through your registrar's control panel.
Add A records to point your domain and subdomains to your CentOS server's IP address.
| Type | Host | Points to | TTL |
|---|---|---|---|
| A | @ | your_server_ip | 3600 |
| A | www | your_server_ip | 3600 |
Replace your_server_ip with your actual server's IP address.
DNS changes may take up to 24 hours to propagate, but they usually update much faster.
Certbot automates the process of obtaining and installing SSL certificates.
sudo yum install -y certbot python3-certbot-nginx
Run Certbot to obtain and install the SSL certificate for your domain.
sudo certbot --nginx -d your_domain.com -d www.your_domain.com
Follow the prompts to complete the installation, including agreeing to the terms of service and choosing whether to redirect HTTP traffic to HTTPS.
Visit https://your_domain.com to ensure that your site is secure.
Certbot automatically renews certificates before they expire.
sudo crontab -e
Add the following line to schedule daily checks:
0 0 * * * certbot renew --quiet
By following this comprehensive guide, you've successfully deployed a simple Flask application on CentOS using Gunicorn as the WSGI server and Nginx as a reverse proxy. Additionally, you've configured DNS settings to make your application accessible via a custom domain and secured your site with an SSL certificate. This setup ensures that your Flask application is robust, scalable, and secure, ready to serve traffic efficiently.