下载Nginx
到官网下载源码文件,地址:http://nginx.org/en/download.html,选择最新版本。本人下载的地址为:http://nginx.org/download/nginx-1.10.2.tar.gz,可用wget命令下载,也可以在windows系统上下载好再传到linux上。
安装编译器和依赖库
yum install gcc gcc-c++ zlib-devel pcre-devel openssl-devel openssl-libs openssl -y
1
如果已经安装,就不必了 安装前配置
cd命令转到解压后的目录下。
./configure --prefix=/usr/local/nginx
1
这样安装时系统就会把Nginx安装到/usr/local/nginx目录下。 编译
make
1
安装
make install
1
安装完成,接下来配置环境变量以后就不用使用绝对路径来操作Nginx了:
查找安装路径:
whereis nginx./nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。./nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
查询nginx进程:
ps aux|grep nginx1.先停止再启动(推荐): 对 nginx 进行重启相当于先停止再启动,即先执行停止命令再执行启动命令。如下:
./nginx -s quit ./nginx2.重新加载配置文件: 当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 ngin x再启动 nginx 即可将配置信息在 nginx 中生效,如下: ./nginx -s reload
启动成功后,在浏览器可以看到这样的页面:
即在rc.local增加启动代码就可以了。
vi /etc/rc.local增加一行 /usr/local/nginx/sbin/nginx 设置执行权限:
chmod 755 rc.local到这里,nginx就安装完毕了,启动、停止、重启操作也都完成了,当然,你也可以添加为系统服务,我这里就不在演示了。
测试安装是否成功:
curl http://192.168.241.129/
1
这里的ip地址为你本机的ip地址
然后会有结果:
[root@localhost nginx-1.12.1]# curl http://192.168.241.129/ <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p>
<p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p> </body> </html>
以上验证了本地是已经安装成功的了,
但是我们远程访问的时候就是访问不了,一直说拒绝访问,那就是可能真的跟端口,防火墙有关了。 (四)防火墙、端口开启设置
1、查看端口是否开放:firewall-cmd --query-port=80/tcp
2、开放端口:firewall-cmd --zone=public --add-port=80/tcp --permanent
3、重启防火墙:firewall-cmd --reload
4、再次查看端口是否成功:firewall-cmd --query-port=80/tcp
过程如下:
5、另外,阿里云的服务器要添加安全组规则
再次测试,显示成功安装