Centos7虚拟机安装nginx

通过yum安装nginx

1. 把 nginx的 包路径信息添加到 /etc/yum.repos.d/nginx.repo:

[root@localhost yum.repos.d]# pwd

/etc/yum.repos.d

[root@localhost yum.repos.d]# ls

CentOS-Base.repo  CentOS-Debuginfo.repo      CentOS-fasttrack.repo  CentOS-Sources.repo  nginx.repo

CentOS-CR.repo    CentOS-Debuginfo.repo.org  CentOS-Media.repo      CentOS-Vault.repo

[root@localhost yum.repos.d]# cat nginx.repo

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/centos/7/x86_64/

gpgcheck=0

enabled=1

2:yum install nginx -y

[root@localhost ~]# yum install nginx -y

Note:如果需要设置代理, 

[root@localhost download]# export http_proxy="http://[proxyip]:[port]";yum install nginx -y

已加载插件: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

完毕!

3.修改端口( 修改成8009,默认为80)

[root@localhost conf.d]# pwd

/etc/nginx/conf.d

[root@localhost conf.d]# vi default.conf

4. 启动nginx:  service nginx start

安装完成,下面直接就可以启动Nginx了:

service nginx start

(其它相关命令:service nginx start/stop)

5. 从虚拟机访问查看是否已启动成功

curl -i localhost:8009

result is like:  "welcome to nginx **** thank you for using nginx "

===> nginx已启动成功

6. 设置从windows主机访问:

 a) 打开IE,输入 192.168.1.111:8009 , 无法访问

b) 返回虚拟机,防火墙设置

[root@localhost init.d]# firewall-cmd --query-port=8009/tcp

no

[root@localhost init.d]# firewall-cmd --add-port=8009/tcp

success

c) 返回IE, 输入 192.168.1.111:8009,现在可以了

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.

For online documentation and support please refer tonginx.org.

Commercial support is available atnginx.com.

Thank you for using nginx.

永久停掉防火墙:

[root@localhost yum.repos.d]# service firewalld stop

Redirecting to /bin/systemctl stop firewalld.service

[root@localhost yum.repos.d]# systemctl disable firewalld.service

Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.

Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

2.测试nginx处理静态文件:

在firefox输入: http://192.168.1.113/media/xdd/22.mp3

 一刚开始无法正常播放,需要做几点设置:

1) 查找配置文件目录, 为  /etc/nginx/nginx.conf

[root@localhost nginx]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

2) 修改“user nginx;”==>"user root;" in /etc/nginx/nginx.conf

3) 修改 /etc/nginx/conf.d/default.conf:

#location / {

    #    root  /usr/share/nginx/html;

    #    index  index.html index.htm;

    #}

  location /media/ {

    root /root/myweb/;

    #alias /root/myweb/media/;

    autoindex on;

    #index 22.mp3;

  }

4.修改SElinux:  关闭selinux服务

[root@localhost ~]# vi /etc/selinux/config

#SELINUX=enforcing  改为  SELINUX=disabled

note:修改配置文件必须 重启系统 才能生效:

       /usr/sbin/sestatus -v|grep -i SELINUX 可以查看状态。

[root@localhost xdd]# setenforce 0  (临时设置生效)

你可能感兴趣的:(Centos7虚拟机安装nginx)