Centos Nginx 编译安装与.cnf配置

Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。
在高连接并发的情况下,Nginx是Apache服务器不错的替代品。

Nginx 安装

[root@bogon src]# cat /etc/redhat-release

系统平台:CentOS Linux release 7.6.1810 (Core) 64位。

一、安装编译工具及库文件


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

二、首先要安装 PCRE


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

1、下载 PCRE 安装包,下载地址: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

[root@bogon src]# cd /usr/local/src/
[root@bogon src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

2、解压安装包:

[root@bogon src]# tar zxvf pcre-8.35.tar.gz

3、进入安装包目录

[root@bogon src]# cd pcre-8.35

4、编译安装

[root@bogon pcre-8.35]# ./configure
[root@bogon pcre-8.35]# make && make install

5、查看pcre版本

[root@bogon pcre-8.35]# pcre-config --version

三、安装 Nginx


1、下载 Nginx,下载地址:http://nginx.org/download/nginx-1.16.0.tar.gz

[root@bogon src]# cd /usr/local/src/
[root@bogon src]# wget https://nginx.org/download/nginx-1.16.0.tar.gz

2、解压安装包

[root@bogon src]# tar zxvf nginx-1.16.0.tar.gz

3、进入安装包目录

[root@bogon src]# cd nginx-1.16.0

4、编译安装(/usr/local/webserver/nginx 为编译安装目录可根据自己需求更改)

[root@bogon nginx-1.16.0]# ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
[root@bogon nginx-1.16.0]# make && make install

5、查看nginx版本

[root@bogon nginx-1.16.0]# /usr/local/webserver/nginx/sbin/nginx -v

到此,nginx安装完成。 冯奎博客

Nginx 配置

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

[root@bogon conf]# /usr/sbin/groupadd www 
[root@bogon conf]# /usr/sbin/useradd -g www www

配置 nginx.conf ,添加虚拟站点,首先打开 nginx.conf

[root@bogon conf]# cd /usr/local/webserver/nginx/conf
[root@bogon conf]# vi nginx.conf

http中最后加上 include /usr/local/webserver/nginx/conf/vhost/*.conf;
冯奎博客 nginx/conf 中新建 vhost/ceshi.test.conf:

[root@bogon conf]# mkdir vhost
[root@bogon conf]# cd vhost
[root@bogon conf]# touch ceshi.test.conf

ceshi.test.conf内容如下:

#server # 单站点开启此处
#server {
#    listen   80;                            #监听端口设为 80。
#    server_name  ceshi.test;             #绑定您的域名。
#    index index.htm index.html index.php;   #指定默认文件。
#    root /usr/local/webserver/nginx/html;   #指定网站根目录。
#    #include location.conf;                 #当您需要调用其他配置文件时才粘贴此项,如无需要,请删除此项。
#}

server {
    listen 80;
    listen 443 ssl;
    server_name ceshi.test;
    root /var/www/ceshi/;

    index index.html index.htm index.php; # 定义首页文件

    location / {
        #try_files $uri $uri/ /index.php$is_args$args;
        #autoindex on;
        #主要是这一段一定要确保存在(隐藏index.php)
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=/$1  last;
            break;
        }
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;  	# 脚本文件请求的路径
        include        fastcgi_params; # 设置代理
    }

    # Enable SSL
    ssl_certificate ssl/cert.pem; # SLL证书位置(相对目录也可换成绝对路径)
    ssl_certificate_key ssl/cert.key; # SLL证书位置(相对目录也可换成绝对路径)
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
    ssl_prefer_server_ciphers on;

    charset utf-8;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    location ~ /\.ht {
        deny all;
    }

    # 设置访问日志位置
    access_log  /usr/local/webserver/nginx/logs/ceshi.test.log;
    error_log  /usr/local/webserver/nginx/logs/ceshi.test.error.log;
}

# This file is auto-generated.
# If you want Laragon to respect your changes, just remove the [auto.] prefix

启动 Nginx

Nginx 启动命令如下:

[root@bogon conf]# /usr/local/webserver/nginx/sbin/nginx

Nginx 其他命令
以下包含了 Nginx 常用的几个命令:

/usr/local/webserver/nginx/sbin/nginx -s reload            # 重新载入配置文件
/usr/local/webserver/nginx/sbin/nginx -s reopen            # 重启 Nginx
/usr/local/webserver/nginx/sbin/nginx -s stop              # 停止 Nginx

如果配置完后无法访问此网站,那么就要查看防火墙的状态,查看安全组是否添加80端口

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT

fastcgi.conf 文件内容

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;  	# 脚本文件请求的路径
fastcgi_param  QUERY_STRING       $query_string;						# 请求的参数;如?app=123
fastcgi_param  REQUEST_METHOD     $request_method;						# 请求的动作(GET,POST)
fastcgi_param  CONTENT_TYPE       $content_type;						# 请求头中的Content-Type字段
fastcgi_param  CONTENT_LENGTH     $content_length;						# 请求头中的Content-length字段。					

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;					# 脚本名称
fastcgi_param  REQUEST_URI        $request_uri;							# 请求的地址不带参数
fastcgi_param  DOCUMENT_URI       $document_uri;						# 与$uri相同。
fastcgi_param  DOCUMENT_ROOT      $document_root;						# 网站的根目录。在server配置中root指令中指定的值
fastcgi_param  SERVER_PROTOCOL    $server_protocol;						# 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
fastcgi_param  REQUEST_SCHEME     $scheme;								# 请求-响应协议
fastcgi_param  HTTPS              $https if_not_empty;					# https加密访问后出现的问题(有https协议时才自动使用 https on,否则忽略这个参数)

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;								# cgi 版本
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;					# nginx 版本号,可修改、隐藏

fastcgi_param  REMOTE_ADDR        $remote_addr;							# 客户端IP
fastcgi_param  REMOTE_PORT        $remote_port;							# 客户端端口
fastcgi_param  SERVER_ADDR        $server_addr;							# 服务器IP地址
fastcgi_param  SERVER_PORT        $server_port;							# 服务器端口
fastcgi_param  SERVER_NAME        $server_name;							# 服务器名,域名在server配置中指定的server_name

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

冯奎博客
请先登录后发表评论
  • latest comments
  • 总共0条评论