安装编译工具及库文件

cd /usr/local/
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

安装 PCRE

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

  1. 下载 PCRE 安装包
cd /usr/local/src/
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
  1. 解压安装包
tar zxvf pcre-8.35.tar.gz -C ..
  1. 进入安装包目录
cd pcre-8.35
  1. 编译安装
./configure
make && make install
  1. 查看pcre版本
pcre-config --version

image

安装 Nginx

  1. 下载 Nginx
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.6.2.tar.gz
  1. 解压安装包
tar zxvf nginx-1.6.2.tar.gz -C ..
  1. 进入安装包目录
cd nginx-1.6.2
  1. 编译安装
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.35
make
make install
  1. 查看nginx版本
/usr/local/nginx/sbin/nginx -v

image

到此,nginx安装完成。

Nginx 配置

  1. 创建 Nginx 运行使用的用户和用户组
/usr/sbin/groupadd nginx 
/usr/sbin/useradd -g nginx nginx
  1. 配置nginx.conf,将/usr/local/nginx/conf/nginx.conf替换为以下内容

user  nginx nginx; #Nginx用户及组:用户 组。window下不指定
worker_processes  1; #设置值和CPU核心数一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/nginx/nginx.pid; #pid(进程标识符):存放路径。
worker_rlimit_nofile 65535; #指定进程可以打开的最大描述符:数目。


events {
    use epoll; #使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
    worker_connections  65535; #每个工作进程的最大连接数量。
}


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"';

    access_log /usr/local/nginx/logs/access.log main; #用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径;

    server_names_hash_bucket_size 128; #服务器名字的hash表大小
    client_header_buffer_size 32k; #客户端请求头部的缓冲区大小
    large_client_header_buffers 4 32k; #请求头缓冲大小
    client_max_body_size 8m; #设定通过nginx上传文件的大小
    #开启高效文件传输模式,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off
    sendfile on;
    tcp_nopush on; #防止网络阻塞
    tcp_nodelay on; #防止网络阻塞
    keepalive_timeout 120; #长连接超时时间,单位是秒
 
    #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;

    #gzip模块设置
    gzip on; #开启gzip压缩输出
    gzip_min_length 1k; #最小压缩文件大小
    gzip_buffers 4 16k; #压缩缓冲区
    gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
    gzip_comp_level 2; #压缩等级
    gzip_types text/plain application/x-javascript text/css application/xml; #压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
    gzip_vary on; #和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩


    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ico|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
        {
            expires 30d;
        }

        location ~ .*.(js|css)$
        {
            expires 30d;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

  1. 检查配置文件nginx.conf的正确性
/usr/local/nginx/sbin/nginx -t

image

启动 Nginx

  1. 启动命令
/usr/local/nginx/sbin/nginx 
  1. 查看是否启动
ps -ef | grep nginx

image

  1. 访问站点
curl http://localhost

Nginx 其他命令

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

Nginx报错

端口被占用错误

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

说明80端口被占用了,更改nginx监听的端口或者释放80端口。

PID错误

启动nginx报以下错误:

nginx: [error] invalid PID number "" in "/usr/local/nginx/nginx.pid"

解决方法:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx -s reload

参考文档

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

原文链接:https://miansen.wang/2018/08/25/2/