Linux is useless after using nginx configuration

after I wrote a website with the flask framework, I deployed it to the CVM. It has been deployed with gunicorn, but I can only access port 2000 set by the program. Now I want to jump to port 2000 by accessing the default port 80. I used nginx, but when I configured it, it didn"t work. The details are as follows

the configuration file is as follows

server{
    listen 80;
    location / {
        proxy_pass http://localhost:2000;
    }
}

I made a soft link to the configuration file, and the linux is shown as follows:

ll /etc/nginx/sites-enabled/bbs
/etc/nginx/sites-enabled/bbs -> /var/www/bbs/bbs.nginx

bbs.nginx is the configuration file, and then I restart nginx

server nginx restart

I made a mistake when I visited the home page next. The home page shows a 403 error, and a specific page behind the home page shows a 404 error
. It may also be because gunicorn, is turned off because when I open gunicron manually, the code is as follows

gunicorn wsgi --bind 0.0.0.0:2000 --pid /tmp/bbs.pid

at this time, only

is displayed on the shell.
[2018-05-20 01:12:31 +0800] [1311] [INFO] Starting gunicorn 19.8.1
[2018-05-20 01:12:31 +0800] [1311] [INFO] Listening at: http://0.0.0.0:2000 (1311)
[2018-05-20 01:12:31 +0800] [1311] [INFO] Using worker: sync
[2018-05-20 01:12:31 +0800] [1314] [INFO] Booting worker with pid: 1314

in this case, I have no way to enter other commands, so I can only press the Ctrl+ key to exit. The exit prompt is as follows

[1]  + 1311 suspended  gunicorn wsgi --bind 0.0.0.0:2000 --pid /tmp/bbs.pid

only then can I enter other orders. I would appreciate it if I asked the boss to help me point out my mistakes or give me this advice.

Mar.12,2021

run commands in the background without affecting terminal input. You can use the & symbol at the end. For example,

gunicorn wsgi --bind 0.0.0.0:2000 --pid /tmp/bbs.pid &

service, for restarting the service is not server!

service nginx restart

use the following instruction to check that the configuration file is correct before restarting:

service nginx configtest
After

starts, check / var/log/nginx/error.log to see if there is any error message.


< H2 > first of all, your deployment ideas are not clear. < / H2 >
next, try to illustrate it with the simplest example.
< H2 > scene settings: < / H2 >

operating system: ubuntu 18.04 LTS
ignore the firewall and
AppArmor
python the environment is managed using pythonenv , the path is / home/username/.virtualenvs/hello
assume that my Flask application is as follows / home/username/project/hello.py (excerpted from Flask official documentation):

-sharp hello.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'
< H2 > question: how do I make gunicorn run (daemonize) in the background? < / H2 >

running gunicorn, directly on the command line is only suitable for development or debug scenarios, and the production environment requires gunicorn to run (daemonize) in the background.
of course, you can run the following command directly under terminal to force gunicorn to run in background mode (&).

gunicorn --bind /run/gunicorn/socket --pid /run/gunicorn/pid hello:app &

however, this mode has inherent defects, such as making gunicorn dependent on the terminal, that runs it from the point of view of reliability (for example, when the terminal process running it dies, gunicorn also dies) and availability (for example, gunicorn cannot run automatically after a system restart), make this method unsuitable for use in a production environment.
remarks : -- bind parameter value of gunicorn is set to / run/gunicorn/socket instead of 0.0.0.0 code 2000, because the performance of unix socket is higher than tcp port . When nginx and gunicorn are not on the same host, you can only use tcp port .

< H2 > one of the solutions systemd < / H2 >

create the following file / etc/systemd/system/gunicorn.service , with the following contents:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
PIDFile=/run/gunicorn/pid -sharp PID
User=www-data -sharp 
Group=www-data -sharp 
RuntimeDirectory=gunicorn -sharp /run
WorkingDirectory=/home/username/project -sharp 
ExecStart=/home/username/.virtualenvs/hello/bin/gunicorn --pid /run/gunicorn/pid \
          --bind unix:/run/gunicorn/socket hello:app -sharp gunicorn
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

run the following command to start the gunicorn service and make it boot automatically.

sudo systemctl enable --now gunicorn.service

run the following command to view the status of the gunicorn service.

sudo systemctl status gunicorn.service

remarks : in addition to systemd , you can also use supervisor to manage gunicorn (self Google ).

< H2 > nginx Settings < / H2 >
server {
    listen 80;
    server_name www.yourhostname.com;
    location / {
        proxy_pass http://unix:/run/gunicorn/socket;
    }
}
Menu