Nginx源码安装

安装说明

系统环境:CentOS6、CentOS7、Red Hat6、Red Hat7
软件:nginx-1.12.0
其他所需软件:openssl-1.1.0e、pcre-8.40、zlib-1.2.8
安装方式:源码编译安装
安装位置:/usr/local/nginx
下载地址:http://nginx.org/en/download.html

安装前准备工作

Tip: 在安装nginx前,需要确保系统安装了gcc、gcc-c++

安装openssl

下载地址:http://www.openssl.org/

# 解压
tar zxvf openssl-1.1.0e.tar.gz
# 进入源码目录
cd openssl-1.1.0e
# 执行配置
./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl/conf
# 编译安装
make
make install
#软链接库文件
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
# 检验安装
/usr/local/openssl/bin/openssl version -a

输出内容

OpenSSL 1.1.0e 16 Feb 2017
built on: reproducible build, date unspecified
platform: linux-x86_64
compiler: gcc -DDSO_DLFCN -DHAVE_DLFCN_H -DNDEBUG -DOPENSSL_THREADS -DOPENSSL_NO_STATIC_ENGINE -DOPENSSL_PIC -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DRC4_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DGHASH_ASM -DECP_NISTZ256_ASM -DPADLOCK_ASM -DPOLY1305_ASM -DOPENSSLDIR="\"/usr/local/openssl/conf\"" -DENGINESDIR="\"/usr/local/openssl/lib/engines-1.1\"" 
OPENSSLDIR: "/usr/local/openssl/conf"
ENGINESDIR: "/usr/local/openssl/lib/engines-1.1"

安装pcre

下载地址:http://www.pcre.org/

# 解压
tar zxvf pcre-8.40.tar.gz
# 进入源码目录
cd pcre-8.40
# 执行配置
./configure --prefix=/usr/local/pcre/
# 编译安装
make
make install

安装zlib

# 解压
tar zxvf zlib-1.2.11.tar.gz
# 进入源码目录
cd zlib-1.2.11
# 执行配置
./configure --prefix=/usr/local/zlib/
# 编译安装
make
make install

安装libunwind

# 解压
tar zxvf libunwind-1.1.tar.gz
# 进入源码目录
cd libunwind-1.1
# 执行配置
CFLAGS=-fPIC ./configure
# 编译安装
make CFLAGS=-fPIC
make CFLAGS=-fPIC install
# 更新共享库
echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
/sbin/ldconfig

安装gperftools

# 解压
tar zxvf gperftools-2.1.tar.gz
# 进入源码目录
cd gperftools-2.1
# 执行配置
./configure
# 编译安装
make
make install

安装nginx

编译安装nginx

#创建www用户
useradd www

# 解压
tar zxvf nginx-1.12.0.tar.gz

# 进入源码目录
cd nginx-1.12.0

# 执行配置
./configure \
--user=www \
--group=www \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-google_perftools_module \
--with-http_addition_module \
--with-openssl=/usr/local/src/openssl-1.1.0e \
--with-pcre=/usr/local/src/pcre-8.40 \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-http_sub_module \
--with-threads

# 编译安装
make
make install

配置nginx

创建相关目录和赋予权限

mkdir -p /var/log/nginx
mkdir -p /usr/local/nginx/conf/websites
mkdir -p /var/htdocs
chmod +w /var/log/nginx
chown -R www:www /var/log/nginx

配置nginx.conf

mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf
user  www www;
worker_processes 24;
error_log  /var/log/nginx/error.log  crit;
pid        /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535;

events {
   use epoll;
   worker_connections  65535;
}

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

   server_names_hash_bucket_size 128;
   client_header_buffer_size 32k;
   large_client_header_buffers 1 128k;
   client_max_body_size 8m;

   sendfile        on;
   tcp_nopush     on;

   keepalive_timeout  60;

   tcp_nodelay on;

   fastcgi_connect_timeout 300;
   fastcgi_send_timeout 300;
   fastcgi_read_timeout 300;
   fastcgi_buffer_size 128k;
   fastcgi_buffers 8 128k;
   fastcgi_busy_buffers_size 256k;
   fastcgi_temp_file_write_size 256k;
   fastcgi_intercept_errors on;
   send_timeout 60;

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

   proxy_ignore_client_abort off;
   proxy_set_header          X-Forwarded-For remote_addr;     #limit_zone  crawlerbinary_remote_addr  10m;
   #error_page 403 http://error.cnbb.com.cn/403.htm;
   #error_page 404 http://error.cnbb.com.cn/404.htm;

   #############################
   include websites/*.conf;
   #############################
}

配置fcgi.conf

vim /usr/local/nginx/conf/fcgi.conf
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/nginx_version;  fastcgi_param  QUERY_STRINGquery_string;
fastcgi_param  REQUEST_METHOD     request_method; fastcgi_param  CONTENT_TYPEcontent_type;
fastcgi_param  CONTENT_LENGTH     content_length;  fastcgi_param  SCRIPT_FILENAMEdocument_rootfastcgi_script_name; fastcgi_param  SCRIPT_NAMEfastcgi_script_name;
fastcgi_param  REQUEST_URI        request_uri; fastcgi_param  DOCUMENT_URIdocument_uri;
fastcgi_param  DOCUMENT_ROOT      document_root; fastcgi_param  SERVER_PROTOCOLserver_protocol;

fastcgi_param  REMOTE_ADDR        remote_addr; fastcgi_param  REMOTE_PORTremote_port;
fastcgi_param  SERVER_ADDR        server_addr; fastcgi_param  SERVER_PORTserver_port;
fastcgi_param  SERVER_NAME        $server_name;

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

生效配置

ulimit -SHn 65535
/usr/local/nginx/sbin/nginx

生成启动脚本

【centos 6.x 启动脚本】

vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: 2345 58 74
# description: nginx web server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

if [ -f /etc/sysconfig/nginx ];then
. /etc/sysconfig/nginx
fi

RETVAL=0
prog="nginx"
start() {
# Check that networking is up.
[ "NETWORKING" = "no" ] && exit 1 echo -n"Starting prog: " daemon /usr/local/nginx/sbin/nginxOPTIONS
RETVAL=? echo [RETVAL -eq 0 ] && touch /var/lock/subsys/nginx
return RETVAL }  stop() { echo -n"Shutting down prog: " killproc /usr/local/nginx/sbin/nginx RETVAL=?
echo
[ RETVAL -eq 0 ] && rm -f /var/lock/subsys/nginx returnRETVAL
}

# See how we were called.
case "1" in start)        start ;;  stop)        stop ;;  status)        status nginx        RETVAL=?
;;

restart|reload)
       stop
       start
       RETVAL=? ;;  condrestart)        if [ -f /var/lock/subsys/nginx ]; then                stop                start                RETVAL=?
       fi
;;

*)
       echo "Usage:0 {start|stop|restart|condrestart|status}"
       RETVAL=3
       esac
       exit $RETVAL

设置脚本开机启动

chmod a+x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on

【centos 7.x 启动脚本】

vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP MAINPID ExecStop=/bin/kill -s QUITMAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

设置生效并开机启动

systemctl daemon-reload
systemctl enable nginx.service

配置/etc/sysctl.conf

追加到/etc/sysctl.conf的内容

net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

net.ipv4.ip_local_port_range = 1024  65535

使之生效

/sbin/sysctl -p

Tip: 如果是docker容器,此内容请添加到宿主机中。

配置默认虚拟主机

vim /usr/local/nginx/conf/websites/default.conf
server
{
        listen       80;
        #server_name  127.0.0.1;
        index index.html;
        root  /var/htdocs;

        location ~ ^/(status|ping)$
        {
                fastcgi_pass  127.0.0.1:9000;
                include fcgi.conf;
                allow 127.0.0.1;
                deny all;
        }

        location /nginx_status
        {
                stub_status on;
                access_log off;
                allow 127.0.0.1;
                deny all;
        }

        access_log  /var/log/nginx/logs_default.log;
}

启动nginx

【centos 6.x】

service nginx start

【centos 7.x】

systemctl start nginx

发表评论