centos7 安装nginx

安装编译工具以及库文件

  yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

安装wget、pcre、pcre-deve

  yum -y install wget pcre pcre-deve

安装nginx

  # 下载 
  wget https://nginx.org/download/nginx-1.20.1.tar.gz  // 可去官网找对应的版本号,现在1.20.1
  tar -xvf nginx-1.20.1.tar.gz
  cd nginx-1.15.9
  # 编译安装
    // 方法一(简单)
      ./configure && make && make install //安装在/usr/local/下
    // 方法二(配置安装路径):
      ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-        pcre=/usr/local/src/pcre-8.42 
    make && make install
  # 查看版本
  /usr/local/nginx/sbin/nginx -v

nginx配置

#创建 Nginx 运行使用的用户 www
/usr/sbin/groupadd www 
/usr/sbin/useradd -g www www
#设置包含多个配置文件,在nginx.conf底部添加
include extra/*.conf;
#启动
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s reload            # 重新载入配置文件
/usr/local/nginx/sbin/nginx -s reopen            # 重启 Nginx
/usr/local/nginx/sbin/nginx -s stop              # 停止 Nginx
# /usr/local/nginx/conf/extra/demo.tilesrow.com.conf 
server
    {
        listen 80;  // 监听端口号
        #listen [::]:80;
        server_name  xx.com; // 域名
        index index.html index.htm index.php default.html default.htm default.php; // 文件所放地址
        root  /data/project/demo1;  // 根路径
        access_log  /usr/local/webserver/nginx/logs/xx.com.log; // 日志
    }

内网映射端口配置

server {
        listen       8000;
        server_name  localhost;
        client_max_body_size 50M;
        access_log  /data/nginx/logs/common.access.log  main;

        location /mm {
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://127.0.0.1:8001;
        }
        location /datav/screen {
            gzip on;
            gzip_comp_level 3;
            gzip_types text/css application/json application/javascript application/octet-stream application/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png image/x-ms-bmp;
            alias  /data/projectname/front/datav/;
            index  index.html index.htm;
            try_files $uri $uri/ index.html;
        }
    }

查看是否安装成功

  ps -ef | grep nginx

安装过程中遇到的问题

  • nginx安装完成后,无法通过ip访问
    • 原因:centos的防火墙改成了firewall,不再叫iptables
  • 解决办法如下:
  firewall-cmd --zone=public --add-port=80/tcp --permanent  
  命令含义:
  –zone #作用域
  –add-port=80/tcp #添加端口,格式为:端口/通讯协议
  –permanent #永久生效,没有此参数重启后失效
  重启防火墙
  systemctl stop firewalld.service  
  systemctl start firewalld.service 

你可能感兴趣的:(centos7 安装nginx)