1、安装nginx所需的pcre库
[root@linux ~]# yum install pcre pcre-devel -y
检查安装结果
[root@linux ~]# rpm -qa pcre pcre-devel
pcre-devel-7.8-7.el6.x86_64
pcre-7.8-7.el6.x86_64
2、检查依赖包
[root@linux ~]# rpm -qa pcre-devel pcre
pcre-devel-7.8-7.el6.x86_64
pcre-7.8-7.el6.x86_64
[root@linux ~]# rpm -qa openssl-devel openssl
openssl-1.0.1e-58.el6_10.x86_64
没有检查到openssl-devel包,需要安装,该包是nginx使用https服务的时候需要的模块,如果没有该包,安装nginx的过程中会报错。
[root@linux ~]# yum install -y openssl openssl-devel
3、安装nginx
wget -q http://nginx.org/download/nginx-1.19.4.tar.gz
useradd nginx -s /sbin/nologin -M
tar xf nginx-1.19.4.tar.gz
cd nginx-1.19.4
./configure --user=nginx --group=nginx --prefix=/application/nginx --with-http_stub_status_module --with-http_ssl_module
此时报如下错误:
./configure: error: C compiler cc is not found
是由于依赖包没有安装成功,安装命令如下:
yum -y install gcc gcc-c++ autoconf automake make
再次执行
./configure --user=nginx --group=nginx --prefix=/application/nginx --with-http_stub_status_module --with-http_ssl_module
make
make install
至此,nginx安装完成。
4、启动nginx
语法检测:
[root@linux nginx-1.19.4]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx/conf/nginx.conf test is successful
启动:
[root@linux nginx-1.19.4]# /application/nginx/sbin/nginx
5、检测nginx安装是否成功
注:直接访问可能会有问题,是因为服务器防火墙没有关闭,使用如下命令,关闭防火墙,并禁止防火墙开机自启动
[root@linux nginx-1.19.4]# /etc/init.d/iptables stop
[root@linux nginx-1.19.4]# /etc/init.d/iptables status
[root@linux nginx-1.19.4]# chkconfig iptables off
6、nginx配置虚拟主机
可采用如下三种模式进行虚拟主机配置:域名、端口、IP,具体步骤如下:
1)增加一个完成的server标签到结尾处;
2)更改server_name及对应网页的root根目录;
3)创建server_name域名对应网页的根目录;
4)检查nginx配置文件语法,平滑重启nginx服务;
5)在客户端对server_name配置的域名做host解析;