阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx安装与配置

148次阅读
没有评论

共计 3258 个字符,预计需要花费 9 分钟才能阅读完成。

Nginx 的安装过程比较简单,但是它的配置相对复杂点。特别是刚接触和使用 nginx 的时候,不明白里面的参数的含义,这时候需要去阅读 nginx 的相关文档。下面的安装过程和配置参数均在生产服务器上实验过多次。

1、安装 nginx

Nginx 下载地址:http://nginx.org/en/download.html

先要安装 pcreopensslzib 等模块

yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
./configure --user=daemon --group=daemon --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make -j 8
make install

2、修改 conf/nginx.conf 里面的默认参数

# nginx.conf
user daemon daemon;
worker_processes 8;
worker_rlimit_nofile 65536;
pid /usr/local/nginx/nginx.pid;
error_log /usr/local/nginx/logs/nginx_error.log info;

events
{
    use epoll;
    worker_connections 65536;
}

http
{
    include mime.types;
    default_type application/octet-stream;
    #charset gb2312;

    server_names_hash_bucket_size 128;
    client_header_buffer_size 320k;
    large_client_header_buffers 4 320k;
    client_max_body_size 8m;

    sendfile on;
    tcp_nopush on;
   
    keepalive_timeout 6000s;

    tcp_nodelay on;

    fastcgi_connect_timeout 600s;
    fastcgi_send_timeout 600s;
    fastcgi_read_timeout 600s;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 8 128k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-Javascript text/css application/xml application/json audio/aac;
    gzip_vary on;

    # Load config files from the directory
    include /usr/local/nginx/sites-enabled/*.conf;
}

上面的配置是 nginx 的全局配置,如 nginx 的总进程数、打开的文件数据、I/ O 方式采用 epoll,gzip 压缩参数。
注意上面的最后一行:include /usr/local/nginx/sites-enabled/*.conf;
采用了类似与 Apache 的目录结构,这样可以针对不同的 Web 应用设置不同的参数,也方便管理。

3、设置具 Web 应用参数
在 sites-enabled 里面设置针对每个 Web 应用的配置参数,主要是基本的虚拟主机配置(server),包括 Web 应用根目录,侦听端口,ServerName,CGI,日志等。
下面是一个常见的虚拟主机 vhost 的配置,其中包括 php-fpm CGI 的调用配置参数。

# mdba.cn.conf
server
{
    listen 80;
    server_name mdba.cn www.mdba.cn 112.124.65.179;

    root /usr/local/louvre-wp;
    index index.php;
    location ~ "\.(js|ico|gif|jpg|png|css)$" {expires 1w;}

    location / {
        fastcgi_ignore_client_abort on;
        #fastcgi_pass unix:/dev/shm/php.socket;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
        index index.php;
        if (!-e $request_filename) {
        rewrite . /index.php last;
        break;
        }
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    log_format louvre '$remote_addr - $remote_user [$time_local]"$request"''$status $body_bytes_sent "$request" "$http_referer" ''"$http_user_agent""$request_time" ';

    access_log /usr/local/nginx/logs/access_louvre.log louvre;
}

fastcgi_pass 127.0.0.1:9000; 设定的端口是 9000,设定之前检查该端口是否已经被其他服务器进程占用了。另外 http 侦听端口可以不使用默认的 80 端口。

4、启动 nginx 与测试
#- t 选项可以测试配置脚本是否有语法错误,如缺少”;”、变量未定义或者拼写有误之类的常见语法问题。 修改 nginx 配置文件,需要重启 nginx 才能使新的配置生效

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx

测试 nginx 与调用 php-fpm 是否正常工作,在上面的设定的 root 目录下面,新建 index.php 文件。

<?php
phpinfo();
?>

浏览器里面输入 112.124.65.179(这是我的阿里云主机的 IP,或者自己的本地 IP), 就以看到 php 的一些运行环境

下面关于 Nginx 的文章您也可能喜欢,不妨参考下:

CentOS 7.2 下编译安装 PHP7.0.10+MySQL5.7.14+Nginx1.10.1  http://www.linuxidc.com/Linux/2016-09/134804.htm

Linux 下编译安装 Nginx 1.8.1 及配置 http://www.linuxidc.com/Linux/2017-02/140495.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

CentOS 7.2 源码安装 Nginx http://www.linuxidc.com/Linux/2017-03/141246.htm

Nginx 的 500,502,504 错误解决方法 http://www.linuxidc.com/Linux/2015-03/115507.htm

Nginx 版本号修改隐藏及记录用户请求需要的时间  http://www.linuxidc.com/Linux/2017-02/141044.htm

CentOS 7 编译安装 Nginx1.10.2 脚本启动失败解决思路 http://www.linuxidc.com/Linux/2017-01/139794.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/141796.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计3258字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中