Install the packages that need to be installed in apt-get first, and then install the packages in pip
Packages that need to be installed in apt-get:
sudo apt-get install python3.6-dev
sudo apt-get install mysql-server
sudo apt-get install mysql-client
sudo apt-get install redis-server
Install nginx: https://www.cnblogs.com/EasonJim/p/7806879.html
Deploy virtual environment
Python 3.6 is now installed in a non-virtual environment
sudo pip install virtualenv #Install the packages required by the virtual environment
virtualenv -p /usr/bin/python3.6 venv #Create a virtual environment with python3.6 (automatically include the corresponding version of pip)
virtualenv -p /usr/bin/python2.7 venv #Create a virtual environment with python2.7 (automatically include the corresponding version of pip, required when installing supervisor)
cd venv #Enter the directory of the virtual environment
source bin/activate #After entering the virtual environment successfully, the command line starts with (venv)
Install pip package
pip install -r file.txt #Install pip related packages, first enter the virtual environment
Install supervisor
. /bin/pip2.7 install supervisor #Install superviosr via pip2.7 (because supervisor does not support python3)
echo_supervisord_conf> supervisord.conf #Generate superviosr configuration file https://blog.csdn.net/wr166/article/details/79051725 Use supervisor to run python3 web project in python2 environment
vi supervisor.conf #After opening, enter the relevant configuration
supervisord -c supervisord.conf #Start the supervisor service through the configuration file
deactivate #Exit the virtual environment # https://blog.csdn.net/charlie_heng/article/details/60573688 Supervisor's simple solution under python3
Firewall/port opening and closing
Turn on the firewall: ufw enable
Turn off the firewall: ufw disable
Open the firewall port: ufw allow port number; such as: ufw allow 5000
Close the firewall port: ufw deny port number;
Restart the firewall: ufw reload
View the status of the firewall port: ufw status
Test whether the port of the remote host is open: telnet 192.168.1.103 80
Start the service and access the external network:
Configuration file in supervisor:
[ program:fws] #Project name
directory= /home/ubuntu/fws #Project location
environment = PATH='home/ubuntu/venv/bin' #Environment
command= /home/ubuntu/venv/bin/python3.6 run.py #Run command
autostart = true #auto start
startsecs = 1
autorestart = true #automatic restart
stopasgroup = true
killasgroup = true
user = ubuntu
stdout_logfile = /home/ubuntu/fws/logs/supevisor.log #output log
stderr_logfile = /home/ubuntu/fws/logs/supevisor_err.log #error log
The content of run.py file:
from fws import app
if __name__ =='__main__':
app.run(host='10.104.136.123',port=5000) #host is the internal network address, the port number should be checked whether the external network can be accessed
Nginx configuration content:
worker_processes 1;
events {
worker_connections 1024;}
http {
include mime.types;
default_type application/octet-stream;
log_format main escape=json '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for" "$request_body"';
access_log /home/ubuntu/fws/logs/nginx.log main;
error_log /home/ubuntu/fws/logs/nginx_err.log;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
location /{
proxy_pass http://10.104.136.123:5000;
proxy_set_header X-Real-Ip $remote_addr;}
location ^~/index.html {
alias /home/web/fws/fws/static/dist/;}
location /static{
alias /home/web/fws/fws/static/;}}}
Then start supervisor, and nginx
Other related command records
pip list --format freeze # pip list
pip install -r file.txt #pip batch installation
pip -V #View the corresponding compiled version of python
whereis python3.6 #View the installation path
https://www.cnblogs.com/yjlch1016/p/8641910.html How to install Python 3.6 on Ubuntu, pip
https://blog.csdn.net/San_South/article/details/80715682 pip error on Ubuntu16.04 ModuleNotFoundError: No module named'pip._internal'
https://blog.csdn.net/wangtaoking1/article/details/51554959 "OSError: mysql_config not found" error when installing Python mysqlclient
https://blog.csdn.net/meteor_s/article/details/79115360 Error record--ImportError: No module named apt_pkg
error: command'x86_64-linux-gnu-gcc' failed with exit status 1 You need to install the corresponding python-dev package according to the python version, such as apt-get install pyton3.6-dev
Recommended Posts