CentOS 7 安装 Nginx 概述
在 CentOS 7 环境下部署 Nginx 服务
方法一:使用 YUM 包管理器安装
由于默认的 YUM 仓库中不包含 Nginx 包,因此直接运行 `yum install nginx` 会失败。首先需要将 Nginx 的官方源添加到系统的 YUM 配置中。
具体操作步骤如下:
1. 将 Nginx 源添加到 YUM 仓库库中
复制代码 代码如下: [root@localhost ~]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2. 检查 Nginx 相关信息
[root@localhost ~]# yum info nginx
3. 执行 YUM 安装 Nginx
[root@localhost ~]# yum install nginx
安装过程输出示例如下:
[root@localhost ~]# yum install nginx
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.usc.edu
* extras: mirror.raystedman.net
* updates: mirror.metrocast.net
正在解决依赖关系
--> 正在检查事务
---> 软件包 nginx.x86_64.1.1.10.1-1.el7.ngx 将被 安装 ······ ······
正在安装 : 1:nginx-1.10.1-1.el7.ngx.x86_64
Thanks for using nginx! Please find the official documentation for nginx here:
* http://nginx.org/en/docs/
Commercial subscriptions for nginx are available on:
* http://nginx.com/products/
----------------------------------------------------------------------
验证中 : 1:nginx-1.10.1-1.el7.ngx.x86_64 1/1
已安装:
nginx.x86_64 1:1.10.1-1.el7.ngx
完毕!
4. 启动 Nginx 服务
[root@localhost ~]# service nginx start
5. 查看 Nginx 版本信息
[root@localhost ~]# nginx -v
6. 访问 Nginx 页面,此时可以通过公网 IP(本地测试可使用 localhost 或 127.0.0.1)查看 Nginx 服务返回的默认欢迎页面。
[root@localhost ~]# curl -i localhost
效果如下:
······ Welcome to nginx!。 ······
7. Nginx 的配置文件位于 /etc/nginx/ 目录下
[root@localhost /]# ll /etc/nginx/
总用量 32
drwxr-xr-x. 2 root root 25 10月 12 13:11 conf.d
-rw-r--r--. 1 root root 1007 5月 31 22:09 fastcgi_params
-rw-r--r--. 1 root root 2837 5月 31 22:09 koi-utf
-rw-r--r--. 1 root root 2223 5月 31 22:09 koi-win
-rw-r--r--. 1 root root 3957 5月 31 22:09 mime.types
lrwxrwxrwx. 1 root root 29 10月 12 13:11 modules -> ../../usr/lib64/nginx/modules
-rw-r--r--. 1 root root 643 5月 31 22:08 nginx.conf
-rw-r--r--. 1 root root 636 5月 31 22:09 scgi_params
-rw-r--r--. 1 root root 664 5月 31 22:09 uwsgi_params
-rw-r--r--. 1 root root 3610 5月 31 22:09 win-utf
8. 实践案例:
目标:修改服务名称,并从外部网络访问该服务
操作步骤:
a. 修改 Nginx 配置文件
[root@localhost nginx]# vim /etc/nginx/conf.d/default.conf
修改 server_name 部分为:server_name yytest.com;
b. 重载服务配置
[root@localhost nginx]# /usr/sbin/nginx -s reload
c. 从外部客户端访问 Nginx 服务(假设服务器 IP 为 192.168.10.11)
例如在客户机(IP 为 192.168.10.10)的浏览器中访问:http://yytest.com
d. 如果发现无法访问,原因可能有两个:一是未在 hosts 文件中进行域名映射;二是即使做了映射,Nginx 服务器的 80 端口可能被占用或未关闭防火墙。
e. 解决方法:
步骤一:修改客户机(192.168.10.10)的 hosts 文件,使用 SwitchHosts 工具添加映射记录 192.168.10.11 yytest.com
步骤二:关闭防火墙,具体操作见下文说明
9. Nginx 常用操作命令
启动服务:
复制代码 代码如下: $ /usr/sbin/nginx 或在任意路径下运行 service nginx start (在 CentOS 7 中也可使用 systemctl start nginx.service )
重启服务:
$ /usr/sbin/nginx –s reload
停止服务:
$ /usr/sbin/nginx –s stop
测试配置文件语法是否正确:
$ /usr/sbin/nginx –t
可能遇到的问题排查:
具体现象如下:1. 本机能 ping 通虚拟机;2. 虚拟机也能 ping 通本机;3. 虚拟机自身可以访问其 Web 服务;4. 但本机无法访问虚拟机的 Web 服务。造成此问题的原因通常是服务器 80 端口未开启或防火墙处于开启状态。 解决办法
如果是 CentOS 6 系统:
解决方法如下:
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
然后保存配置:
/etc/rc.d/init.d/iptables save
重启防火墙服务
/etc/init.d/iptables restart
对于 CentOS 防火墙,只需关闭其服务即可:
查看 CentOS 防火墙状态:/etc/init.d/iptables status
关闭 CentOS 防火墙服务:/etc/init.d/iptables stop
永久关闭防火墙开机自启:chkconfig –level 35 iptables off
如果是 CentOS 7 系统:
[root@rhel7 ~]# systemctl status firewalld.service
[root@rhel7 ~]# systemctl stop firewalld.service
[root@rhel7 ~]# systemctl disable firewalld.service
[root@rhel7 ~]# systemctl status firewalld.service
扩展知识:
启动一个服务:systemctl start firewalld.service
关闭一个服务:systemctl stop firewalld.service
重启一个服务:systemctl restart firewalld.service
显示一个服务的状态:systemctl status firewalld.service
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service;echo $?
查看已启动的服务列表:systemctl list-unit-files|grep enabled
方法二:通过手动下载安装包解压安装
1. 下载 Nginx 安装包。
[root@localhost ~]# wget http://nginx.org/download/nginx-1.10.1.tar.gz
2. 将安装包复制到指定的安装目录
[root@localhost ~]# cp nginx-1.10.1.tar.gz /usr/local/
3. 解压压缩包
[root@localhost ~]# tar -zxvf nginx-1.10.1.tar.gz
[root@localhost ~]# cd nginx-1.10.1
4. 启动 Nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx
5. 查看版本号
[root@localhost ~]# nginx -v
6. 通过 URL 访问 Nginx,地址为 localhost 或 127.0.0.1
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。