Use Nginx and u under CentOS

A while ago, I wrote a blog program using the flask framework. After I finished writing, I wanted to deploy it to the server. Because I was Xiaobai, I googled a lot of information and talked about bits and pieces. And some places just tell you how to configure it, but the details Why is this matching but did not explain, so I summarized the process from beginning to end.

Deploying the project on the server is simply to let your project run on this server and be able to access your project on the public network.
So if you want to achieve these two points, the work that needs to be done is as follows:

  1. The server environment is built so that the server has the necessary environment for running the project
  2. Upload your project code to the server
  3. Configure server proxy to process requests from the Internet.

1. Server environment construction

Let me talk about the choice of the system first. First exclude the Windows system. Under the Linux system, Ubuntu, Debian, and CentOS are very good.

Ubuntu has strong software source support, but it is more suitable for desktop operating systems. Debian is stable, occupies a small amount of hard disk space and memory, but it has developed too fast, and its security and stability are not as good as CentOS. So I chose CentOS. For the comparison of the three, see http://waynerqiu.com/7/156.html.

The purchased server needs to be operated with a remote connection, and putty is recommended here. I am using CentOS 6.3 x86_64. Different versions may have different software installation addresses.

The Linux system comes with a low version of python, which can be viewed through python -V. The built-in python version will be related to some functions of the system, such as yum.

The flask project requires python2.7 or higher, so we need to install python2.7 or python3.5 in the system.

Download and install Python 2.7.5:

# wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2 

If there is no wget at this time, you can install it through yum install wget

# tar jxvf Python-2.7.5.tar.bz2
# cd Python-2.7.5
#. /configure --prefix=/usr/local
# make && make install

At this time, python2.7.5 is not the python that our system will use (now after typing python -V, the lower version of python will be displayed). So it is necessary to establish a soft connection to make the system default python point to python2.7.5.

# mv /usr/bin/python /usr/bin/python2.4.3//Your python version may be different
# ln -s /usr/local/bin/python27 /usr/bin/python

Using a new version of python will cause yum installation software to report an error, because yum is not compatible with python2.7, so we need to specify the python version used by yum.

# vi /usr/bin/yum

Enter edit mode, change #!/usr/bin/python to #!/usr/bin/python2.7.5

Install related development kits and some packages

# yum groupinstall "Development tools"
# yum install zlib zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

Install pip

# wget https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz --no-check-certificate

Since downloading pip is based on https protocol, it is necessary to add --no-check-certificate after wget url, otherwise it cannot be downloaded.

Unzip and install pip

# chmod +x pip-1.3.1.tar.gz 
# tar xzvf pip-1.3.1.tar.gz 
# cd pip-1.3.1 
# python setup.py install 

In the last step, an error may be reported "ImportError: No module named setuptools" because the setuptools is not installed

Install setuptools

# wget https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg  --no-check-certificate 
# chmod +x setuptools-0.6c11-py2.7.egg 
# sh setuptools-0.6c11-py2.7.egg 

This step may also report an error "zlib not available", you can use #rpm -qa | grep zlib to view the available installation packages, and install the ones that are not installed (we have installed zlib and zlib-devel above)

Run sh setuptools-0.6c11-py2.7.egg again, but still an error is reported? The same error?

So this should not be a problem with zlib, but a problem with python. We need to recompile python2.7.5, and modify the Modules/Setup.dist file before compiling. turn up

# zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz

Remove the comment and recompile python2.7.5

# cd Python-2.7.5
#. /configure --prefix=/usr/local
# make && make install

This way zlib should be usable, go back to the setuptools installation, and then back to the pip installation, this time pip is finally installed. You can install flask-related packages in the global environment (if your server includes only one project in the future), or you can install the package in a virtual environment. Don't worry, upload the project code.

2. Upload your project code to the server

Then you can upload your flask project code to the server, you can use flashFXP or FileZilla to upload.

Here is my upload directory to introduce the next steps. My flask project code is placed in /home/www/WebBlogold (WebBlogold is my project folder). Don't understand the Linux directory structure? You can google it, here is a recommended article http://yangrong.blog.51cto.com/6945369/1288072.

If you have multiple projects on your server, it is recommended that you install a virtualenv virtual environment. Virtualenv can solve the dependency problem of different versions of the same package between projects.

Use pip to install virtualenv virtual environment

pip install virtualenv 

Enter the project directory and type virtualenv venv in the directory, where venv is the name of the virtual environment folder you want to build, and the following two lines will appear as a result

New python executable in venv/bin/python
Installing setuptools, pip...done.

It means that the folder has been created. Use the source venv/bin/activate command to enable the virtual environment. After enabling, there will be a (venv) in front of your current path name.

To install flask related packages in a virtual environment, you can install them one by one, or you can write all the packages in a txt file and install them all at once.

flaskrelated.txt file:

Flask==0.10.1
Flask-Login==0.2.11
Flask-Mail==0.9.1
Flask-Moment==0.4.0
Flask-PageDown==0.1.5
Flask-SQLAlchemy==2.0
and many more

Save them in your project folder, type pip install -r flaskrelated.txt in the virtual environment to install them.

3. Configure server proxy

My server agent is installed globally, of course you can also install it in a virtual environment.

There are two proxies used, nginx and uwsgi. Let me explain first, if you can access your project without nginx, the purpose of using nginx is for security and [load balancing] (https://cloud.tencent.com/product/clb?from=10680). Configure nginx as the front-end proxy, and uwsgi as the back-end proxy server (the front and back ends mentioned here are relative positions and have no actual meaning). When processing requests from the Internet, they must be processed by nginx. The request is then passed to uwsgi, and the project itself can be accessed through uwsgi.

If there is no nginx but only uwsgi server, the Internet request is directly processed by uwsgi and fed back to our project.
Nginx can implement security filtering, anti-DDOS protection and other safe operations, and if multiple servers are configured, nginx can ensure that the server load is relatively balanced.

And uwsgi is a web server that implements the WSGI protocol (Web Server Gateway Interface), http protocol, etc., it can receive and process requests, send responses, etc. So only uwsgi is also possible.

Install uwsgi first

pip install uwsgi

Create a configuration file uwsgiconfig.ini in your project root directory (uwsgi supports multiple configuration file formats, xml, ini, json, etc.)

[ uwsgi]

socket =127.0.0.1:8001//The address and port used when starting the program, usually running the flask project locally,//Address and port is 127.0.0.1:5000,//But on the server, the port is set through uwsgi, and the project is started through uwsgi.//In other words, when uwsgi is started, the project is also started.
chdir =/home/www///Project directory

wsgi-file = manage.py      //The startup file of the flask program is usually run locally by//      python manage.py runserver to start the project

callable = app      //Application variable name enabled in the program

processes =4//Number of processors

threads =2//Number of threads

stats =127.0.0.1:9191//Service address for obtaining uwsgi statistics

Save the configuration file, we can start uwsgi by typing uwsgi uwsgiconfig.ini.

Install nginx: Since nginx is not in the yum installation software source, add it

# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
# yum install nginx

Some commands of nginx

Don't rush to start our nginx proxy server, let's write the nginx configuration file first.

The configuration file of nginx is in the /etc/nginx/conf.d/ folder, and it may also be in /etc/nginx/sites-enabled/default. We open the default configuration file, most of the content is annotated, we need to modify.

events {
 worker_connections  1024;}
http {
 include       mime.types;    
 default_type  application/octet-stream;    
 sendfile        on;    
 keepalive_timeout  65;

 server {
  listen       80;//Default web access port
  server_name  xxx.xxx.xxx.xxx;//Your public network ip
  # charset koi8-r;
  access_log  /home/www/WebBlogold/logs/access.log;//The request log received by the server,//Need to be created under the project folder//logs folder, the same below.
  error_log  /home/www/WebBlogold/logs/error.log;//Error log

  location /{

   include        uwsgi_params;//Here is the imported uwsgi configuration

   uwsgi_pass     127.0.0.1:8001;//Need to match the address of the socket item in the uwsgi configuration file//the same,Otherwise, uwsgi cannot receive the request.

   uwsgi_param UWSGI_PYHOME /home/www/WebBlogold/venv;//python location(Virtual environment)

   uwsgi_param UWSGI_CHDIR  /home/www/WebBlogold;//Project root directory

   uwsgi_param UWSGI_SCRIPT manage:app;//Start the main program of the project(Run locally//This main program can be built in flask//Access your project on the server)}}}
Below is a bunch#, All are comments, don't worry about it.

When starting nginx, if you find an error of "98:Address already in use", don't worry, it's probably because your nginx has already started.

Check the port occupancy by netstat -ntlp to see if port 80 is occupied by your nginx. If you find that other programs occupy the default port 80 of nginx, you can kill these processes and then start nginx.

At this point, we have finally completed all the work and can access our project immediately.

Now, check the nginx configuration file to see if nginx -t is not successfully applied, check the configuration file.

  1. Type nginx to start nginx
  2. Type uwsgi uwsgiconfig.ini to start uwsgi

At this time uwsgi will tell you whether your project has errors. If the local operation is good, there is generally no error.

The error that may be reported is that the import path of the project-related module is incorrect, just modify it. If there is no error, open the browser and enter your server IP address, or if you have the corresponding domain name and resolve to your IP, you can also enter the domain name.

If everything goes well, the page you want is displayed. If not, first check the log files. They are in the logs folder under your project root directory.

Recommended Posts

Use Nginx and u under CentOS
Install and use docker under CentOS 6.8
Installation and use of Mysql under CentOS
KVM installation and preliminary use under CentOS 7.2
CentOs7.3 compile and install Nginx 1.9.9
centos7 kvm installation and use
CentOS7 postgresql installation and use
Centos7 elk7.1.1 installation and use
Install and configure keepalived under CentOS 5.9
Compile and install LAMP under Centos 5.2
Upgrade OpenSSL and OpenSSH under CentOS7
CentOS 6.x compile and install Nginx
CentOS7.2 and Nginx configuration virtual host
CentOS7.3 install iptables and detailed use
Deploy and optimize Tomcat under Centos
Java-JDK installation and configuration under CentOS
Install Python3 and ansible under CentOS8
CentOS7 install and use SQL Server
Install Python3 and Py under CentOS7
Erlang 20.2 installation and deployment under CentOS 7
Use Jexus 5.8.2 to deploy and run Asp.net core under Centos
Install Mono 3.2 and Jexus 5.4 under CentOS 6.3
Compile and install libmodbus library under CentOS7
Tomcat installation and configuration under CentOS 7 (Tomcat startup)
MySQL 8.0 installation, deployment and configuration under CentOS 6/7
Installation and configuration of redis under centos7
Zabbix installation and deployment and localization under CentOS
Deploy Docker and configure Nginx in CentOS
Jenkins installation and deployment tutorial under CentOS 7
Install Mono 2.10.8 and Jexus 5.0 under 32- and 64-bit CentOS 6.0
Centos7 install Nginx
How to install and use Docker on CentOS 7
How to quickly build Nginx server under CentOS
How to install and use Composer on CentOS 8
CentOS 7 installation and configuration graphic tutorials under VMware10
Centos7 installation of PHP and Nginx tutorial detailed
Install svn and configuration through yum under CentOS
Installation and configuration of rsync server under CentOS 6.5
How to install and use Curl on CentOS 8
CentOS7 Docker Nginx deployment and operation detailed explanation
MySQL 8.0 installation and deployment under CentOS, super detailed!
Installing CentOS 6 and SSH configuration under Windows 8 Hyper-V
"Source Production Control" series (2) Using Nginx 1.18 on CentOS 8: Compile, Install and Basic Use
Install centos7 and connect
CentOS MONO nginx running
Installation and cracking of confluence6.3 operation records under Centos
Deploy GitBook under CentOS7
Vmware-install and start Centos 8
Compile Hadoop-2.7.6 under CentOS7.4
Use Rancher to build a K8s cluster under CentOS7
Install mysql5.7 under CentOS7
Vmware-install and start Centos 7
Install ActiveMQ under Centos7
Installation and cracking of Jira7 operation records under Centos
Install PostgreSQL12 under CentOS7
Install CentOS under VMware
Installation and use of GDAL in Python under Ubuntu
[python] python2 and python3 under ubuntu
Use iptables under ubuntu
Deploy JDK+Tomcat8 under CentOS
How to install and use Cockpit on CentOS 8/RHEL 8