CentOS 7 compiling and installing lnmp environment (Chapter 1 of nginx)

Posted by shiranwas on Mon, 27 Dec 2021 21:24:04 +0100

1: Nginx installation configuration
1. Install compilation tools and library files
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2. First install PCRE
PCRE is used to enable Nginx to support Rewrite.
Download the PCRE installation package at: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
Enter the download directory (select the directory where the downloaded files are saved)
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

Solve the above wget not found problem (this problem will not occur if it is already installed)

Download again. After downloading, enter the download directory. If you do not exit, you should still be in the download directory (/ usr/local/src)

Unzip the installation package
tar zxvf pcre-8.35.tar.gz

Enter the installation package directory
cd pcre-8.35
Compile and install
./configure
make && make install
View pcre version
pcre-config --version

3. Download nginx
Download Nginx at: https://nginx.org/en/download.html
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.6.2.tar.gz


Unzip the installation package:
tar zxvf nginx-1.6.2.tar.gz
Enter the installation package directory
cd nginx-1.6.2
Compile and install
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
! [insert picture description here]( https://img-blog.csdnimg.cn/a08d26fd91f04669ac832e1ee0c50403.png
make

make install

Start Nginx, and the Nginx start command is as follows: (it can be started at any position)
/usr/local/webserver/nginx/sbin/nginx

Visit the site (check the firewall status if the following problems occur)

systemctl status firewalld

Turn off the firewall and view the firewall status
service firewalld stop

The firewall has been turned off. Please visit the site again successfully

Other commands of Nginx, including several commands commonly used by Nginx:
/usr/local/webserver/nginx/sbin/nginx -s reload # reload the configuration file
/usr/local/webserver/nginx/sbin/nginx -s reopen # restart Nginx
/usr/local/webserver/nginx/sbin/nginx -s stop # stop Nginx
View nginx version

Solution:
Open the file where the environment variable is located:
vi /etc/profile

At the end of the profile file, add a line (your nginx installation location)
PATH=$PATH:/usr/local/webserver/nginx/sbin

Reload the environment and solve the problem
source /etc/profile

If you want to use service nginx start (stop,restart) to start, stop and restart nginx, the following problems will occur

resolvent:
Because nginx is not added to the system service, you can add one manually.
In / etc / init D / create a startup script named nginx, which is as follows:

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /usr/local/webserver/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/webserver/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/webserver/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
force_reload() {
    restart
}
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

preservation.
Modify your own nginx path
nginx="/usr/local/webserver/nginx/sbin/nginx"
NGINX_CONF_FILE="/usr/local/webserver/nginx/conf/nginx.conf"

Set execution permission: Chmod + X / etc / init d/nginx

Register as service: chkconfig --add nginx

Set startup: chkconfig nginx on

Note that you must save it in UNIX format (notepad + +: Edit - document format conversion - convert to UNIX format), otherwise an error will be reported as follows

Solution: instead of creating files manually, use vi nginx to create files and copy the contents

Run systemctl daemon reload

After that, you can use the following command
service nginx start
service nginx stop
service nginx restart

Nginx configuration: (the original configuration file nginx.conf remains unchanged. Create a new folder to store all subsequent configuration files in any location. Subsequent configuration needs to be done in nginx.conf. I put it in the nginx directory at the same level as CONF)
vi /usr/local/webserver/nginx/conf/nginx.conf

Add the following content in the http module. The address is the path of your new folder

Create a new configuration file. The configuration contents are as follows. Modify the domain name, project path and error log file to their own path

server {
    listen 80;
    server_name www.lnmp.com;
    charset utf-8;
    index index.html index.htm index.php;
    root /www/blog/public;

    proxy_connect_timeout 600s;
    proxy_send_timeout 600s;
    proxy_read_timeout 600s;
    send_timeout 300s;

    fastcgi_connect_timeout 600;
    fastcgi_send_timeout 600;
    fastcgi_read_timeout 600;
    uwsgi_read_timeout 600;

   #limit_conn perip 2;
   #limit_conn perserver 4;
 
   #proxy_read_timeout 6;
   #keepalive_timeout 1s;

   #location  /api/banners/ {
   #	limit_conn perserver 4;
   #}

    location / {
	try_files $uri $uri/ /index.php$is_args$args;
    }
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    
    error_log    /usr/local/webserver/nginx/logs/tbk_nginx_error.log    error;
    error_page 404 /index.php;
    location ~ /\. {
        deny all;
    }

    location ~ .*\.(woff|js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
        try_files $uri =404;
    }
    
    location ~ .*\.(woff|jpg|jpeg|png|gif|ico|css|js)$ {
        expires 1h;
        add_header Cache-Control public;
        add_header Pragma public;
        add_header Vary Accept-Encoding;
    }

    location ~ \.php(.*)$ {
   	fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params; 
    }
    location ~ /\.(?!well-known).* {
        deny all;
    }

    
    # security headers
    add_header X-Frame-Options "SAMEORIGIN" ;
    add_header X-XSS-Protection "1; mode=block" ;
    add_header X-Content-Type-Options "nosniff" ;
    add_header Referrer-Policy "no-referrer-when-downgrade" ;
    add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" ;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" ;
    # gzip
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;
}


Restart nginx and use the domain name to access the site (PHP is not installed at this time, you can access the html file first to verify whether the configuration is correct)

Create a new html file

Test access succeeded

Topics: PHP Linux Nginx