linux安装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信息

?

1

[root@localhost ~]# yum info nginx

3.使用yum安装ngnix

?

1

[root@localhost ~]# yum install nginx

效果如下:

?

1

[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

?

1

[root@localhost ~]# service nginx start

5.查看nginx版本

?

1

[root@localhost ~]# nginx -v

6.访问nginx,现在你可以通过公网ip (本地可以通过 localhost /或 127.0.0.1 ) 查看nginx 服务返回的信息。

?

1

[root@localhost ~]# curl -i localhost

效果如下:

      ······
Welcome to nginx!。
      ······

7.nginx配置文件位置在/etc/nginx/

?

1

2

3

4

5

6

7

8

9

10

11

12

[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配置文件

?

1

[root@localhost nginx]# vim /etc/nginx/conf.d/default.conf

修改server_name部分:server_name  yytest.com;

b.重载服务

?

1

[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 )

 

重启:

?

1

$ /usr/sbin/nginx –s reload

停止:

?

1

$ /usr/sbin/nginx –s stop

测试配置文件是否正常:

?

1

$ /usr/sbin/nginx –t

可能遇到的问题:

具体情况如下  1。本机能ping通虚拟机  2。虚拟机也能ping通本机  3。虚拟机能访问自己的web  4。本机无法访问虚拟己的web  这个问题的原因是服务器的80端口没有打开或防火墙没有关闭
解决办法

如果是centos6:

解决方法如下: 

?

1

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT

然后保存: 

?

1

/etc/rc.d/init.d/iptables save

重启防火墙 

?

1

/etc/init.d/iptables restart

CentOS防火墙的关闭,关闭其服务即可: 

查看CentOS防火墙信息:/etc/init.d/iptables status 

关闭CentOS防火墙服务:/etc/init.d/iptables stop 

永久关闭防火墙: chkconfig –level 35 iptables off

如果是centos7

?

1

2

3

4

5

6

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux安装Nginx有几种方法。一种方法是通过包管理器进行安装。您可以使用命令"yum install nginx"来安装最新的稳定版本,默认情况下安装的是Nginx 1.20.2版本。另外一种方法是通过源码编译安装Nginx。这需要下载Nginx安装包并解压缩,然后进行依赖安装和配置。具体步骤如下: 1. 下载Nginx安装包,并解压缩。 2. 安装Nginx的依赖包。 3. 进入解压缩后的Nginx目录,执行"./configure"命令进行配置。 4. 执行"make"命令进行编译。 5. 执行"make install"命令进行安装。 6. 修改Nginx的配置文件"nginx.conf",可以设置用户和用户组等参数。 7. 启动Nginx,可以使用命令"nginx"。 8. 停止或重启Nginx,可以使用命令"nginx -s stop"和"nginx -s reload"。 9. 设置Nginx开机自启动,可以将Nginx添加到系统的启动项中。 10. 配置防火墙,确保80端口开放以允许Nginx的HTTP访问。 根据您的需求和喜好,您可以选择适合您的安装方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Linux安装nginx详细步骤](https://blog.csdn.net/adaizzz/article/details/126669430)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [linux 系统下四种nginx安装方法](https://blog.csdn.net/shallow72/article/details/123878716)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值