1、环境安装
以下命令按顺序安装即可,成功之后进行下一步操作;
yum install gcc-c++
yum -y install pcre*
yum -y install openssl*
2、上传压缩包
压缩包上传到虚拟机的根目录下的usr下的local,即 /usr/local
cd /usr/local/
3、解压压缩包
tar -zxvf nginx-1.9.9.tar.gz
4、进入nginx目录
cd nginx-1.9.9
5、设置安装目录
设置nginx的安装目录 /usr/local/nginx
./configure --prefix=/usr/local/nginx
6、编译、安装
到安装目录下进行编译和安装,以下命令任选其一;
make && make install
安装Iptables管理工具&&启动Iptables&&设为Iptables开机自启&&清空Iptables规则&&保存Iptables默认规则,安装完成之后提示如下;
完毕! Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service. iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 确定 ]
yum -y install iptables-services && systemctl start iptables && systemctl enable iptables&& iptables -F && service iptables save
7、启动nginx服务并查看服务
到/usr/local/nginx/sbin/目录下进行进程的启动
切换目录:cd /usr/local/nginx/sbin/
启动进程:./nginx
查询:ps -ef | grep nginx
7.1、nginx操作常用命令
进入 nginx 目录中: cd /usr/local/nginx/sbin
才能操作
关闭命令:./nginx -s stop
查看版本号:./nginx -v
重新加载nginx配置文件:./nginx -s reload
如启动成功提示如下:
root 107055 1 0 16:10 ? 00:00:O0 nginx: master process ./nginx nobody 107056 107055 0 16:10 ? 00:00:00 nginx: worker process root 107303 96623 0 16:10 pts/0 00:00:00 grep --color=auto nginx
8、释放端口号
防火墙释放nginx所需的80端口号,并重启防火墙,再次查看端口号是否释放成功;端口号释放成功,提示【success】
释放端口号:firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙:systemctl restart firewalld.service
关闭防火墙:systemctl stop firewalld
防火墙自启:systemctl enable firewalld
防火墙不自启:systemctl disable firewalld
查看开放的端口(简单查询):firewall-cmd --list-ports
查看开放的端口(详细查询):firewall-cmd --list-all
9、安装成功
浏览访问http://安装nginx的虚拟机IP:80,提示如下及安装成功
Welcome to nginx!
If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.
For online documentation and support please refer to nginx.orgCommercial support is available at nginx.com.
Thank you for using nginx.
10、nginx设置开机自启
随后开始设置开机自启,编辑etc文件
vi /etc/rc.d/rc.local
在最后一行加入nginx对应的路径,保存退出
/usr/local/nginx/sbin/nginx
设置完毕后需要给文件添加可执行权限,添加后可看到字体【rc.local】变绿
cd /etc/rc.d
chmod 777 rc.local
ll
最后一步,使用reboot重启,使用 ps -ef 检查是否成功开机自启。如图显示自启成功
ps -ef | grep nginx
如启动成功提示如下:
[rootalocalhost -]# ps -ef l grep nginx
root 9948 1 0 16:10 ? 00:00:00 nginx: naster process /usr/local/nginx/sbin/nginx nobody 107056 107055 0 16:10 ? 00: 00: 00 nginx: worker process root 107303 96623 0 16:10 pts/0 00:00:00 grep --color=auto nginx