Install and download Nginx in the Linux (CentOS) environment and configure it

  • 2020-05-17 07:35:58
  • OfStack

1. Preparation

Choose to install these first: GCC, PCRE (Perl Compatible Regular Expression), zlib, OpenSSL.

Nginx is written by C, which needs to be compiled by GCC. PCRE is used in Rewrite and HTTP modules of Nginx; Gzip in Nginx USES zlib;

Use the command "# gcc" to see if gcc is installed. If the "gcc: no input files" message appears, it means that it has been installed.

Otherwise, you'll need the command "# yum install gcc" to install it! Channel 1 May require multiple input of y for confirmation.

Once installed, you can test it again with the command "#gcc" or with the command "# gcc-v" to see the version number.

In the same way, install PCRE, zlib, OpenSSL (devel, develop development kit) with the following command:


# yum install -y pcre pcre-devel 
# yum install -y zlib zlib-devel 
# yum install -y openssl openssl-devel 

Download and install

Create a directory (nginx-src) and enter it; Then, download, unzip, configure, compile, and install from the official address (http:// nginx.org /) :


# mkdir nginx-src && cd nginx-src 
# wget http://nginxorg/download/nginx-targz 
# tar xzf nginx-targz  
# cd nginx-3 
# /configure 
# make 
# make install 
# whereis nginx 
nginx: /usr/local/nginx 

The default installation path is: /usr/local/nginx; Jump to the sbin path in its directory and you can start or stop it.


# /nginx -h 
nginx version: nginx/3 
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives] 
Options: 
 -?,-h     : this help 
 -v      : show version and exit 
 -V      : show version and configure options then exit 
 -t      : test configuration and exit 
 -q      : suppress non-error messages during configuration testing 
 -s signal   : send signal to a master process: stop, quit, reopen, reload 
 -p prefix   : set prefix path (default: /usr/local/nginx/) 
 -c filename  : set configuration file (default: conf/nginxconf) 
 -g directives : set global directives out of configuration file 

Activation: nginx

Stop: nginx-s stop

3. Add to system services

Use the command "# vi/etc/init d/nginx", open the editor, enter the following contents:


#!/bin/sh 
# chkconfig: 2345 85 15 
# Startup script for the nginx Web Server 
# description: nginx is a World Wide Web server  
# It is used to serve HTML files and CGI 
# processname: nginx 
# pidfile: /usr/local/nginx/logs/nginxpid 
# config: /usr/local/nginx/conf/nginxconf 
 
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 
DESC="nginx deamon" 
NAME=nginx 
DAEMON=/usr/local/nginx/sbin/$NAME 
SCRIPTNAME=/etc/initd/$NAME 
 
test -x $DAEMON || exit 0 
 
d_start(){ 
 $DAEMON || echo -n "already running" 
} 
 
d_stop(){ 
 $DAEMON -s quit || echo -n "not running" 
} 
 
 
d_reload(){ 
 $DAEMON -s reload || echo -n "can not reload" 
} 
 
case "$1" in 
start) 
 echo -n "Starting $DESC: $NAME" 
 d_start 
 echo "" 
;; 
stop) 
 echo -n "Stopping $DESC: $NAME" 
 d_stop 
 echo "" 
;; 
reload) 
 echo -n "Reloading $DESC conf" 
 d_reload 
 echo "reload " 
;; 
restart) 
 echo -n "Restarting $DESC: $NAME" 
 d_stop 
 sleep 2 
 d_start 
 echo "" 
;; 
*) 
 echo "Usage: $ScRIPTNAME {start|stop|reload|restart}" >&2 
 exit 3 
;; 
esac 
 
exit 0 

Save exit, then use the following command to make it executable; Then, add the configuration and view it.

You can modify the value with chkconfig or the ntsysv tool to change whether it starts from scratch.


# chmod +x /etc/initd/nginx 
# chkconfig --add nginx 
# chkconfig nginx on/off 
# chkconfig --list nginx 
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off

Related articles: