RHEL/CentOS下Nginx安装

1. 准备环境

sudo yum install yum-utils

2. 配置下载位置知识库

用vi,nano等工具创建文件/etc/yum.repos.d/nginx.repo,里面写入如下内容。

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key

默认为安装稳定版。如果需要使用mainline版,用下面的命令进行切换。

sudo yum-config-manager --enable nginx-mainline

3. 安装Nginx

sudo yum install nginx

验证安装的版本:

bash-4.2$ nginx -v
nginx version: nginx/1.16.0

4. 启动Nginx

直接启动nginx可执行文件即可。

sudo nginx

验证启动是否成功命令:

ps -ef|grep nginx

下面是输出例子,如果看到nginx的主进程和工作进程,说明已经正常启动了。

bash-4.2$ ps -ef|grep nginx
root       8157      1  0 14:59 ?        00:00:00 nginx: master process nginx
nginx      8158   8157  0 14:59 ?        00:00:00 nginx: worker process
adm        8160   7808  0 14:59 pts/1    00:00:00 grep nginx

5. 验证安装结果

5.1. 本地curl命令验证

在服务器本地很方便的验证方法,不需要浏览器即可验证。

curl localhost   #web服务端口为默认的80端口情况
curl localhost:8080   #web服务端口为8080端口情况

输出例子:

[root@ conf.d]# curl localhost:8080
<!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>

如果有上面的输出表示正常启动nginx. 如果是下面的这种输出,说明web服务有些异常。

C:\Users\SonyMark>curl 192.168.1.23
curl: (7) Failed to connect to 192.168.1.23 port 80: Timed out

当然,还有可以通过下面的命令,来查看web服务端口比如默认的80,是否已经处在侦听状态。

netstat -anp | grep :80   #centos 6以前
ss -anp | grep :80        #centos 7以后
lsof -i:80                #lsof工具需要用yum安装一下
5.2 远程验证

远程可以用手机PC及等多种设备的浏览器进行验证。但由于是远程访问,需要穿越防火墙。注意防火墙设置。
(1) 传统的通过iptable进行的IP/端口限制的解除。(本文割爱)
(2) CentOS7系统,由于防火墙Firewalld 为默认安装,需要配置Firewalld。

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

当然,也可以考虑关闭或者停止Firewalld。

sudo systemctl disable firewalld  #关闭防火墙
sudo systemctl stop firewalld     #停止防火墙

(3) 对于云服务器,需要另行配置完全组,保证端口能被外部访问。(本文割爱)

下面是输出例子:
在这里插入图片描述

6. 配置nginx

首先找到nginx的位置。

whereis nginx
sudo find / -name "nginx.conf"

下面是输出例子:

bash-4.2$ whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
bash-4.2$ sudo find / -name "nginx.conf"
/etc/nginx/nginx.conf

配置文件为/etc/nginx/nginx.conf 以及/etc/nginx/conf.d/default.conf
简单的修改,比如网站根目录,端口号等一般在/etc/nginx/conf.d/default.conf配置文件里面。

7. 常用nginx命令

nginx           #启动
nginx -s reload #重新加载配置文件
nginx -s quit   #优雅退出
nginx -s stop   #立即停止
nginx -t        #验证配置文件

  1. nginx: Linux packages: http://nginx.org/en/linux_packages.html
  2. centos7虚拟机开启端口后 外部不能访问的问题:https://blog.csdn.net/Honnyee/article/details/81535464
发布了73 篇原创文章 · 获赞 27 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/sitebus/article/details/95616753