1.在nginx: download下载最新版nginx包,推荐使用稳定版。
2. 上传nginx到linux系统。我这里是自己建的目录,路径为:/home/software/3-nginx
3.安装依赖环境
(1)安装gcc环境
yum install gcc-c++
(2)安装PCRE库,用于正则表达式解析。
yum install -y pcre pcre-devel
(3)zlib压缩和解压缩依赖安装
yum install -y zlib zlib-devel
(4)SSL安全的加密套接字协议层,用于HTTP安全传输,也就是https。
yum install -y openssl openssl-devel
4.将刚才上传到/home/software/3-nginx目录下的压缩包解压,需要注意,解压得到的是源码,源码需要编译后才能安装。
tar -zxvf nginx-1.24.0.tar.gz
5.编译之前,先创建nginx临时目录,如果不创建,在启动的过程中会报错。
mkdir /var/temp/nginx -p
6.在nginx目录,输入如下命令进行配置,目的是为了创建makefile文件。
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/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
注意:我这里是在/home/1-software/4-nginx/nginx-1.24.0(解压出来的压缩文件)目录下执行该命令。这里说明一下“\”代表在命令行中换行,用于提高命令的可读性。
命令 | 解释 |
--prefix | 指定nginx安装目录 |
--pid-path | 指定nginx的pid |
--lock-path | 锁定安装文件,防止被恶意篡改或误操作 |
--error-log | 错误日志 |
--http-log-path | http日志 |
--with-http_gzip_static_module | 启用gzip模块,在线实时压缩输出数据流 |
--http-client-body-temp-path | 设定客户端请求的临时目录 |
--http-proxy-temp-path | 设定http代理临时目录 |
--http-fastcgi-temp-path | 设定fastcgi临时目录 |
--http-uwsgi-temp-path | 设定uwsgi临时目录 |
--http-scgi-temp-path | 设定scgi临时 |
7.在目录/home/1-software/4-nginx/nginx-1.24.0下用make命令进行编译。
make
8.在目录/home/1-software/4-nginx/nginx-1.24.0下安装。
make install
9.通过whereis nginx命令可以查看安装的nginx位置,进入nginx安装位置(/usr/local/nginx),进入sbin目录,启动nginx。
./nginx
停止nginx:
./nginx -s stop
重新加载:
./nginx -s reload
10.最后打开浏览器,访问虚拟机所在的IP就可以访问nginx的默认页面啦。
提示:
1.如果在虚拟机安装nginx,需要关闭防火墙。
切换至root用户,先输入“systemctl stop firewalld”命令关闭防火墙,然后输入“systemctl disable firewalld”命令禁用防火墙即可。
2.如果在云服务安装nginx,需要开启默认的nginx 80 端口。
centos7 开放80端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent
备注:如果执行firewall-cmd --zone=public --add-port=80/tcp --permanent命令出现下面的报错提示:
FirewallD is not running
执行下面的命令:
systemctl start firewalld
然后再执行:
firewall-cmd --zone=public --add-port=80/tcp --permanent
命令含义:
--zone #作用域
--add-port=80/tcp #添加端口,格式为:端口/通讯协议
--permanent #永久生效,没有此参数重启后失效
出现success表明添加成功
重启防火墙:systemctl restart firewalld.service
再次在浏览器上输入云主机ip,也可以访问Nginx。
3.本地的Windows或Mac也需要关闭防火墙。