Linux 下源码编译安装 Nginx
1.0.1 Nginx 编译源码所需的工具和库
yum install -y gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel patch
1.0.2 编译安装 libevent-2.1.8-stable.tar.gz
mkdir lnmp cd lnmp/ wget https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz tar zxvf libevent-2.1.8-stable.tar.gz cd libevent-2.1.8-stable ./configure make -j 2 && make install
1.0.3 编译安装 tmux-2.3.tar.gz
wget https://github.com/tmux/tmux/releases/download/2.3/tmux-2.3.tar.gz tar zxvf tmux-2.3.tar.gz cd tmux-2.3 / CFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib" ./configure make -j 2 && make install unset LDFLAGS ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5 # 注意: 64位系统 用这个 ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5 # 注意: 32位系统 用这个
1.0.4 设置 nginx 用户和组
#新增nginx用户组
groupadd www
#创建用户 设置不允许登陆系统 -M不创建家目录,/dev/null 首先表示标准输出重定向到空设备文件
useradd -M www -s /sbin/nologin -d /dev/null -g www
1.0.5 新建Nginx安装目录,并设置权限
mkdir -p /usr/local/nginx # Nginx安装目录 mkdir -p /data/logs/ wwwlog # 日志目录
1.0.6 编译nginx参数说明
#--with-http_dav_module #启用支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认关闭,需要编译开启
#--with-http_stub_status_module #启用支持(获取Nginx上次启动以来的工作状态)
#--with-http_addition_module #启用支持(作为一个输出过滤器,支持不完全缓冲,分部分相应请求)
#--with-http_sub_module #启用支持(允许一些其他文本替换Nginx相应中的一些文本)
#--with-http_flv_module #启用支持(提供支持flv视频文件支持)
#--with-http_mp4_module #启用支持(提供支持mp4视频文件支持,提供伪流媒体服务端支持)
#--with-pcre=/usr/local/src/pcre-8.37 #需要注意,这里指的是源码,用#./configure --help |grep pcre查看帮助;为了支持rewrite重写功能
#--with-http_stub_status_module #支持nginx状态查询
#--with-http_ssl_module #支持https
#--with-http_spdy_module #支持google的spdy,想了解请百度spdy,这个必须有ssl的支持
# 注:重新编译时,一定要查看以前的编译配置,只需在原有配置参数后添加新的参数即可
1.0.7 开始编译安装 nginx-1.12.0.tar.gz
官网下载网址 http://nginx.org
mkdir -p /usr/local/nginx mkdir /data/wwwroot/default mkdir -p /data/logs/wwwlogs/ chown www.www -R /data/wwwroot # 设置站点目录权限 chown www.www -R /usr/local/nginx # 设置nginx 安装目录权限 wget http://nginx.org/download/nginx-1.13.0.tar.gz wget http://zlib.net/zlib-1.2.11.tar.gz wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz wget https://www.openssl.org/source/openssl-1.1.0e.tar.gz tar zxvf nginx-1.13.0.tar.gz tar zxf pcre-8.40.tar.gz tar zxvf openssl-1.1.0e.tar.gz
1.0.8 关闭debug模式
cd nginx-1.13.0 sed -i 's@CFLAGS="$CFLAGS -g"@#CFLAGS="$CFLAGS -g"@' auto/cc/gcc
1.0.9 编译Nginx配置参数
./configure \ --prefix=/usr/local/nginx \ --user=www --group=www \ --with-http_stub_status_module \ --with-http_v2_module \ --with-http_ssl_module \ --with-http_gzip_static_module \ --with-http_realip_module \ --with-http_flv_module \ --with-openssl=../openssl-1.1.0e \ #这里要指向openssl-1.0.2k解压目录 --with-pcre=../pcre-8.40 \ #这里要指向pcre-8.40解压目录 --with-zlib=../zlib-1.2.11 \ --with-pcre-jit \ --with-ld-opt='-ljemalloc' # 内存优化
1.1.0 把Nginx 路径加入系统PATH环境变量
vim /etc/profile # 修改export PATH= 在最下面添加 export PATH=/usr/local/nginx/bin:$PATH # 如果有多个路径请用 英文冒号(;) 隔开 source /etc/profile # 使/etc/profile里的配置立即生效
source /etc/profile nginx -v nginx version: nginx/1.13.0
重启加载系统配置
1.1.1 建立开机启动配置文件
cd /lnmp /bin/cp Nginx-init-CentOS /etc/init.d/nginx # 复制启动文件 chkconfig --add nginx # 添加启动 chkconfig nginx on
1.1.2 修改启动文件安装路径
修改所有的 /usr/loca/nginx 改为你nginx 安装路径
sed -i "s@/usr/local/nginx@/usr/local/nginx@g" /etc/init.d/nginx sed -i "s@/etc/nginx/nginx.conf@/usr/local/nginx/conf/nginx.conf@g" /etc/init.d/nginx /bin/cp nginx.conf /usr/local/nginx/conf/nginx.conf # 复制配置文件
1.1.3 优化nginx.conf配置文件
cat > /etc/nginx/nginx.conf << EOF user www www; worker_processes auto; error_log /data/logs/wwwlogs/error_nginx.log crit; pid /var/run/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; multi_accept on; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 1024m; #缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存到本地再传给用户 client_body_buffer_size 10m; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; sendfile on; tcp_nopush on; keepalive_timeout 120; server_tokens off; tcp_nodelay on; 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; fastcgi_intercept_errors on; #Gzip Compression gzip on; gzip_buffers 16 8k; gzip_comp_level 6; gzip_http_version 1.1; gzip_min_length 256; gzip_proxied any; gzip_vary on; gzip_types text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml text/javascript application/javascript application/x-javascript text/x-json application/json application/x-web-app-manifest+json text/css text/plain text/x-component font/opentype application/x-font-ttf application/vnd.ms-fontobject image/x-icon; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; server { listen 80; server_name _; access_log /data/logs/wwwlogs/access_nginx.log combined; root /data/wwwroot/default; index index.html index.htm index.php; #error_page 404 /404.html; #error_page 502 /502.html; #网站定制化开启 HTTPS 的301重定向 #RewriteCond %{SERVER_PORT} !^443$ #RewriteCond %{HTTP_USER_AGENT} !MSIE/[1-8]\. [NC] #RewriteCond %{HTTP_HOST} www.fooher.com #RewriteRule ^.*$ https://www.fooher.com%{REQUEST_URI} [L,R=301] location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; deny all; } location ~ [^/]\.php(/|$) { #fastcgi_pass remote_php_ip:9000; fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ { expires 30d; access_log off; } location ~ .*\.(js|css)?$ { expires 7d; access_log off; } location ~ /\.ht { deny all; } } include vhost/*.conf; } EOF
1.1.4 代理配置
cat > /usr/local/nginx/conf/proxy.conf << EOF proxy_connect_timeout 300s; # nginx跟后端服务器连接超时时间(代理连接超时) proxy_send_timeout 900; proxy_read_timeout 900; # 连接成功后,后端服务器响应时间(代理接收超时) proxy_buffer_size 64k; # 设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffers 4 64k; # proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 proxy_busy_buffers_size 128k; # 高负荷下缓冲大小(proxy_buffers*2) proxy_redirect off; proxy_hide_header Vary; proxy_set_header Accept-Encoding ''; proxy_set_header Referer \$http_referer; proxy_set_header Cookie \$http_cookie; proxy_set_header Host \$host; proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传递请求,而不缓冲到磁盘 proxy_ignore_client_abort on; #不允许代理端主动关闭连接 EOF
1.1.5 设置 Nginx 日志自动分割
cat > /etc/logrotate.d/nginx << EOF /data/logs/wwwlogs/*nginx.log { daily rotate 5 missingok dateext compress notifempty sharedscripts postrotate [ -e /var/run/nginx.pid ] && kill -USR1 \`cat /var/run/nginx.pid\` endscript } EOF ldconfig # 更新动态库配置文件 service nginx start # 启动服务 netstat -tulnp |grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 43842/nginx
至此,Linux 下源码编译 Nginx 安装完成