centos7安装nginx的两种方法

第一种方式:通过yum安装

直接通过 yum install nginx 肯定是不行的,因为yum没有nginx,所以首先把 nginx 的源加入 yum 中

运行下面的命令:

1.将nginx放到yum repro库中
[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安装ngnix
[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服务(192.168.10.11)
如在客户机(192.168.10.10)的浏览器访问:http://yytest.com

d.你发现访问不了,原因1,你没有在hosts文件做映射;原因2,及时你在hosts文件中了映射,由于nginx服务器的80端口堵塞或防火墙没关

e.解决办法:
步骤一:修改客户机(192.168.10.10)的hosts文件,使用SwitchHosts工具添加 192.168.10.11     yytest.com
步骤二:关闭防火墙,具体下文有说明

9.nginx常用操作
启动:
$ /usr/sbin/nginx或任意路径下运行service nginx start(centos7是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端口没有打开或防火墙没有关闭

解决办法:

如果是centos6:
解决方法如下: 
/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

如果是centos7
[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.查看版本s
[root@localhost ~]# nginx -v

6.url访问nginx localhost或127.0.0.1
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 148,827评论 1 317
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 63,511评论 1 266
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 99,318评论 0 218
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,108评论 0 189
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,112评论 1 266
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,387评论 1 185
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 30,905评论 2 283
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,657评论 0 177
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,144评论 0 223
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,744评论 2 225
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,100评论 1 236
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,565评论 2 222
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,041评论 3 216
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,769评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,301评论 0 178
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,173评论 2 239
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,313评论 2 242

推荐阅读更多精彩内容