Nginx installation tutorial for Centos7 server, centos7nginx
1. Download and install gcc
Because Nginx is developed in C language, the source code downloaded from the official website needs to be compiled. compilation depends on the gcc environment. If there is no gcc environment, you need to install it.
[root@CentOS ~]# yum -y install gcc
2. Download and install other third-party Libraries
Some modules of Nginx depend on other third-party libraries, such as the pcre Library (rewrite module supported), zlib library (gzip module supported), and openssl (ssl module supported.
[root@CentOS ~]# yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
3. Download the installation package
[root@CentOS local]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
4. Unzip the installation package
[Root @ CentOS local] # tar-zxvf nginx-1.12.2.tar.gz nginx-1.12.2/nginx-1.12.2/auto/nginx-1.12.2/conf/nginx-1.12.2/contrib/nginx-1.12.2/src/nginx-1.12.2/configurenginx-1.12.2/LICENSEnginx-1.12.2/READMEnginx-1.12.2/html/nginx-1.12.2 /man /...... (skip the content )......
5. Create a directory named nginx to save the compiled and installed files.
[Root @ CentOS local] # mkdir nginx [root @ CentOS local] # Total ll usage 960drwxr-xr-x. 2 root 6 February 24 16:18 bindrwxr-xr-x. 2 root 6 May November 5 2016 etcdrwxr-xr-x. 2 root 6 August 2016 gamesdrwxr-xr-x. 2 root 6 August 2016 includedrwxr-xr-x. 2 root 6 December November 5 2016 libdrwxr-xr-x. 2 root 6 November 5 2016 lib64drwxr-xr-x. 2 root 6 November 5 2016 libexecdrwxr-xr-x. 10 root 141 February 24 19:03 mysql Drwxr-xr-x. 6 root 54 March 10 00:54 nginx ???????????? # Here is the newly created nginx directory drwxr-xr-x. 9 mysql 186 March 10 00:53 nginx-1.12.2 ???? # Here is the decompressed directory-rw-r --. 1 root 981687 October 17 21:20 nginx-1.12.2.tar.gzdrwxr-xr-x. 2 root 6 December November 5 2016 sbindrwxr-xr-x. 5 root 49 January 30 14:30 sharedrwxr-xr-x. 2 root 6 December November 5 2016 src
6. Generate Makefile
[root@CentOS local]# cd nginx-1.12.2/[root@CentOS nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
-- Prefix is used to specify the directory for compilation and installation, that is,/usr/local/nginx. In addition, we can see the new Makefile file under the/usr/local/nginx-1.12.2 directory.
[Root @ CentOS nginx-1.12.2] # ll total usage 708drwxr-xr-x. 6 mysql 4096 March 10 00:52 auto-rw-r --. 1 mysql 278202 October 17 21:16 CHANGES-rw-r --. 1 mysql 423948 October 17 21:16 CHANGES. rudrwxr-xr-x. 2 mysql 168 March 10 00:52 conf-rwxr-xr-x. 1 mysql 2481 October 17 21:16 configuredrwxr-xr-x. 4 mysql 72 March 10 00:52 contribdrwxr-xr-x. 2 mysql March 10 00:52 html-rw-r --. 1 my SQL mysql 1397 October 17 21:16 LICENSE-rw-r --. 1 root 376 March 10 00:54 Makefile ???? # Here is the generated Makefile file drwxr-xr-x. 2 mysql 21 Aug 17 00:52 mandrwxr-xr-x. 3 root 174 March 10 00:54 objs-rw-r --. 1 mysql 49 October 17 21:16 READMEdrwxr-xr-x. 9 mysql March 10 00:52 src [root @ CentOS nginx-1.12.2] #
7. Compile and install
[root@CentOS nginx-1.12.2]# make && make install
8. view the installed files
[Root @ CentOS local] # cd nginx [root @ CentOS nginx] # Total ll usage 4drwxr-xr-x. 2 root 4096 March 10 00:54 confdrwxr-xr-x. 2 root 40 March 10 00:54 htmldrwxr-xr-x. 2 root 6 March 10 00:54 logsdrwxr-xr-x. 2 root 19 aug 17 00:54 sbin [root @ CentOS nginx] #
As shown above, the Nginx installation directory contains four directories, including conf, html, logs, and sbin.
9. Start Nginx
[Root @ CentOS nginx] # Total ll usage 4drwxr-xr-x. 2 root 4096 March 10 00:54 confdrwxr-xr-x. 2 root 40 March 10 00:54 htmldrwxr-xr-x. 2 root 6 March 10 00:54 logsdrwxr-xr-x. 2 root 19 aug 17 00:54 sbin [root @ CentOS nginx] #. /sbin/nginx ???????? # Start Nginx [root @ CentOS nginx] # ps-ef | grep nginx ???? # Check whether the Nginx process already has root 13511 1 0? 00:00:00 nginx: master process./sbin/nginxnobody 13513 13511 0? 00:00:00 nginx: worker processroot 13517 3494 0 00:00:00 pts/0 grep -- color = auto nginx [root @ CentOS nginx] #
10. Create an nginx script in the/etc/init. d directory.
[Root @ CentOS init. d] # touch nginx [root @ CentOS init. d] # chmod u + x nginx ???? # Add the user execution permission to the script [root @ CentOS init. d] # ll total usage 48-rw-r -- r --. 1 root 15131 September 12 2016 functions-rwxr-xr-x. 1 root 10576 February 24 18:57 mysql-rwxr-xr-x. 1 root 2989 September 12 2016 netconsole-rwxr-xr-x. 1 root 6643 September 12 2016 network-rwxr -- r --. 1 root 0 March 10 01:55 nginx ???? # You can see the newly created script nginx-rw-r --. 1 root 1160 November 7 2016 README-rwxr -- r --. 1 root 450 March 2 22:50 svnd [root @ CentOS init. d] #
11. The edited nginx script content is as follows:
#!/bin/bash# chkconfig: - 85 15PATH=/usr/local/nginxDESC="nginx daemon"NAME=nginxDAEMON=$PATH/sbin/$NAMECONFIGFILE=$PATH/conf/$NAME.confPIDFILE=$PATH/logs/$NAME.pidSCRIPTNAME=/etc/init.d/$NAMEset -e[ -x "$DAEMON" ] || exit 0do_start() {$DAEMON -c $CONFIGFILE || echo -n "nginx already running"}do_stop() {$DAEMON -s stop || echo -n "nginx not running"}do_reload() {$DAEMON -s reload || echo -n "nginx can't reload"}case "$1" instart)echo -n "Starting $DESC: $NAME"do_startecho ".";;stop)echo -n "Stopping $DESC: $NAME"do_stopecho ".";;reload|graceful)echo -n "Reloading $DESC configuration..."do_reloadecho ".";;restart)echo -n "Restarting $DESC: $NAME"do_stopdo_startecho ".";;*)echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2exit 3;;esacexit 0
Note that the preceding configuration information can be modified based on the Nginx installation location.
12. start or stop the nginx Service
[Root @ CentOS init. d] # service nginx startStarting nginx daemon: nginx. [root @ CentOS init. d] # ps-ef | grep nginx ???? # Are you sure the nginx service has started root 14696 1 0? 00:00:00 nginx: master process/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx. confnobody 14699 14696 0? 00:00:00 nginx: worker processroot 14702 3494 0 00:00:00 pts/0 grep -- color = auto nginx [root @ CentOS init. d] #
[Root @ CentOS init. d] # service nginx stopStopping nginx daemon: nginx. [root @ CentOS init. d] # ps-ef | grep nginx ???? # Confirm that the nginx service has stopped root 14734 3494 0 00:00:00 pts/0 grep -- color = auto nginx [root @ CentOS init. d] #
13. Add the nginx service to the system service and set it to start upon startup.
[Root @ CentOS init. d] # chkconfig -- add nginx [root @ CentOS init. d] # chkconfig nginx on note: the request is being forwarded to "systemctl enable nginx. service ". Created symlink from/etc/systemd/system/multi-user.target.wants/nginx. service to/usr/lib/systemd/system/nginx. service. [root @ CentOS init. d] #