Centos7 安装nginx-1.16.1

Centos7 安装nginx-1.16.1

1、装备安装环境

准备安装依赖:

[root@nginx-test]# yum install -y gcc pcre pcre-devel openssl openssl-devel gd gd-devel

等依赖安装完成后,下载nginx,可以从官网找到合适的版本:nginx
NGINX列表

需要提前安装wget yum install wget
[root@nginx-test]# wget -c http://nginx.org/download/nginx-1.16.1.tar.gz

解压:

[root@nginx-test]# tar-zxvfnginx-1.16.1.tar.gz
[root@nginx-test]# cd nginx-1.16.1
[root@nginx-test]# 

2、开始安装

具体是想默认配置安装还是自定义配置,可自行斟酌。

2.1、默认

[root@nginx-test]# ./configure

2.2、配置具体 用户和用户组

[root@nginx-test]# ./configure \
--prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/run \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-http_image_filter_module \
--with-debug 

2.3、解决安装依赖报错

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
[root@nginx-test]# yum -y install openssl openssl-devel

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
[root@nginx-test]# yum -y install pcre-devel

./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.

[root@nginx-test]# yum -y install libgd-dev libgd2-xpm-dev

configure: error: You need a C++ compiler for C++ support.
配置:错误:您需要一个c++编译器来支持c++。

[root@nginx-test]# yum -y install gcc-c++

2.4、不依赖用户组

[root@nginx-test]# ./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

2.5、进行安装

[root@nginx-test]# make && make install 

然后就是安装成功了!!

3、nginx基本操作

启动nginx
[root@nginx-test]# /usr/local/nginx/sbin/nginx
停止nginx
[root@nginx-test]# /usr/local/nginx/sbin/nginx -s stop
[root@nginx-test]# /usr/local/nginx/sbin/nginx -s quit
重启nginx
[root@nginx-test]# /usr/local/nginx/sbin/nginx -s reload
检查nginx.conf
[root@nginx-test]# /usr/local/nginx/sbin/nginx -t
将nginx加入环境变量
[root@nginx-test logs]# vim /etc/profile    #编辑配置文件
[root@nginx-test logs]# source /etc/profile #刷新配置文件
[root@nginx-test logs]# cat /etc/profile    #查看配置文件	
export NGINX_HOME="/usr/local/nginx"
export JAVA_HOME="/usr/java/jdk1.8.0_171-amd64"
export PATH=$PATH:$JAVA_HOME/bin:$NGINX_HOME/sbin
查看nginx进程:
[root@nginx-test]# netstat -tlunp | grep nginx
tcp        000.0.0.0:800.0.0.0:*               LISTEN      17506/nginx: master 
[root@nginx-test]# ps -ef | grep nginx
root     175061014:28 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx    1750717506014:28 ?    00:00:00 nginx: worker process
root     1752210068014:39 pts/ 000:00:00 grep --color=auto nginx
[root@nginx-test]#

查看NGINX进程

4、F&Q/常见问题

如果在第一次启动的时候遇到 nginx: [emerg] getpwnam("nginx") failed这个错误,没有安装nginx用户导致的无法启动,需要添加nginx用户,按下面命令操作即可,然后再次启动即可

[root@nginx-test]# id nginx
id: nginx: no such user
[root@nginx-test]#  useradd -s /sbin/nologin -M nginx
[root@nginx-test]# /usr/local/nginx/sbin/nginx 

创建NGINX用户

4、nginx设置开机启动
在/etc/init.d目录下创建nginx脚本

[root@hk-web-database-pc init.d]# cd /etc/init.d/
[root@hk-web-database-pc init.d]# vim nginx

nginx内容如下:

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# 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启动文件路径
nginx_config="/usr/local/nginx/conf/nginx.conf"
#nginx配置文件路径
nginx_pid="/usr/local/nginx/logs/nginx.pid"
#nginxPID文件

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 nginx service functions.
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文件添加执行权限

# chmod +x nginx

添加开机启动

[root@hk-web-database-pc init.d]# chkconfig --add nginx
//添加nginx启动项
[root@hk-web-database-pc init.d]# chkconfig nginx on
//设置nginx开机启动
[root@hk-web-database-pc init.d]# chkconfig --list
//查看启动项
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

aegis          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
jexec          	0:off	1:on	2:on	3:on	4:on	5:on	6:off
mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
nginx          	0:off	1:off	2:on	3:on	4:on	5:on	6:off

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://www.hesc.info/archives/20191223023043

Buy me a cup of coffee ☕.