In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how centos uses Nginx to deploy flask applications". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Environmental preparation
Python package installation tool pip: sudo apt-get install pip
Virtualenv: in order to avoid conflicts between the system environment and the application environment, using virtual environments to install application dependencies, virtualenv can create a separate development environment for each python application, installed by sudo pip install virtualenv
Use of virtual environment
Flask project address: / usr/local/flasky, just run the instruction under the project address:
Virtualenv venv
It is customary to use "venv" as the name of the virtual environment, so we have created a folder for the virtual environment, venv, as shown in the red box below:
Next, activate the virtual environment, using only instructions
Source venv/bin/activate
After activating the virtual environment, the command line header will be marked with (venv). After only the virtual environment, all the operations of installing python will be installed in the virtual environment with packages and references, which will not affect the global python environment.
To exit the virtual environment, directly tap:
Deactivate
You can exit the virtual environment.
Configure the flask application environment
Flask application development, will be applied to a lot of templates, "Dog Book" introduces a more convenient way to directly export the flask dependency of the development environment into a txt file, and then use the corresponding instructions in the centos virtual environment to install according to this txt file will be very convenient.
Pip freeze > requerements.txt
This directive exports the text of a file named requirements.txt with the contents shown in the following figure
Then use the following instructions in centos
Pip install-r requerments.txt
This will install a copy consistent with the development environment in the virtual environment.
Note that batch installation instructions must be used in the virtual environment, otherwise they will be installed globally, and the consequences will be painful.
Install uwsgi
There are not many choices for the actual production operation environment of flask, and the more mature ones are [gunicorn] and [uwsgi]. Here we recommend a book, Dong Weiming's "python web Development practice", which talks about these two deployments.
The following content is from Baidu encyclopedia:
Uwsgi is a web server, which implements wsgi protocol, uwsgi, http and other protocols. The role of httpuwsgimodule in nginx is to exchange with the uwsgi server. Wsgi is a web server gateway interface. It is a specification for a web server (such as a nginx,uwsgi server) to communicate with web applications, such as programs written in the flask framework.
The installation instructions are as follows. Be sure to enter the virtual environment and activate it:
Pip install uwsgi
You do not need to use sudo because virtualvenv has no permission requirements.
Upload project files
The linux connection tool used by bloggers is xshell. In a previous article, we talked about how to upload files to the linux host. I will not elaborate here, but show the general project structure and launch the file manage.py.
The tree command can show the file structure in the form of a tree diagram, and can give the parameter to set the depth of the tree. Here the blogger shows two layers.
Installation commands:
Sudo apt-get install tree
The manage.py code is as follows
# codingchangesutf8: codingintoflaskyUnivenv python'''created on 2017/11/9@author: savitar, project:'''import osfrom app import create_appfrom flask_script import manager, shellapp = create_app (os.getenv ('flask_config') or' default') manager = manager (app) def make_shell_context (): return dict (app=app) manager.add_command ("shell", shell (make_context=make_shell_context) @ manager.commanddef deploy (): "" run deployment tasks. " Passif _ _ name__ = ='_ main__': manager.run () # app.run ()
Command line to run the flask project
Python manage.py runserver
The above commands are generally used when debugging in the development environment, or comment out "manager.run ()" and use "app.run ()", which can execute the project directly in pycharm. I will not elaborate here.
Configure uwsgi
We create a new file "config.ini" directly under the root directory of flask, using configuration startup. The contents of the document are as follows
[uwsgi] # address and port used for uwsgi startup socket = 127.0.0.1 home 500 virtual environment directory home = / usr/local/flasky/venv# points to the website root directory chdir = / usr/local/flasky#python startup program file wsgi-file = application variable name for startup within the manage.py#python program callable = number of app# processors processes = number of threads threads = "state monitoring address stats = 127.0 .0.1 919 sets the size of the internal cache for uwsgi packet parsing. Default 4kbuffer-size = 32768
How the configuration file is executed, enter instructions on the command line:
Uwsgi config.ini
Or instead of writing the configuration file (not recommended), enter it directly on the command line
Uwsgi-socket 127.0.0.1 wsgi-file manage.py 5000-wsgi-file manage.py-callable app-process 4-threads 2
The callable=app app here is a variable in the manage.py program file, and the type of this variable is the application class of flask.
This is because the blogger has configured the project to start itself, and there is already a configuration file running, so just configure it according to the normal process. Ctrl+c shutdown program, in the actual project, there may be multiple projects running on our server, we need to start the application with the server and run as a background service is the actual project need, so we need to install another tool to guide the execution of uwsgi
Install supervisor
Supervisor can launch multiple applications at the same time, and the most important thing is that when an application down falls down, it can automatically restart the application to ensure availability.
Sudo apt-get install supervisor
The global configuration file for supervisor is available in the
Open the default configuration file, and on the bottom line we see that the default configuration file loads all configuration files from the / etc/supervisord/ directory
We don't need to modify the default configuration file, we just need to create a new configuration file (named flask_supervisor.conf) in the / etc/supervisord/ directory
The contents of the file are as follows:
[program:flasky] # start the command entry command=/usr/local/flasky/venv/bin/uwsgi / usr/local/flasky/config.ini# the directory where the command program is located, the user name of the user=rootautostart=trueautorestart=true# log address of the command running command stdout_logfile=/usr/local/flasky/logs/uwsgi_super.log
Here command this line of code looks very long, in fact, is before our "uwsgi config.ini" instruction, here is the absolute path, to ensure the accuracy of commands and files, we can also copy this line of code out to execute, the result is ok. The autostart and autorestart parameters ensure that our application can always be started, and the service can be restarted even if the down is dropped.
Start the service
Sudo service supervisor start
Termination of service
Sudo service supervisor stop
Install nginx
Nginx is lightweight, strong performance, occupies less resources, and can well deal with high concurrency reverse proxy software.
Forward proxy and reverse proxy
A forward agent acts as a medium to return resources acquired on the Internet to associated clients. The agent and client are in a local area network and are transparent to the service. Reverse proxy, according to the request of the client, get resources from the back-end server, and then return these resources to the client. The agent and the server are then a local area network, which is transparent to the client. Nginx is the best choice for reverse proxy.
The role of reverse proxy
Improving the io processing ability of dynamic languages
Encryption and ssl acceleration
It's clear.
Load balancing
Cache static content
Compression is supported.
Nginx installation instructions:
Sudo apt-get install nginx
Configure nginx
We find the configuration file for nginx. Instead of modifying the default nginx.conf (path / etc/nginx/nginx.conf) file, we just need to create a new folder (conf.d) under the same folder and then create a new configuration file (flask_ng.conf) under conf.d, as shown below.
The content of flask_ng.conf file is as follows
Server {listen 80; server_name www.cloud-test.com; # public network address location / {include uwsgi_params; uwsgi_pass 127.0.0.1 location 5000; # points to the internal address applied by uwsgi, and all requests will be forwarded to uwsgi processing uwsgi_param uwsgi_pyhome / usr/local/flasky/venv; # to the virtual environment directory uwsgi_param uwsgi_chdir / usr/local/flasky # point to the website root directory uwsgi_param uwsgi_script manage:app; # specify the launcher}}
Start nginx
Sudo service nginx restart
Then the browser accesses the server http://www.cloud-test.com address directly, and the result is as follows
Small case, the solution is to modify the native hosts directly and bind it, as shown in the following figure
And then visit.
View the operation of the application
Supervisorctl is supervisord's command line client tool, using the same configuration as supervisord, so I won't talk about it here. Next, we mainly introduce the common commands of supervisorctl operation:
Enter the command supervisorctl to enter supervisorctl's shell interface (or pure command line), and you can see how the application is running.
You can enter the command below. :
Help # View help
Status # View program status
Stop program_name # closes the specified program
Start program_name # start the specified program
Restart program_name # restart the specified program
Tail-f program_name # View the log of the program
Update # restart the program modified by the configuration file (modified configuration to load the new configuration with this command)
You can also operate directly through the shell command:
Supervisorctl status
Supervisorctl update
This is the end of "how centos uses Nginx to deploy flask applications". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.