Alicloud server configuration (Ubuntu + nginx + flask)


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 xenial nginx
deb-src 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
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:



The configuration is complex, so gunicorn is used instead.

Enter the virtual Python environment:

pip3 install uwsgi


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

import os
bind='127.0. 0.1:8080 '# bound port
Workers = 4 #worker quantity

Start gunicorn:

gunicorn -c 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 # 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


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

Add to profile:

command=/home/www/myapp/py3env/bin/gunicorn -c /home/www/myapp/ myapp:app


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


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/
├── 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