Install and compile the package

  • You do not need to type sudo if you are under root. Add sudo before the command under the subuser.
  1. $ sudo apt-get install build-essential
  2. $ sudo apt-get install libncurses5-dev libncursesw5-dev libreadline6-dev
  3. $sudo apt-get install libdb5.1-dev libgdbm-dev libsqlite3-dev libssl-dev
  4. $ sudo apt-get install libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev

Download and install PYTHon3

  1. Download python3 package $wget – no – check – certificate www.python.org/ftp/python/…

    ‘Download may cause invalid certificates, just install ca-certificates or use –no-check-certificate download to avoid the following error’

  2. Go to download directory $CD download directory /

  3. $tar -zxf python-3.6.3.tgz

  4. Go to python3 $CD python-3.6.3

Compile and install

  1. $vim Modules/ setup.dist is required to resolve PIP package manager dependencies
  2. Uncomment the following line, which is about 361 in the file. Esc :wq Save the changes and exit zlib zlibmodule.c -i(exec_prefix)/lib -lz

4. Customize installation directories

  1. Create the installation directory $mkdir -p /usr/local/python3.6.3

  2. Go to the python3 directory $CD python-3.6.3

  3. Specify the installation directory ./configure –enable-optimizations

  4. Instead of using make directly, the all argument compiles all submodules.

     $ make all
     $ make install
    Copy the code

5. Add PYTHon3 to PATH

  1. Open the ~/.bashrc file
  2. Adding an Execution PathPATH: / usr/local/python3.6.3 / bin to add a new alias information to modify the default use alias Python version of Python = ‘/ usr/bin/python3.6.3’
  3. The configuration file $source. bashrc takes effect
  4. View version: ** Since python3.6.3 has been added above, enter directlyPython2 -v: pythON2.7.3 $python3 -v: python3.6.3 is displayed

Install Nginx

  1. The installation nginx -v

  2. Start nginx $sudo /etc/init.d/nginx start to access: your server IP address such as http://45.45.162.162

    If the figure above appears, it indicates success

  3. Nginx $sudo nginx -s stop

Install uWSGI

  1. Python3 has been installed so $pip3 install uWSgi will be used to install it
  2. View version $UWSgi –version
  3. Installation method 2: download: $wget projects. Unbit. It/downloads/u…
  4. $tar ZXVF uwsgi-latest.tar.gz

Eight, test,

If you have an existing project, you can upload it to the server and manage it in Git. Just clone it with Git.

If: you need to upload project files locally, you can use the SCP command, which is not verbose here. Anyway, we put our project files on the server, and then we can use VirtualEnv to manage the Python environment without going into virtualEnv. This is done directly in flask Demo.

  1. $pip3 install virtualenv

  2. Creating a package cd myproject

  3. Creating a Virtual EnvironmentSource env/bin/activate (2) Install the flask in virtual environmentDeactivate (4) creates the startup file $vim run.py (5) writes to a flask, which the port sets itself. I’m using port 80 here. from flask import Flask app = Flask(name) @app.route(“/”) def hello(): return “Hello World!” if name == “main“: app.run(host=’0.0.0.0′, port=80) (6) esc+wq Save the changes and exit

  4. Run the run.py file (1) and stop NginxIptables -a INPUT -p TCP –dport 80 -j ACCEPT # cd myproject $ python3 run.py

  5. Use other computers and mobile phones 4G access to your server IP address + port. E.g. http://45.32.122.555:80/ appears: Hello World! That’s a success.

Configure Nginx

(1) EntryVim default — — — — — — — — — — — — — — — — – # or configuration directly/etc/nginx/sites-enabled/default: /etc/nginx/sites-enabled/default: /etc/nginx/sites-enabled/default: /etc/nginx/sites-enabled (3) after the last write :wq save and exit server {listen 80; Server_name 45.32.162.255; charset utf-8; client_max_body_size 75M; location / { include uwsgi_params; Uwsgi_pass 127.0.0.1:8000;

uwsgi_param UWSGI_PYHOME /root/myproject/venv; uwsgi_param UWSGI_CHDIR /root/myproject; uwsgi_param UWSGI_SCRIPT run:app; }}parsing listen 80; # server listening portServer_name 45.32.162.255;Write your domain name or public IP address herecharset utf-8; # client_max_body_size 75M;# The difference between GET and POST include uwsgi_params; # Import uWSGi configurationUwsgi_pass 127.0.0.1:8000;[uWSGI] [uWSgi] [uWSGI] [uWSGI] uwsgi_param UWSGI_PYTHON /root/myproject/venv; The path of the Python interpreter is set to its own path. uwsgi_param UWSGI_CHDIR /root/myproject; # project root uwsgi_param UWSGI_SCRIPT run:app; [Flask app] [run] [falsk] [falsk app] [falsk app]

(4) Check whether the configuration file is correct. If the configuration file fails to be detected, check whether the configuration file is missing.

    $ nginx -t
Copy the code

# indicates that the test succeeds

(5) When accessing the Nginx server, you should get 502 Bad Gateway because the request was forwarded by Nginx, but there is no forwarding server to handle the request (uWSGi has not been configured yet).

    $ service nginx start
Copy the code

Configure UWSGi

  1. Ini file (uWSGi supports several configuration file formats: XML, INI, JSON, etc.)(1) Access the project root directoryVim uwsgi.ini (3) :wq save and exit [uwsgi]

    The socket = 127.0.0.1:8000

    plugins = python

    chidir = /root/myproject

    wsgi-file = run.py

    callable = app

    # uWSGi listener port # This line must be added otherwise the request will present an error message: Unavailable modifier: 0 # Project root directory [path according to their own specific path] Flask startup file [I’ll call it run.py] App = falsk

  2. Start test (1) Start NginxCD –Uwsgi uwsgi.ini if all is well, you can see the uWSgi startup information on the terminal.http://45.32.162.255:80The page appears: Hello World!Nginx and UWSGi configurations are successful

    # But there is still some way to go before the actual project is launched, because UWSGi is directly started in the foreground, and the UWSGi process was shut down when our connection terminal was disconnected from the server, so we need UWSGi to run in the background.

    After we connect to the server and start the project, we can use the browser to access it successfully. But once we close the connection to the server and then use the browser to access it, it doesn’t work. Projects run in the foreground which means. When you close the connection to the server, you also close the server command window. Projects running in the foreground are also closed. So we need to run the project in the background.

  3. Background start project (1) Start with nohUP: run the command without hanging up, and use “&” to make your command run in the background (nohup detailed naming parameters please check the official website) Note: do not omit the am& sign.Ps – ef | grep uwsgi find uwsgi process uwsgi. Ini: root 1 0 to 7950? 00:00:00 uwsGi uwsgi.ini Process ID Varies from person to person. The id in this case is 7950. (4) Kill the background UWSgi process kill -9 with the process ID or killall uwsgi killall uWSgi processes.Killall uwsgi (5) Use the browser to access the IP browser. 502 Bad Gateway indicates that the UWSgi program is stopped. Nohup. out logs are generated in the root directory of the project

Use the Supervisor process to monitor

# Supervisor is a powerful operation and maintenance tool written by Python2. Python3 is not currently supported by the Supervisor. You can solve the problem in the following ways.

Can be installed through PIP. Python3 PIP is used here, so the installation will fail. Use the following.

  1. $apt-get Install Supervisor $apt-get Install Supervisor

  2. Enter the Default Configuration file (Modify the Configuration File)Conf press Shift +G to add: files = /etc/supervisor/*. Conf esc:wq

    It’s easy to set up the conf file for each app individually and not have to put it all in the container container when it launches the container container. Error: <class ‘socket.error’>, [Errno 99] Cannot assign requested address: file: The/usr/lib/python2.7 / socket. Py line: 575 error

    Perform: (if not executed under root add sudo) at the front of $supervisord - c/etc/supervisor/supervisord conf $supervisorctl - c / etc/supervisor/supervisord. Added to the conf # here: every time after modify the configuration if appear the error: <class 'socket.error'>, [Errno 2] No such file or directory: file: The/usr/lib/python2.7 / socket. # please follow the above two orders again Enter: the supervisor said shell mode successfully quit: the supervisor shell supervisor > exitCopy the code
  3. Create a script file to generate a default configuration file. vim app.conf

  4. To enter edit mode to add configuration/program: myproject directory = / root/myproject/command = uwsgi – ini/root/myproject/uwsgi ini autostart = true startsecs = 5 autorestart = true startretries = 3 user = root redirect_stderr = true stdout_logfile_maxbytes = 20MB Stdout_logfile_backups = 20 stdout_logfile = / TMP /app.log stopasgroup=false killasgroup=false esc:wq Save the configuration and exit

    [program: myProject] :

    Directory = /root/myproject/ : #

    The command = uwsgi – ini/root/myproject/uwsgi ini: # startup command (equivalent to direct start uwsgi uwsgi. Ini path) is added to the same

    It is known that the container is running normally when it is running and no abnormal exit is seen 5 seconds after it is started. Startretries = 3 (default: 3)

    Redirect_stderr = true: # redirect stderr to stdout, default false stdout_logfile_maxbytes = 20MB: Stdout logfile size, default 50MB stdout_logfile_backups = 20: # stdout logfile backups

    Note: the stdout log file, it’s important to note that it won’t be able to run properly when the specified directory isn’t there, so it’s going to need to be created manually (it’s going to create the log file automatically when it’s configured).

    stdout_logfile = /tmp/app.log

    Stopasgroup and KillasGroup are two useful configuration items. If we use a Rest service like Flask, which usually starts several processes, the Supervisor cannot restart the service if stopasGroup is not enabled. When the main process is started, an error message such as the port is occupied is displayed.

    Stopasgroup =false: The default is false. If set to true, when a process receives a stop signal, it automatically sends the signal to its children. If this configuration item is true, then killasGroup is also implied to be true. For example, when Flask is used in Debug mode, the Flask does not pass stop signals it receives to its child processes, so you need to set this configuration item.

    Killasgroup =false: The default value is false. If this parameter is set to true, when a process receives the kill signal, it automatically sends the signal to its child processes. If the program uses Python’s Multiprocessing, it will automatically stop its child threads.

    $CD ~ $CD /myproject/ $mkdir TMPCopy the code

    Configuration is complete

    For more configuration parameters, please refer to the official website

    The supervisor is written by PYTHon2, but we only need to specify the running Python3 interpreter to run the program.

  5. To enable the configuration to take effect (you need to perform this operation whenever you modify the master configuration file or add a child configuration file), enter firstIt is safe to run the following command to reload the configuration file: $supervisorctl reload

  6. It is easy to run the container shell, such as start app, restart app, etc. supervisorctl $stop myProject $stop myProject $stop myProject $stop MyProject

  7. The above can only be viewed on the console. We need to view the # Add Modify configuration file on the Web interface# if it is found in vim mode it will be modified # if it is not clicked shift+G it will be added to the end

    Inet_http_server port = 45.32.111.111:9001 username = user password = 666666 # If you don't need a password you can comment it on the front of the machine; # #; username = user # ; $CD /etc/supervisor $supervisorctl reload $supervisorctl reload $CD /etc/supervisor $supervisorctl reload  -A INPUT -p tcp --dport 9001 -j ACCEPT $ cd /etc/supervisor $ supervisorctlCopy the code
  8. Start with a browser # Enter your IP and port 45.32.111.111:9001

If you see the Supervisor port above, it means that your project has been successfully started. After that, you can start/stop your project by opening the Supervisor port to monitor and manage your project.

— — — — — — — — — — — — — — — — — what’s missing is insufficient please guide me!!!!!!

Feel good ~ point a praise to play down cough up ~ thank you!!