centos7安装nginx的两种方法介绍
centos7安装nginx
第一种方式:通过yum安装
直接通过yuminstallnginx肯定是不行的,因为yum没有nginx,所以首先把nginx的源加入yum中。
运行下面的命令:
1.将nginx放到yumrepro库中
[root@localhost~]#rpm-ivhhttp://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2.查看nginx信息
[root@localhost~]#yuminfonginx
3.使用yum安装ngnix
[root@localhost~]#yuminstallnginx
效果如下:
[root@localhost~]#yuminstallnginx
已加载插件:fastestmirror,langpacks
Loadingmirrorspeedsfromcachedhostfile
*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
Thanksforusingnginx!
Pleasefindtheofficialdocumentationfornginxhere:
*http://nginx.org/en/docs/
Commercialsubscriptionsfornginxareavailableon:
*http://nginx.com/products/
----------------------------------------------------------------------
验证中 :1:nginx-1.10.1-1.el7.ngx.x86_64 1/1
已安装:
nginx.x86_641:1.10.1-1.el7.ngx
完毕!
4.启动nginx
[root@localhost~]#servicenginxstart
5.查看nginx版本
[root@localhost~]#nginx-v
6.访问nginx,现在你可以通过公网ip(本地可以通过localhost/或127.0.0.1)查看nginx服务返回的信息。
[root@localhost~]#curl-ilocalhost
效果如下:
······
Welcometonginx!。
······
7.nginx配置文件位置在/etc/nginx/
[root@localhost/]#ll/etc/nginx/ 总用量32 drwxr-xr-x.2rootroot2510月1213:11conf.d -rw-r--r--.1rootroot10075月3122:09fastcgi_params -rw-r--r--.1rootroot28375月3122:09koi-utf -rw-r--r--.1rootroot22235月3122:09koi-win -rw-r--r--.1rootroot39575月3122:09mime.types lrwxrwxrwx.1rootroot2910月1213:11modules->../../usr/lib64/nginx/modules -rw-r--r--.1rootroot6435月3122:08nginx.conf -rw-r--r--.1rootroot6365月3122:09scgi_params -rw-r--r--.1rootroot6645月3122:09uwsgi_params -rw-r--r--.1rootroot36105月3122:09win-utf
8.实践:
目的:修改服务名,接着从外部访问这个服务
操作:
a.修改nginx配置文件
[root@localhostnginx]#vim/etc/nginx/conf.d/default.conf
修改server_name部分:server_name yytest.com;
b.重载服务
[root@localhostnginx]#/usr/sbin/nginx-sreload
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或任意路径下运行servicenginxstart(centos7是systemctlstartnginx.service)
重启:
$/usr/sbin/nginx–sreload
停止:
$/usr/sbin/nginx–sstop
测试配置文件是否正常:
$/usr/sbin/nginx–t
可能遇到的问题:
具体情况如下 1。本机能ping通虚拟机 2。虚拟机也能ping通本机 3。虚拟机能访问自己的web 4。本机无法访问虚拟己的web 这个问题的原因是服务器的80端口没有打开或防火墙没有关闭
解决办法
如果是centos6:
解决方法如下:
/sbin/iptables-IINPUT-ptcp--dport80-jACCEPT
然后保存:
/etc/rc.d/init.d/iptablessave
重启防火墙
/etc/init.d/iptablesrestart
CentOS防火墙的关闭,关闭其服务即可:
查看CentOS防火墙信息:/etc/init.d/iptablesstatus
关闭CentOS防火墙服务:/etc/init.d/iptablesstop
永久关闭防火墙:chkconfig–level35iptablesoff
如果是centos7
[root@rhel7~]#systemctlstatusfirewalld.service [root@rhel7~]#systemctlstopfirewalld.service [root@rhel7~]#systemctldisablefirewalld.service [root@rhel7~]#systemctlstatusfirewalld.service
扩展知识:
启动一个服务:systemctlstartfirewalld.service
关闭一个服务:systemctlstopfirewalld.service
重启一个服务:systemctlrestartfirewalld.service
显示一个服务的状态:systemctlstatusfirewalld.service
在开机时启用一个服务:systemctlenablefirewalld.service
在开机时禁用一个服务:systemctldisablefirewalld.service
查看服务是否开机启动:systemctlis-enabledfirewalld.service;echo$?
查看已启动的服务列表:systemctllist-unit-files|grepenabled
第二种方式:通过手动下载安装包解压安装
1.下载nginx包。
[root@localhost~]#wgethttp://nginx.org/download/nginx-1.10.1.tar.gz
2.复制包到你的安装目录
[root@localhost~]#cpnginx-1.10.1.tar.gz/usr/local/
3.解压
[root@localhost~]#tar-zxvfnginx-1.10.1.tar.gz [root@localhost~]#cdnginx-1.10.1
4.启动nginx
[root@localhost~]#/usr/local/nginx/sbin/nginx
5.查看版本s
[root@localhost~]#nginx-v
6.url访问nginxlocalhost或127.0.0.1
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。