简单nginx反代方法, flask app, 多个app等

We are going to build a simple and smooth process using the following steps:

  1. if you don’t have uwsgi installed, try this on ubuntu:
 sudo pip3 install uwsgi
  1. locate your flask project folder, if you don’t know how to create a a flask app, check this link:
    flask app creation
    https://datasciencebyexample.com/2021/09/29/2021-09-29-1/

  2. create a uswgi init file inside your flask project, for example, you can call it myproject.ini

[uwsgi]
module = main:app

master = true
processes = 2

socket = myproject.sock
chmod-socket = 660
vacuum = true

die-on-term = true
  1. Let’s then create the systemd service unit file. Creating a systemd unit file will allow Ubuntu’s init system to automatically start uWSGI and serve the Flask application whenever the server boots.

Create a unit file ending in .service within the /etc/systemd/system directory to begin, for example

sudo emacs /etc/systemd/system/myproject.service

inside the file:

[Unit]
Description=uWSGI instance to serve myproject
After=network.target

[Service]
User=<your user name>
Group=www-data


WorkingDirectory=<your flask app directory>

ExecStart=/usr/local/bin/uwsgi --ini myproject.ini

[Install]
WantedBy=multi-user.target

Then type these in the command line:

sudo systemctl start myproject
sudo systemctl enable myproject

you can check the service status

sudo systemctl status myproject

if any changes to the project, just do this to reflect new web app:

sudo service myproject restart
  1. install the nginx if you don’t have one
sudo apt update
sudo apt install nginx

At the end of the installation process, Ubuntu (18.04) will start Nginx. The web server should already be up and running.

We can check with the systemd init system to make sure the service is running by typing:

systemctl status nginx
  1. Begin by creating a new server block configuration file in Nginx’s sites-available directory. Let’s call this myproject to keep in line with the rest of the guide:
sudo emacs /etc/nginx/sites-available/myproject

inside the file:

server {
    listen 80;
    server_name englishlearningbyexample.com www.englishlearningbyexample.com;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:<your flask app dir>/myproject.sock;
    }
}

above I was using
www.englishlearningbyexample.com
as the example for server_name, but you should replace with yours.

  1. final steps

To enable the Nginx server block configuration you’ve just created, link the file to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

then do

sudo systemctl restart nginx

now you should see the website running

If you encounter any errors, trying checking the following:

sudo less /var/log/nginx/error.log: checks the Nginx error logs.
sudo less /var/log/nginx/access.log: checks the Nginx access logs.
sudo journalctl -u nginx: checks the Nginx process logs.
sudo journalctl -u myproject: checks your Flask app’s uWSGI logs.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值