Compile and install nginx on CentOS7

The installed operating system is CentOS-7-x86_64-Minimal-1511, and the ngixn version is nginx-1.10.1.tar.gz, see attachment.

The first step
is that after CentOS-7 is installed, you don’t need to think about it. After connecting to the server, first execute the following commands with Xshell or SecureCRT. Later, when compiling nginx, the system will detect these, which are all pits~ ~~
File upload and download: yum -y install lrzsz
Package download command: yum -y install wget
Perl language running environment: yum -y install perl
C language compilation environment: yum -y install gcc gcc-c++ (mostly required This environment is required for compiled programs or executable programs in C language.) The

second step is to download the package
and use the rz command to upload the nginx-1.10.1.tar.gz package to the /usr/local/src/ directory and decompress it (command : tar zxvf nginx-1.10.1.tar.gz), usually it is recommended to put it under this directory for such programs that need to be compiled and installed. Download various required software packages, see below:
cd /usr/local/src/ &&
wget -ct 5 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz &&
wget -ct 5 http://www.openssl.org/source/openssl-1.0.1i.tar.gz &&
wget -ct 5 http://zlib.net/zlib-1.2.10.tar.gz &&
wget -ct 5 http://www.cmake.org/files/v2.8/cmake-2.8.11.2.tar.gz &&
wget -ct 5 http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz &&
wget -ct 5 http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz &&
wget -ct 5 https://bitbucket.org/libgd/gd-libgd/downloads/libgd-2.1.0.tar.gz &&
wget -ct 5 http://download.osgeo.org/libtiff/tiff-4.0.3.tar.gz &&
wget -ct 5 ftp://ftp.simplesystems.org/pub/png/src/libpng16/libpng-1.6.28.tar.gz &&
wget -ct 5 http://www.ijg.org/files/jpegsrc.v9a.tar.gz &&
echo "Download complete!


Decompress the three packages of openssl-1.0.1i, pcre-8.39, and zlib-1.2.10, and other packages do not need to be decompressed for the time being.
Note: The first line of cd /usr/local/src/ refers to the storage directory after these packages are downloaded; there are two ftp file downloads in the above file, it is recommended to copy the address directly to the browser to download and upload to /usr/local /src/ directory, and then delete the two lines with ftp above, because when this command is executed in Xshell, the ftp file cannot be downloaded.

The third step is to compile the nginx installation package
cd /usr/local/src &&
tar -zxvf  nginx-1.10.1.tar.gz &&
cd nginx-1.10.1 &&
./configure --prefix=/usr/local/mysoft/nginx \
--without-http_memcached_module \
--user=www \
--group=www \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-openssl=/usr/local/src/openssl-1.0.1i \
--with-zlib=/usr/local/src/zlib-1.2.10 \
--with-pcre=/usr/local/src/pcre-8.39 \
--with-http_stub_status_module \
--with-http_sub_module \

Note:
After using Xshell to connect to the root directory of the server, in the above command line,
cd /usr/local/src &&
tar -zxvf nginx-1.10.1.tar.gz &&
cd nginx-1.10.1 &&
is used to specify the section to which you want to The compiled nginx package directory, of course, you can also execute cd usr/local/src/nginx-1.10.1 to directly locate the directory where nginx decompresses the package, then the above compiled code becomes the following simple form:

./configure --prefix=/usr/local/mysoft/nginx \
--without-http_memcached_module \
--user=www \
--group=www \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-openssl=/usr/local/src/openssl-1.0.1i \
--with-zlib=/usr/local/src/zlib-1.2.10 \
--with-pcre=/usr/local/src/pcre-8.39 \
--with-http_stub_status_module \
--with-http_sub_module \

Description:
--prefix=/usr/local/mysoft/nginx is used to specify the directory that nginx needs to be installed in, which is separate from the directory just decompressed. Usually, the custom-installed package is recommended to be placed under /usr/local/, here I Under this directory, the mysoft directory is created, and then the nginx directory is created to distinguish it from the system program.
--with-openssl=/usr/local/src/openssl-1.0.1i \ is the decompressed directory in the second step
--with-zlib=/usr/local/src/zlib-1.2.10 \same
as above- -with-pcre=/usr/local/src/pcre-8.39 \ Same as above
These

three files are necessary, otherwise there will be pits later, you know~~~ After pressing Enter to execute the play command, see if there is any in Xshell Wrong, according to the above steps down should not be wrong~~. At this point, the nginx compilation and detection has been completed, and then the following installation commands are executed under the directory just now. There are also online compilation and installation commands that are written and executed together. Personal habits are executed separately, which is clear and clear.
make && make install &&
echo "Installation of NGINX is complete!"

After waiting for the console to display "Installation of NGINX is complete", execute the cd usr/local/mysoft/nginx command to check whether there are files under the directory. Normally, there are 4 directory folders, namely conf, html, logs, and sbin. At this point, the nginx installation is successful.

The fourth step is to set the startup script and start Nginx:
Xshell enters the system root directory and executes the following command vi /etc/init.d/nginx to create a script. The directory needs to be specified under /etc/init.d/. Script command:
#!/bin/bash
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/mysoft/nginx/conf/nginx.conf
# pidfile: /usr/local/mysoft/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/mysoft/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/mysoft/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx.lock
 
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
    be
    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
    be
    be
    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

Note:
Only three lines starting with # are useful, they are:
#!/bin/sh
# checkconfig : the starting line
# discription : the starting line

# config: /usr/local/mysoft/nginx/conf/nginx .conf
# pidfile: /usr/local/mysoft/nginx/logs/nginx.pid
These two configurations are specified to the directory folder where your nginx is installed successfully. After the nginx.pid file is run,

nginx="/usr/local is generated. /mysoft/nginx/sbin/nginx"
This configuration specifies the directory folder location where your nginx is successfully installed

NGINX_CONF_FILE="/usr/local/mysoft/nginx/conf/nginx.conf"
This configuration specifies the directory where your nginx is successfully installed The location of the file

lockfile=/var/lock/subsys/nginx.lock
This configuration is assigned to the bottom of /var/lock/subsys/ of the system, and nginx.lock is customized to this After the

execution , press ESC to exit the editing environment, and execute: wq Save the file and exit vi.



The fifth step is to add to the service
and execute the following commands in sequence:
chmod a+x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
After /etc/init.d/nginx start

is all set, it is recommended to restart the server or virtual machine.

Finally use the command:
$ service nginx start 
$ service nginx stop 
$ service nginx restart 
$ service nginx reload 
 
$ /etc/init.d/nginx start 
$ /etc/init.d/nginx stop 
$ /etc/init.d/nginx restart 
$ /etc/init.d/nginx reload

to check whether the setting is successful:
query nginx process: ps aux |grep nginx
query server ip: ifcfg
linux firewall policy: iptables -L (iptables is a firewall, -L is a list policy)
After querying the server ip, copy the ip address to the navigation bar to access, if the welcome page of "Welcome to nginx!" appears, it means that the nginx service is successfully set up.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326124655&siteId=291194637