Linux下部署Nginx环境

方式1 安装Nginx源

  • sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@izm5e33l0ge76uqi8nq87az /]# sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
获取http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
警告:/var/tmp/rpm-tmp.j7U0Sv: 头V4 RSA/SHA1 Signature, 密钥 ID 7bd9bf62: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
[root@izm5e33l0ge76uqi8nq87az /]# 
  • 查看 Nginx 源是否配置成功
[root@izm5e33l0ge76uqi8nq87az /]# cd /etc/yum.repos.d/
[root@izm5e33l0ge76uqi8nq87az yum.repos.d]# ls
CentOS-Base.repo      mysql-community.repo         nodesource-el7.repo
epel.repo             mysql-community-source.repo
mongodb-org-4.0.repo  nginx.repo
  • 安装Nginx
[root@izm5e33l0ge76uqi8nq87az yum.repos.d]# cd /
[root@izm5e33l0ge76uqi8nq87az /]# yum install -y nginx
已加载插件:fastestmirror
nginx                                                    | 2.9 kB     00:00     
nginx/x86_64/primary_db                                    |  45 kB   00:22     
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 nginx.x86_64.1.1.16.0-1.el7.ngx 将被 安装
--> 解决依赖关系完成

依赖关系解决

================================================================================
 Package       架构           版本                          源             大小
================================================================================
正在安装:
 nginx         x86_64         1:1.16.0-1.el7.ngx            nginx         766 k

事务概要
================================================================================
安装  1 软件包

总下载量:766 k
安装大小:2.7 M
Downloading packages:
nginx-1.16.0-1.el7.ngx.x86_64.rpm                          | 766 kB   01:14     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 数据库已被非 yum 程序修改。
  正在安装    : 1:nginx-1.16.0-1.el7.ngx.x86_64                             1/1 
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* http://nginx.org/en/support.html

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
  验证中      : 1:nginx-1.16.0-1.el7.ngx.x86_64                             1/1 

已安装:
  nginx.x86_64 1:1.16.0-1.el7.ngx                                               

完毕!
[root@izm5e33l0ge76uqi8nq87az /]# 
  • 启动 Nginx 并设置开机自动运行
[root@izm5e33l0ge76uqi8nq87az /]# systemctl start nginx
[root@izm5e33l0ge76uqi8nq87az /]# systemctl enable nginx
  • 开启80端口
firewall-cmd --zone=public --list-ports
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --list-ports
27017/tcp 9093/tcp 8000/tcp 5000/tcp 3306/tcp 8080/tcp
  • 打开浏览器 显示Welcome to nginx! 即配置nginx成功
  • Nginx 反向代理配置
  • 关闭 Selinux
vi etc/selinux/config
修改 SELINUX=enforcing 为 SELINUX=disabled 
必须重启 linux init 6
  • 配置反向代理
[root@izm5e33l0ge76uqi8nq87az /]# cd /etc/nginx/conf.d
[root@izm5e33l0ge76uqi8nq87az conf.d]# ls
default.conf
配置default.conf的信息
server {
listen 80;
server_name www.bbb.com; location / {
#设置主机头和客户端真实地址,以便服务器获取客户端真实 IP proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #禁用缓存
proxy_buffering off;
#反向代理的地址
proxy_pass http://127.0.0.1:3001; }
}
或者
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root /work/build/build;
        index index.html index.htm;

    }
  • 检查并重启nginx
[root@izm5e33l0ge76uqi8nq87az conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@izm5e33l0ge76uqi8nq87az conf.d]# systemctl stop nginx
[root@izm5e33l0ge76uqi8nq87az conf.d]# systemctl start nginx

方式2 安装Nginx源

  • 安装epel库
  • yum install epel-release -y
[root@iZm5e33l0ge76uqi8nq87aZ /]# yum install epel-release -y
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 epel-release.noarch.0.7-11 将被 安装
--> 解决依赖关系完成
依赖关系解决
======================================================================================================
 Package                      架构                   版本                   源                    大小
=======================================================================================================
正在安装:
 epel-release                 noarch                 7-11                   epel                  15 k
事务概要
=======================================================================================================
安装  1 软件包
总下载量:15 k
安装大小:24 k
Downloading packages:
epel-release-7-11.noarch.rpm                                                    |  15 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : epel-release-7-11.noarch                                                           1/1 
警告:/etc/yum.repos.d/epel.repo 已建立为 /etc/yum.repos.d/epel.repo.rpmnew 
  验证中      : epel-release-7-11.noarch                                                           1/1 
已安装:
  epel-release.noarch 0:7-11                                                                           
完毕!
  • 安装nginx
  • yum -y install nginx
  验证中      : gperftools-libs-2.6.1-1.el7.x86_64                                               20/25 
  验证中      : libX11-common-1.6.5-2.el7.noarch                                                 21/25 
  验证中      : freetype-2.8-12.el7_6.1.x86_64                                                   22/25 
  验证中      : 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64                                        23/25 
  验证中      : 1:nginx-mod-http-image-filter-1.12.2-2.el7.x86_64                                24/25 
  验证中      : freetype-2.4.11-15.el7.x86_64                                                    25/25 

已安装:
  nginx.x86_64 1:1.12.2-2.el7                                                                          
作为依赖被安装:
  dejavu-fonts-common.noarch 0:2.33-6.el7        dejavu-sans-fonts.noarch 0:2.33-6.el7                
  fontconfig.x86_64 0:2.13.0-4.3.el7             fontpackages-filesystem.noarch 0:1.44-8.el7          
  gd.x86_64 0:2.0.35-26.el7                      gperftools-libs.x86_64 0:2.6.1-1.el7                 
  libX11.x86_64 0:1.6.5-2.el7                    libX11-common.noarch 0:1.6.5-2.el7                   
  libXau.x86_64 0:1.0.8-2.1.el7                  libXpm.x86_64 0:3.5.12-1.el7                         
  libjpeg-turbo.x86_64 0:1.2.90-6.el7            libpng.x86_64 2:1.5.13-7.el7_2                       
  libxcb.x86_64 0:1.13-1.el7                     libxslt.x86_64 0:1.1.28-5.el7                        
  nginx-all-modules.noarch 1:1.12.2-2.el7        nginx-filesystem.noarch 1:1.12.2-2.el7               
  nginx-mod-http-geoip.x86_64 1:1.12.2-2.el7     nginx-mod-http-image-filter.x86_64 1:1.12.2-2.el7    
  nginx-mod-http-perl.x86_64 1:1.12.2-2.el7      nginx-mod-http-xslt-filter.x86_64 1:1.12.2-2.el7     
  nginx-mod-mail.x86_64 1:1.12.2-2.el7           nginx-mod-stream.x86_64 1:1.12.2-2.el7               
作为依赖被升级:
  freetype.x86_64 0:2.8-12.el7_6.1                                                                     

完毕!
  • 启动nginx

  • systemctl start nginx.service
    打开IP地址


    nginx.png
  • 创建网站根目录

  • mkdir -p /var/www/test/public_html

  • 编辑根目录下的文件

  • vim /var/www/test/public_html/index.html


      
              

Hello World

  • 添加虚拟主机配置文件
  • vim /etc/nginx/conf.d/vitual.conf
server{
listen 80;
server_name 主机地址;
root var/www/test/public_html;
index index.html;
}
  • 查看nginx状态 nginx -t
[root@iZm5e33l0ge76uqi8nq87aZ conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
  • 重启systemctl restart nginx.service
  • 打开网址 配置成功


    nginx.png

你可能感兴趣的:(Linux下部署Nginx环境)