# 参考链接

  • https://www.runoob.com/linux/nginx-install-setup.html

# 安装过程

# 安装依赖

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

# 安装 pcre

PCRE 作用是让 Nginx 支持 Rewrite 功能。

cd /usr/local/src/
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure
make && make install
## 查看版本判断是否安装成功
pcre-config --version

# 安装 nginx

cd /usr/local/src/
sudo -s wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
make && make install
## 查看版本判断是否安装成功
/usr/local/webserver/nginx/sbin/nginx -v

# nginx 配置

# 设置环境变量

vi /etc/profile
#行尾加上如下
export  PATH="$PATH:/usr/local/webserver/nginx/sbin"
source /etc/profile

# 创建 Nginx 运行使用的用户 www

/usr/sbin/groupadd www
/usr/sbin/useradd -g www www

# 配置文件 (部分)

cat /usr/local/webserver/nginx/conf/nginx.conf
####################################################################################
http{
  
user www www;
worker_processes auto; #设置值和 CPU 核心数一致
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}
#vhost 
include vhosts/*.conf; 
}

vhost 配置,例:我的 php tp5 框架 常用配置
/usr/local/webserver/nginx/conf/vhost/app.conf

server {
    listen 80;
    server_name  _;
    root   /data/www/om/public;
    index  index.php index.html index.htm;
    gzip  on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 9;
    gzip_types       text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php application/javascript application/json;
    gzip_disable "MSIE [1-6]\.";
    gzip_vary on;
    location / {
                #主要是这一段一定要确保存在
                if (!-e $request_filename) {
                    rewrite  ^(.*)$  /index.php?s=/$1  last;
                    break;
                }
                #结束
                #autoindex  on;
    }
    location ~ /(.+\.php)(/.+)*$ {
        # try_files $uri =404;
        # rewrite /(.+\.php)(/.+)*$ /hello.html last;
        # try_files $uri =404;
        client_max_body_size 10m;
        fastcgi_pass_header Authorization;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        # fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/../:/tmp/:/proc/";
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$
    {
          #允许跨域请求
          add_header Access-Control-Allow-Origin '*';
          add_header Access-Control-Allow-Headers X-Requested-With;
          add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
          expires 30d;
          access_log off;
    }
}