四、Centos7.2 上安装Nginx

1、准备工作

1)关闭iptables

iptables -t nat -F

查看iptables

iptables -t nat -L

2)关闭selinux 查看操作

setenforce 

关闭操作

setenforce 0

3)初始化

cd /opt;mkdir app download logs work backup

4)确认gcc yum源可用

yum list|grep gcc

5)两项安装

yum -y install gcc gcc-c++ autoconf pcre pcre-devel make auromake

yum -y install wget httpd-tools vim

2.利用网络,构建nginx的yum仓库。

vim /etc/yum.repos.d/nginx.repo

然后 访问http://nginx.org/en/linux_packages.html#stable

复制其中

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1
修改成
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

完成后保存退出

3.yum install nginx 即可安装完成

备注:

1)查看nginx编译配置参数:nginx -V

2)查看nginx版本号:nginx-v

3) 查看nginx安装目录:rpm -ql nginx
 发现可以在/etc/nginx/conf.d配置nginx

4.查看nginx站点

这里需要设置防火墙开放80端口

firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙  systemctl start firewalld.service  
备注: 关闭防火墙命令  systemctl stop firewalld.service 

启动nginx服务

nginx

重启nginx服务

systemctl restart nginx.service

停止nginx服务

nginx -s stop
nginx -s quit

nginx停止命令stop与quit参数的区别在于stop是快速停止nginx,可能并不保存相关信息,quit是完整有序的停止nginx,并保存相关信息

5.备份重要的配置文件

cp /etc/nginx/nginx.conf /opt/backup/nginx.conf
cp /etc/nginx/conf.d/default.conf /opt/backup/default.conf
  1. 设置nginx的配置文件
$ vi /etc/nginx/nginx.conf  (修改配置文件,全局配置文件)
      #开启进程数 <=CPU数 
      worker_processes  1;
      #自定义错误日志保存位置,全局设置,默认logs/error.log
      #error_log  logs/error.log;
   #每个进程最大连接数(最大连接=连接数x进程数)每个worker允许同时产生多少个链接,默认1024
      events {
          worker_connections  1024;
      }
      #
      http {
          #文件扩展名与文件类型映射表
          include       mime.types;
          #默认文件类型
          default_type  application/octet-stream;
          #自定义日志文件输出格式 全局设置
          log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
          #自定义全局请求日志保存位置,全局设置,默认logs/access.log, 定义格式:文件存储位置 + 日志输出格式
          #access_log  logs/access.log  main;
          #打开发送文件
          sendfile        on;
          #连接超时时间
          #keepalive_timeout  0;
          keepalive_timeout  65;
          #打开gzip压缩
          #gzip  on;
          #配置虚拟主机,基于域名、ip和端口,可以配置多个server
          server {
              #监听端口,可以是ip:port 或者 port
              listen       80;
              #监听域名,可以是ip或者域名,server_name有三种匹配方式:精准匹配(www.domain.com)、通配符匹配(*.domain.com 、www.*)、正则表达式匹配(~^(?.+)\.domain\.com$)
              server_name  localhost:8080;
              #自定义请求日志,局部,当前server有效
              #access_log  logs/host.access.log  main;
              #错误页面及其返回地址
              error_page   500 502 503 504  /50x.html;
              #请求匹配,同样有精准(= /index.html)、模糊(~ index)、正则,允许配置多个location
              location / {
                  #返回根路径地址(相对路径:相对于/usr/local/nginx/)
                  root   html;
                  #默认主页
                  index  index.html index.htm;
                  proxy_pass http://localhost:8080
              }
              #
              location /html {
                  root   html;
                  index  index.html index.htm;
              }
              #
          }
          #
      }

转载于:https://my.oschina.net/words/blog/1925560

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值