In this document I have documented the process of deploying the Flask Web site in Ubuntu, which includes user creation, code acquisition, installation of the PYTHON3 environment, virtual environment settings, Uwsgi startup program settings, and Nginx as the front-end reverse proxy. I hope to be of some help to you.
Create a Python Web program dedicated account
123 |
AddUser haseovim/etc/sudoers #将haseo用户加入导sudo用户清单中sudo usermod -a-g www-data Haseo |
Install Python3 and configure the program to run the environment
1. Update the Ubuntu Software Library
123 |
sudo apt-get updatesudo apt-get-y upgradesudo apt-get install build-essential libssl-dev libffi-dev python3-dev #安装一些 The necessary toolkit |
2. Install Python package management tool
python3 -Vsudo apt-get install -y python3-pippip3 install virtualenv
Configure Python programs
1. Create a program directory
mkdir -p /var/www/html/pricing-service
2. Modify Directory Permissions
sudo chown haseo:haseo /var/www/html/pricing-service
3. Create an SSH key that allows users to synchronize GitHub's code
ssh-keygencat ~/.ssh/id_rsa.pub # 复制公钥并增加到GitHub(https://github.com/settings/keys)
4. Copy the code on GitHub
git clone [email protected] .
5. Create a log directory
mkdir log
6. Create a virtual directory
1234 |
PIP3 Install virtualenvpython3-m virtualenv venv # executes in Pricing-service directory ./venv/bin/pip install-r Requirements . Txt./venv/bin/pip Install Uwsgi |
Configure Uwsgi
1. Test if Python directly runs the program to access
vim ~/myproject/wsgi.pyfrom flask import Flaskapp = Flask(__name__)@app.route("/")def hello(): return "
2. Create a WSGI Portal file
vim ~/myproject/wsgi.pyfrom myproject import appif __name__ == "__main__": app.run()
3. Test whether the UWSGI is running properly
uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app
4. Create a UWSGI configuration file
We started to create the Uwsgi configuration file after the previous test.
vim ~/myproject/wsgi.ini[uwsgi]module = wsgi:appmaster = trueprocesses = 5socket = socket.sockchmod-socket = 660vacuum = truedie-on-term = true
5. Create a SYSTEMD file
sudo vim /etc/systemd/system/price_service.service[Unit]Description=uWSGI instance to serve price_serviceAfter=network.target[Service]User=haseoGroup=www-dataWorkingDirectory=/var/www/html/pricing-serviceEnvironment="PATH=/var/www/html/pricing-service/venv/bin"ExecStart=/var/www/html/pricing-service/venv/bin/uwsgi --ini wsgi.ini[Install]WantedBy=multi-user.target
6. Start and enable the WSGI service
sudo systemctl start price_servicesudo systemctl enable price_service
Configure Nginx1. Installing Nginx
apt-get install nginx
2.Nginx Status View and process management
123456 |
Systemctl status Nginxip addr show Eth0 | grep inet | awk ' {print $;} ' | sed ' s/\/.*$//' #获取服务器的IP地址 sudo systemctl start Nginxsudo systemctl Reloa D nginxsudo systemctl disable Nginx # elegant nginx start sudo systemctl enable Nginx when system starts |
3. Configure Nginx Site
vim /etc/nginx/sites-available/defaultserver { listen 8080; #监听IP real_ip_header X-Forwarded-For; set_real_ip_from 127.0.0.1; # 告诉Python程序是谁发送的请求 server_name localhost; location / { # 用户访问的根目录比如 http://www.bihell.com/ include uwsgi_params; # Flask程序需要uwsgi解析 uwsgi_pass unix:/var/www/html/pricing-service/socket.sock; #uwsgi通过这个文件传递信息 uwsgi_modifier1 30; } # 404错误页面配置,下同 error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }}
4. Soft link Nginx sites-enabled
directory
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled
5. Test Configuration
sudo nginx -t
6. Restart Nginx is done
sudo systemctl restart nginx
Deploy your Python Web program to an Ubuntu server