Integrated installation of Nginx+UWSGI+Django

Posted by hookit on Thu, 27 Jan 2022 06:48:38 +0100

1. Nginx+openssl installation:
1, Download dependent files pcrenginx
    1,pcre: 
        PCRE A library is a set of functions that use Perl 5 Regular expression pattern matching is implemented with the same syntax and semantics.PCRE Have your own native API,And a group with POSIX regular expression  API Corresponding wrapper function.
        PCRE Libraries are free, even for building proprietary software. Download address of the latest version on the official website: https://ftp.pcre.org/pub/pcre/


    2,The above dependency software download and storage directory(/usr/local/src)And download link address
        wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
        
    3,nginx:  
        nginx[engine x]It's a HTTP And reverse proxy server, a mail proxy server and a general TCP / UDP Proxy server.
        Latest download address on the official website: http://nginx.org/en/download.htm
        wget http://nginx.org/download/nginx-1.16.1.tar.gz
        

2, Software installation
    All the software that needs to be installed has been downloaded/usr/local/src/catalogue
    1,Installation and compilation environment
        yum install -y gcc gcc-c++ pcre-devel zlib zlib-devel openssl openssl-devel
    
        
    2,pcre install
        cd /usr/local/src/
        tar zxf pcre-8.42.tar.gz
        cd pcre-8.42
        ./configure --prefix=/usr/local/pcre
        make
        make install
        
        You can ignore the following warning
        libtool: warning: relinking 'libpcreposix.la'
        libtool: warning: relinking 'libpcrecpp.la'
        
    
        
    3,nginx Install and store the software in /usr/local/src Under the directory
        1,cd /usr/local/src/
            tar zxf nginx-1.16.1.tar.gz
            groupadd nginx
            useradd -g nginx nginx -s /bin/false
            mkdir /usr/local/nginx
            cd nginx-1.16.1
            ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_sub_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre=/usr/local/src/pcre-8.42
            make
            make install
            
            
        2,Description of compilation parameters:
            1)see nginx The startup configuration file is missing a dynamic file command
                ldd $(which /usr/local/nginx/sbin/nginx)
            2)If missing, use the following to find the missing number.so Dynamic file ln -sv solve
                find / -type f -name "libprofiler.so*" Find out if the file exists
                ln -sv /usr/local/lib/libprofiler.so.0.4.8 /lib64/libprofiler.so.0
                ln -sv /usr/local/lib/libunwind.so.8.0.1 /lib64/libunwind.so.8
            3)View what has been edited nginx modular
                /usr/local/nginx/sbin/nginx -V
                The full path command used here/usr/local/nginx/sbin/nginx 
            
            4)nginx Edit installation parameter description

                1)--prefix=/usr/local/nginx
                    Define the installation directory. The default is/usr/local/nginx
                    
                2)--user=nginx 
                    Set the name of the non privileged user whose credentials will be used by the worker process. After installation, the configuration file can be nginx.conf Used in user Command change name. The default user name is nobody. 
                    
                3)--group=nginx
                    Sets the name of the group whose credentials will be used by the worker process. After installation, the configuration file can be nginx.conf Used in user Command change name. By default, the group name is set to the name of a non privileged user.
                    
                4)--with-http_realip_module
                    Enable HTTP_Realip Module, which is used to modify the client in the client request header IP Address value, which is generally used in reverse proxy, will be the real client IP To the back-end application server. This module is not built by default
                    
                5)--with-http_stub_status_module
                    Enable HTTP_Stub_Status Module, status information statistics module, used to return a Nginx Status information statistics page, which can be accessed by administrators Nginx Request processing, current connection
                    Waiting for connection and other statistics are generally used for monitoring Nginx Operation status of the. This module is not built by default.
                    
                6)--with-http_sub_module
                    Enable http_sub Module, which is a filter used to modify the content of the response and replace a specified string with another string. This module is not built by default.
                    
                7)--with-http_ssl_module 
                    Allows you to build a module that will HTTPS Protocol support added to HTTP The server. This module is not built by default. need OpenSSL Library to build and run this module.
                    
                8)--with-http_gzip_static_module 
                    Enable http_gzip_static Module for compressing static content into".gz"It is a pre compressed file with file extension and cached locally. In response, this file will be sent to replace the ordinary file.
                    The advantage of using this module is that there is no need to( Gzip)The file needs to be processed and compressed during each transmission. In use Squid+Nginx Environment, when Nginx Enable( Gzip)When the content transmission compression function,
                    in use Squid3.0 When setting up the environment in previous versions, you will find that, Squid The compressed state is not returned to the client, which is caused by not enabling this module. This module is not built by default.
                    
                9)--with-pcre=/usr/local/src/pcre-8.38
                    Enable PCRE Use of library. PCRE Library is a Perl Libraries, including Perl Compatible regular expressions. If you installed the source code PCRE Library, you need to use this setting PCRE Directory path of the library.    
                    
                    
                10)--with-openssl=/usr/local/src/openssl-1.1.1d 
                    If you installed the source code OpenSSL Library, you need to use this setting OpenSSL Directory path of the library.
                    
                    1,openssl: 
                        OpenSSL It is a powerful commercial full-featured toolkit, which is suitable for transport layer security( TLS)Secure socket layer( SSL)agreement. It is also a general encryption library.
                        Download address of the latest official version: https://www.openssl.org/source/
                    
                    2,openssl download
                        wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz
                    
                    3,openssl install
                            cd /usr/local/src/
                            tar zxf openssl-1.1.1d.tar.gz
                            cd openssl-1.1.1d
                            ./config 
                            make
                            make install
                    
                11)--with-zlib=/usr/local/src/zlib-1.2.11 
                    If you installed the source code Zlib Library, you need to use this setting Zlib Directory path of the library. When enabled HTTP_Gzip Module needs the support of this library.
                    1,zlib: 
                        A large, beautiful but elegant and unobtrusive compression library (also free, not mentioned, not subject to patent restrictions)
                        Latest download address on the official website: https://zlib.net/
                    
                    2)zlib download
                        wget https://zlib.net/zlib-1.2.11.tar.gz
                    
                    3,zlib install
                        cd /usr/local/src/
                        tar zxf zlib-1.2.11.tar.gz
                        cd zlib-1.2.11
                        ./configure 
                        make
                        make install

2. Edit nginx startup script

vi /etc/init.d/nginx

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
.  /etc/rc.d/init.d/functions
# Source networking configuration.
.  /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0

# Start nginx daemons functions.
start() {
    if [ -e $nginx_pid ];then
        echo "nginx already running...."
        exit 1
    fi
    echo -n $"Starting $prog: "
    daemon $nginxd -c ${nginx_config}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
    return $RETVAL
}

# Stop nginx daemons functions.
stop() {
    echo -n $"Stopping $prog: "
    killproc $nginxd
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}

reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    reload)
        reload
        ;;
    restart)
        stop
        start
        ;;
    status)
        status $prog
        RETVAL=$?
        ;;
        *)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
    exit $RETVAL

nginx startup script add permissions
chmod 755 /etc/init.d/nginx

nginx setting startup
chkconfig nginx on

3. Python 3 installation


1)Python3 Download address:
wget https://www.python.org/ftp/python/3.6.10/Python-3.6.10.tar.xz


2)Environment compilation
yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc gcc-c++ openssl-devel


3)python install
decompression tar.xz First, we need to xz Command installation yum install -y xz
Decompression: xz -d Python-3.6.10.tar.xz
Unzip again: tar -xf Python-3.6.10.tar
mkdir /usr/local/python3.6/
cd Python-3.6.10
./configure --prefix=/usr/local/python3.6/ --enable-optimizatio --with-openssl=/usr/bin/openssl
make -j4
make install

4)Establish software connection generation python3 and pip3
ln -s /usr/local/python3.6/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3.6/bin/pip3.6 /usr/bin/pip3

5)set up python3 environment variable
export PATH=$PATH:/usr/local/python3.6/bin/
source /etc/profile

4. ipython installation

ipython is a python interactive shell, which is much easier to use than the default python shell and supports variables Automatic completion , auto indent,

It supports bash shell commands and has built-in many useful functions and functions. Learning ipython will enable us to use python with a higher efficiency.

At the same time, it is also the best platform for scientific computing and interactive visualization using Python.

  pip3 install ipython

Note: in case of the following picture problems

Solution: manually set the source (- i - url) after the pip command. The Douban source is manually specified here

pip3 install ipython -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

Change pip source

Using domestic pip source, the download module will be faster

Common pip sources are:

Watercress: https://pypi.douban.com/simple

Alibaba cloud: https://mirrors.aliyun.com/pypi/simple

Accelerated installation command:

pip install module name – i https://pypi.douban.com/simple

Make the pip source permanent:

Linux system:

1. Create under the user root directory pip hide directory file

      mkdir ~/.pip

2. Enter PIP hide directory create pip Conf configuration file

      cd ~/.pip && touch pip.conf

3. Pip Conf configuration file content addition

      [global]

      index-url = http://pypi.douban.com/simple

      [install]

      use-mirrors =true

      mirrors =http://pypi.douban.com/simple/

      trusted-host =pypi.douban.com

5. Install the virtual environment. When using django, you can create different application development and use the python modules you need

1) install virtual environment command
    pip3 install virtualenv
2) using virtual environment commands

    cd /usr/local
    /usr/local/python3.6/bin/virtualenv -p python3 web
3) take effect of the created virtual environment and enter the virtual environment web
    source web/bin/activate

4) exit virtual environment command

    deactivate

Note: virtual environment can virtualize pyhton into multiple environments, and one environment can be used for one project.

6. Install Django

1)Here we go python virtual environment
    source /usr/local/web/bin/activate
2)install django,
    pip3 install django==2.1.7
    Note: diango==2.1.7 This represents installation django Which version. diango==You can modify it to what you want to install django edition

3)Create a django project,
    django-admin.py startproject myweb
  Note: django project Created to/usr/local/nginx/html/Under the directory
4)stay myweb Create application directory under directory python manage.py startapp blog 5)start-up django project 1)python manage.py runserver 8000 2)python manage.py runserver IP:8000 Note: 1)The current access path is http://127.0.0.1:8000 2)The current access path is http://IP:8000 runserver Access can be added later IP:8000 And modify ports

7. UWSGI installation


1)uwsgi install

    pip3 install uwsgi

2)uwsgi Configuration file parameter writing

  mkdir /etc/uwsgi

  vim /etc/uwsgi/uwsgi.ini

[uwsgi]
#User name and user group to start uwsgi
uid = root
gid = root

#Directory of python virtual environment
home=/usr/local/web/
#Specify the path where wsgi is located
wsgi-file=/usr/local/nginx/html/myweb/myweb/wsgi.py
#Project directory
chdir=/usr/local/nginx/html/myweb
#The application of the specified project refers to myweb
module=myweb.wsgi:application
#Set log directory
daemonize=/var/log/uwsgi.log
#Specifies the pid file path at startup
pidfile = /var/run/uwsgi.pid
#Specify the file path of the sock (used by nginx) 
socket=/tmp/uwsgi.sock
#Start the main process to manage other processes. Other uwsgi processes are sub processes of the master process. If you kill the master process, it is equivalent to restarting all uwsgi processes
master = true
#The number of processes is the same as that of processes  
workers=2
#Enable thread
enable-threads=true
#Number of threads
threads = 4
#Set the maximum number of seconds to wait for the end of a work subprocess in a smooth restart (restart until the received request is processed)
#This configuration will cause the work process to be forcibly terminated if the end time of the work process exceeds 8 seconds during the smooth restart of the work sub process (ignoring the previously received request and ending directly)
reload-mercy = 8
#Automatically remove unix Socket and pid files when the service stops
vacuum=true
#Serialize the accepted content, if possible
thunder-lock=true
#Set self interrupt time
harakiri=30
#Set buffer
post-buffering=4096

Note: if multithreading is set in django code, if multithreading is not enabled in uwsgi configuration file, parameter configuration is not supported.

3) uwsgi start parameter command
uwsgi --ini /etc/uwsgi/uwsgi.ini
4) Edit uwsgi startup script
vim /etc/init.d/uwsgi

#!/bin/bash
# chkconfig: 2345 75 10
DESC="uwsgi daemon"
NAME=uwsgi
DAEMON=/usr/local/python3.6/bin/uwsgi #uwsgi path under Python 3
CONFIGFILE=/etc/uwsgi/$NAME.ini       #Specified uwsgi profile
PIDFILE=/var/run/$NAME.pid            #pid path in configuration file
SCRIPTNAME=/etc/init.d/$NAME          #Path where the startup script is located
FILEOFILE=/tmp/uwsgififo

#set -e
#[ -x $"DAEMON" ] || exit 0

do_start(){
        if [ ! -f $PIDFILE ];then
                $DAEMON $CONFIGFILE || echo -n "uwsgi runing"
        else
                echo "The PID is exist..."
        fi
}


do_stop(){
        if [ -f $PIDFILE ];then
                $DAEMON --stop $PIDFILE || echo -n "uwsgi not running"
                rm -fr $PIDFILE
                echo "$DAEMON STOPED."
        else
                echo "The $PIDFILE doesn't found"
        fi
}

do_reload(){
        if [ -p $FILEOFILE ];then
                echo w > $FILEOFILE
        else
                $DAEMON --touch-workers-reload $PIDFILE || "uwsgi can't reload"
        fi
}

do_status(){
        ps aux |grep $NAME|grep -v "/bin/bash"
}

case "$1" in
        status)
                echo -en "Status $NAME:\n"
                do_status
                ;;
        start)
                echo -en "Starting $NAME:\n"
                do_start
                ;;
        stop)
                echo -en "Stopping $NAME:\n"
                do_stop
                ;;
        restart)
                echo -en "Restarting $NAME:\n"
                do_stop
                do_start
                ;;
        reload|graceful)
                echo -en "Reloading $NAME:\n"
                do_reload
                ;;
        *)
                echo "Usage $SCRIPTNAME {start|stop|reload|status}"
                exit 3
esac
exit 0

8. Set nginx+uwsgi integration so that nginx can access django applications

Modify nginx Conf configuration file, add under server

 location / {
            #root   html;
            index  index.html index.htm;
            # Import an Nginx module, which is used to communicate with uWSGI
            include uwsgi_params;
            #uwsgi_pass 127.0.0.1:9090;
            #uwsgi_param UWSGI_SCRIPT  myweb.wsgi;
            #uwsgi_param UWSGI_CHDIR /usr/local/nginx/html/myweb;
            client_max_body_size 35m;
            uwsgi_cache_valid 1m;
            uwsgi_temp_file_write_size 64k;
            uwsgi_busy_buffers_size 64k;
            uwsgi_buffers 8 64k;
            uwsgi_buffer_size 64k;
            uwsgi_read_timeout 300;
            uwsgi_send_timeout 300;
            # Set connection uWSGI timeout
            uwsgi_connect_timeout 60;
            # The specified uwsgi's sock file will directly throw all dynamic requests to him
            uwsgi_pass unix:/tmp/uwsgi.sock;
        }

Note: you can directly modify or add the location in nginx server based on the original configuration
1)uwsgi_pass 127.0.0.1:9090;
Set the uwsgi proxy connection address here
2)uwsgi_param UWSGI_SCRIPT myweb.wsgi;
module points to the WSGI Py file.
3)uwsgi_param UWSGI_CHDIR /website/myweb;
Location of django project

 

Topics: Django