Alicloud server configuration (Ubuntu + nginx + flask)

Time:2021-12-23

Alicloud server configuration (Ubuntu + nginx + flask)

Ubuntu 16.04
Nginx 1.12.0
MongoDB 3.4
Python 3

Environment configuration

Configure FTP service

sudo apt-get install vsftpd

Start vsftpd service:

sudo service vsftpd restart

Install FileZilla for windows, enter the host, user name, password and port, and then connect.

Nginx installation

changenginxInstall the source to ensure that the latest stable version is installed.:

vim /etc/apt/sources.list

add to:

deb http://nginx.org/packages/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/ubuntu/ xenial nginx

Update the source, otherwise an error will be reported

sudo apt-get update

Install nginx:

sudo apt-get install nginx

Start the nginx test:

sudo /etc/init.d/nginx start
#Or
service nginx start

At this point, open the browser to visit your server, and you can see the classic nginx welcome page!
see:Nginx Install

Python related

Installing the python 3 environmentpip

sudo apt-get install python3-pip

Install the required to create a stand-alone Python environmentvirtualenv

pip install virtualenv

Create a python 3 virtual environment under the specified path:

virtualenv -p /usr/bin/python3 py3env

Start the virtual environment:

source py3env/bin/activate

Exit the virtual environment:

deactivate

uWSGI

The configuration is complex, so gunicorn is used instead.

Enter the virtual Python environment:

pip3 install uwsgi

Gunicorn

It is easier to configure with gunicorn. In a virtual environment,pip install gunicorn, install gunicorn and create a new configuration file deploy_config.py, as follows:

import os
bind='127.0. 0.1:8080 '# bound port
Workers = 4 #worker quantity
backlog=2048
debug=True
proc_name='gunicorn.pid'
pidfile='/var/log/gunicorn/debug.log'
loglevel='debug'

Start gunicorn:

gunicorn -c deploy_config.py myapp:app

myappIs the entry Python file name,appIs the name of the flask instance. If the worker related information is output, it indicates that the startup is successful.

Configure nginx

modify/etc/nginx/sites-available/LowerdefalutThe document is as follows:

server {
    listen 80;
    server_ name example. com; #  This is the external domain name of the host machine. You can also use an IP address

    location / {
        proxy_ pass  http://127.0.0.1:8080 # Here is the service address to gunicorn host
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
  }

After configuration, a soft link is sent to/etc/nginx/sites-enabled/defalutBelow

ln -s /etc/nginx/sites-available/defalut /etc/nginx/sites-enabled/defalut

Note: it can also be deleteddefaultFile, create your own configuration file and establish a soft link.

Configure Supervisor

Installation:

apt-get install python-setuptools
easy_install supervisor
echo_supervisord_conf > /etc/supervisord.conf

Add to profile:

[program:myapp]
command=/home/www/myapp/py3env/bin/gunicorn -c /home/www/myapp/deploy_config.py myapp:app
autorstart=true
directory=/home/www/myapp
autorestart=true
startsecs=10
startretries=20

[program:nginx]
command=/usr/sbin/nginx
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
stdout_logfile=/var/deploy/log/nginx.log
stderr_logfile=/var/deploy/log/nginx.err

In case of port occupancy error:

sudo unlink /tmp/supervisor.sock
sudo unlink /var/run/supervisor.sock

Start the supervisor:

supervisord -c /etc/supervisord.conf

Close Supervisor:

supervisorctl shutdown

service crond reload

supervisorctl reload

supplement

Linux commands

command function example
cp Copy files or directories cp file1 file2

Listening port:

lsof -i tcp | grep LISTEN

******************************
sshd       837 root    3u  IPv4   8888      0t0  TCP *:ssh (LISTEN)
vsftpd    4463 root    3u  IPv4  19989      0t0  TCP *:ftp (LISTEN)

Nginx knowledge supplement

tree /etc/nginx/
/etc/nginx/
├── conf.d
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── naxsi_core.rules
├── naxsi.rules
├── naxsi-ui.conf.1.4.1
├── nginx.conf
├── proxy_params
├── scgi_params
├── sites-available
│   └── default
├── sites-enabled
│   └── default -> /etc/nginx/sites-available/default
├── uwsgi_params
└── win-utf
  • foldersites-enabledThe file in issites-availableHard links to files in folders.

  • Profile fromLoaded in sites available, the default configuration file isDefault ` file.

  • nginx.confMaster profile.

  • uwsgi_paremsIs a python related file.

  • fastcgi_parmsIs a PHP related file.

  • Default site directory for nginx/usr/share/nginx/html

Common commands:

Nginx - s stop quick close nginx
Nginx - s quit gracefully closes nginx
Nginx - s reload reload configuration
Nginx - s reopen reopen log file

Get a list of all running nginx processes:

ps -ax | grep nginx

If nginx main processpidby1628, then availablekillCommand sendingQUITSignal to close this process:

Kill - s quit process ID