introduction
When developing and deploying web applications, developers often need to deal with issues such as multi-port services, firewall configuration, and production environment optimization. This article will use a practical case to explain in detail how to transition from a development environment (using python3) to a production environment (using WSGI servers such as Gunicorn) and manage multiple ports (such as 5000 and 6000). We will also discuss how to configure firewalls, optimize performance, and provide necessary Java code examples (such as Spring Boot comparison implementations).
1. Flask application basics: development environment operation
During the development stage, we usually run the application directly using the Flask built-in server:
# from flask import Flask app = Flask(__name__) @('/') def home(): return "Hello, World!" if __name__ == '__main__': (host='0.0.0.0', port=6000, debug=True)
Operation mode:
python3
Problem: Flask development server has low performance and is not suitable for production environments, and debug=True will bring security risks.
2. Multi-port service management
Running multiple services (such as ports 5000 and 6000) on the same server is completely feasible, just make sure:
The port is not occupied:
sudo netstat -tulnp | grep -E '5000|6000'
The application is bound to different ports, for example:
# (Port 5000) if __name__ == '__main__': (host='0.0.0.0', port=5000) # (Port 6000) if __name__ == '__main__': (host='0.0.0.0', port=6000)
3. Firewall configuration: open 6000 port
Different Linux system firewall management tools, and different ways to open ports:
(1) UFW (Ubuntu/Debian)
sudo ufw allow 6000/tcp sudo ufw enable sudo ufw status
(2) Firewalld (CentOS/RHEL)
sudo firewall-cmd --zone=public --add-port=6000/tcp --permanent sudo firewall-cmd --reload
(3) iptables (traditional method)
sudo iptables -A INPUT -p tcp --dport 6000 -j ACCEPT sudo service iptables save # or netfilter-persistent save
4. From development to production: Deploy using Gunicorn
Flask development servers are not suitable for production environments, and should use WSGI servers such as Gunicorn:
(1) Install Gunicorn
pip install gunicorn
(2) Run Flask application
gunicorn -w 4 -b 0.0.0.0:6000 app:app
-w 4: 4 working processes (recommended to set to 2*CPU core number + 1)
app:app: app instance in file
(3) Use configuration files (recommended)
Create gunicorn_conf.py:
bind = "0.0.0.0:6000" workers = 4 timeout = 120 accesslog = "gunicorn_access.log" errorlog = "gunicorn_error.log"
run:
gunicorn -c gunicorn_conf.py app:app
5. Nginx reverse proxy optimization
Gunicorn handles dynamic requests, Nginx handles static files, load balancing and HTTPS:
(1) Install Nginx
# Ubuntu/Debian sudo apt install nginx # CentOS/RHEL sudo yum install nginx
(2) Configure Nginx
Create /etc/nginx//flask_app.conf:
server { listen 80; server_name ; location / { proxy_pass http://127.0.0.1:6000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } location /static/ { alias /path/to/static/files; } }
Test and restart:
sudo nginx -t sudo systemctl restart nginx
6. System Service Management (systemd)
Let Gunicorn run in the background and start it up automatically:
Create /etc/systemd/system/:
[Unit] Description=Gunicorn Flask App After= [Service] User=youruser Group=www-data WorkingDirectory=/path/to/app ExecStart=/path/to/venv/bin/gunicorn -c gunicorn_conf.py app:app [Install] WantedBy=
Start and enable:
sudo systemctl daemon-reload sudo systemctl start flaskapp sudo systemctl enable flaskapp
7. Java Comparative Implementation (Spring Boot)
If you are a Java developer, you can use Spring Boot to implement similar functions:
(1) Multi-port management
// Main application (Port 5000)@SpringBootApplication public class App1 { public static void main(String[] args) { (, args); } } // The second application (Port 6000)@SpringBootApplication public class App2 { public static void main(String[] args) { SpringApplication app = new SpringApplication(); (("", "6000")); (args); } }
(2) Production environment deployment
Using java -jar or Docker:
java -jar --=5000 java -jar --=6000
8. Summary and Best Practices
1. Development environment: Use python3 to debug, but do not use it for production.
2. Production environment:
- Using Gunicorn + Nginx
- Disable debug=True
- Use systemd to manage services
3. Multi-port management:
- Make sure the port is not occupied
- Open firewall ports
Comparison: Spring Boot supports multiple ports and has a similar deployment method.
Through the above steps, your Flask applications will be highly available, secure and scalable, suitable for production environments!
This is the article about the full guide to Flask application deployment and multi-port management practice. For more relevant Flask application deployment content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!