1.1 安装前工作
首先更新系统软件源,使用以下命令更新系统 -
yum update
依赖包安装
[root@localhost src]# yum -y install gcc gcc-c++ autoconf automake libtool make cmake
[root@localhost src]# yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
1.2 下载Nginx安装源文件
源码下载,官网下载地址:http://nginx.org/en/download.html下载并上传到服务器
或直接在服务上执行以下命令下载
[root@localhost ~]# cd /usr/local/src
[root@localhost src]# wget -c http://nginx.org/download/nginx-1.18.0.tar.gz
解压文件命令
tar zxvf nginx-1.18.0.tar.gz
在编译之前还要做一些前期的准备工作,如:依懒包安装,Nginx用户和用户组等。
1.3
新建nginx用户及用户组
使用 root 用户身份登录系统,执行以下命令创建新的用户。
[root@localhost src]# groupadd nginx
[root@localhost src]# useradd -g nginx -M nginx
useradd命令的-M参数用于不为nginx建立home目录
修改/etc/passwd,使得nginx用户无法bash登陆(nginx用户后面由/bin/bash改为/sbin/nologin),
[root@localhost src]# vi /etc/passwd
然后找到有 nginx 那一行,把它修改为(后面由/bin/bash改为/sbin/nologin):
1.4 编译配置、编译、安装
进入解压的nginx源码目录:/usr/local/src/ 执行以下命令
[root@iZbp1chwdyqnsfyr531z9zZ]# cd /usr/local/src/nginx*
[root@iZbp1chwdyqnsfyr531z9zZ nginx-1.18.0]# ./configure --prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/run/nginx.pid \
--with-http_ssl_module \
--user=nginx \
--group=nginx \
--with-pcre \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module
注意:上面的反斜杠\表示换行继续。
–prefix=/usr/local/nginx 指定安装到 /usr/local/nginx 目录下。
配置完成后,接下来执行编译
[root@iZbp1chwdyqnsfyr531z9zZ nginx-1.18.0]# make
[root@iZbp1chwdyqnsfyr531z9zZ nginx-1.18.0]# make install
查看版本:
[root@iZbp1chwdyqnsfyr531z9zZ nginx-1.18.0]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.18.0
启动Nginx程序、查看进程
[root@iZbp1chwdyqnsfyr531z9zZ nginx-1.18.0]# /usr/local/nginx/sbin/nginx
[root@iZbp1chwdyqnsfyr531z9zZ nginx-1.18.0]# ps -ef | grep nginx
root 22030 1 0 10:20 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 22031 22030 0 10:20 ? 00:00:00 nginx: worker process
root 22033 19200 0 10:20 pts/0 00:00:00 grep --color=auto nginx
nginx停止、重启
未添加nginx服务前对nginx的管理只能通过一下方式管理:
nginx 管理的几种方式
启动Nginx
/usr/local/nginx/sbin/nginx
从容停止Nginx:
kill -QUIT 主进程号 # 如上一步中的 ps 命令输出的 29151,就是 Nginx的主进程号
快速停止Nginx:
kill -TERM 主进程号
强制停止Nginx:
pkill -9 nginx
平滑重启nginx
/usr/nginx/sbin/nginx -s reload
现在我们来看看安装的Nginx的运行结果,可以简单地使用curl命令访问localhost测试,结果如下
[root@iZbp1chwdyqnsfyr531z9zZ nginx-1.18.0]# curl localhost
也可以打开浏览访问目标服务器的IP,在本示例中,服务器的IP地址是:47.98.115.141 打开浏览器访问
提示: 如果访问失败,在确保Nginx启动的前提下,检查SeLinux和防火墙是否已关闭。关闭防火墙命令:
systemctl stop firewalld.service。
如果修改配置,运行下面命令验证配置是否合法
[root@iZbp1chwdyqnsfyr531z9zZ nginx-1.18.0]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
END