Centos 在线安装 nginx

一个人想着一个人 提交于 2019-12-15 17:38:52

centos 在线安装 nginx

  1. 安装nginx

​ 参考文档: http://nginx.org/en/linux_packages.html 中的RHEL/CentOS章节,按照步骤安装repository。

sudo yum install yum-utils
sudo vi /etc/yum.repos.d/nginx.repo  // 创建该文件并添加[nginx-stable] 和 [nginx-mainline]的内容保存

[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
module_hotfixes=true

[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
module_hotfixes=true

// 保存好后安装nginx的stable稳定版
sudo yum install nginx
  1. 启动nginx
[root@localhost ~] sudo service nginx start
Redirecting to /bin/systemctl start nginx.service
[root@localhost ~] netstat -ntlp | grep 80   // 没有netstat自己安装下 sudo yum install net-tools
tcp  0  0 0.0.0.0:80  0.0.0.0:*   LISTEN  1589/nginx: master  // nginx服务启动,并且监听在80端口
  1. 测试

    在自己的电脑输入 http://ip:80, 出现以下界面则说明安装成功

    1. 如果出现了访问失败,则需要在访问的电脑中进行如下检测

      
      telnet 192.168.0.101 80  // 查看nginx服务器的80端口能不能访问。
    2. 如果不能访问80端,查看服务器 selinux和firewall 是否开启了

      [root@localhost ~] sudo getenforce  // 查看selinux是否开启
      enforcing                  // enforecing 则未开启了
      [root@localhost ~] vi /etc/selinux/config  // 永久关闭selinux,打开/etc/selinux/config编辑
      
      SELINUX = disabled   // 将 SELINUX = enforcing 改为 disabled

      可以重启服务器生效。

    3. 给firewalld 添加 80 端口

      [root@localhost ~] sudo systemctl status firewalld    // 查看firewalld是否开启了
      firewalld.service - firewalld - dynamic firewall daemon
         Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
         Active: active (running) since Sun 2019-12-15 23:57:33 CST; 4min 36s ago
           Docs: man:firewalld(1)
       Main PID: 824 (firewalld)
          Tasks: 2 (limit: 4912)
         Memory: 35.4M
         CGroup: /system.slice/firewalld.service
                 └─824 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid
      
      12月 15 23:57:32 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
      12月 15 23:57:33 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.

      出现了上面的说明firewalld已经开启了。

      [root@localhost ~] sudo firewall-cmd --zone=public --add-port=80/tcp --permanent  //添加80端口
      success
      [root@localhost ~] sudo systemctl restart firewalld  // 重启下firewalld

      继续测试,应该可以访问成功了。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!