Nginx-1.12.2编译安装

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx-1.12.2编译安装相关的知识,希望对你有一定的参考价值。

nginx的概述

Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日。

Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行。其特点是占有内存少,并发能力强,事实上Nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用Nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

Nginx官方网站
Nginx官方文档

Nginx安装配置

环境规划

序号 主机名 IP地址 描述 系统版本
1 linux-node1 eth0:192.168.56.11 Web服务器 CentOS Linux release 7.2

系统优化

  • 关闭selinux和iptables
[[email protected] ~]# setenforce 0
[[email protected] ~]# getenforce
Disabled
[[email protected] ~]# sed -i ‘s#SELINUX=enforcing#SELINUX=disabled#g‘ /etc/sysconfig/selinux

[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# systemctl stop firewalld
  • 同步网络时间服务器
[[email protected] ~]# ntpdate 0.pool.ntp.org
10 Jan 12:38:28 ntpdate[2446]: adjust time server 120.25.115.20 offset 0.048309 sec
[[email protected] ~]# hwclock
Wed 10 Jan 2018 08:31:31 PM CST -0.944430 seconds
[[email protected] ~]# crontab -e
####Synchronization Network Time Server####
*/5 * * * * /usr/sbin/ntpdate 0.pool.ntp.org &>/dev/null
[[email protected] ~]# crontab -l
####Synchronization Network Time Server####
*/5 * * * * /usr/sbin/ntpdate 0.pool.ntp.org &>/dev/null
  • 安装相关依赖包
[[email protected] ~]# yum -y install gcc gcc-c++ zlib-devel gd-devel

安装依赖软件

  • 安装pcre支持正则表达式
[[email protected] ~]# tar xvfz pcre-8.39.tar.gz -C /usr/local/src/
[[email protected] ~]# cd /usr/local/src/pcre-8.39/
[[email protected] pcre-8.39]# ./configure --prefix=/usr/local/pcre-8.39
[[email protected] pcre-8.39]# make && make install
[[email protected] pcre-8.39]# ln -s /usr/local/pcre-8.39/ /usr/local/pcre
[[email protected] pcre-8.39]# ls -l /usr/local/pcre
lrwxrwxrwx 1 root root 21 Feb  9 16:57 /usr/local/pcre -> /usr/local/pcre-8.39/
  • 安装openssl支持加密访问
[[email protected] ~]# tar xvfz openssl-1.0.2a.tar.gz -C /usr/local/src/
[[email protected] ~]# cd /usr/local/src/openssl-1.0.2a/
[[email protected] openssl-1.0.2a]# ./config --prefix=/usr/local/openssl-1.0.2a
[[email protected] openssl-1.0.2a]# make && make install
[[email protected] openssl-1.0.2a]# ln -s /usr/local/openssl-1.0.2a/ /usr/local/openssl
[[email protected] openssl-1.0.2a]# ls -l /usr/local/openssl
lrwxrwxrwx 1 root root 26 Feb  9 17:01 /usr/local/openssl -> /usr/local/openssl-1.0.2a/
  • 安装zlib支持压缩
[[email protected] ~]# tar xvfz zlib-1.2.7.tar.gz -C /usr/local/src/
[[email protected] ~]# cd /usr/local/src/zlib-1.2.7/
[[email protected] zlib-1.2.7]# ./configure --prefix=/usr/local/zlib-1.2.7
[[email protected] zlib-1.2.7]# make && make install
[[email protected] zlib-1.2.7]# ln -s /usr/local/zlib-1.2.7/ /usr/local/zlib
[[email protected] zlib-1.2.7]# ls -l /usr/local/zlib
lrwxrwxrwx 1 root root 22 Feb  9 17:03 /usr/local/zlib -> /usr/local/zlib-1.2.7/
  • 安装geoip支持按地域访问
[[email protected] ~]# tar xvfz GeoIP-1.6.11.tar.gz -C /usr/local/src/
[[email protected] ~]# cd /usr/local/src/GeoIP-1.6.11/
[[email protected] GeoIP-1.6.11]# ./configure
[[email protected] GeoIP-1.6.11]# make && make install
[[email protected] GeoIP-1.6.11]# echo "/usr/local/lib" > /etc/ld.so.conf.d/geoip.conf
[[email protected] GeoIP-1.6.11]# tail -1 /etc/ld.so.conf.d/geoip.conf
/usr/local/lib
[[email protected] GeoIP-1.6.11]# ldconfig

安装Nginx

  • 创建nginx运行用户www
[[email protected] ~]# groupadd -g 60000 www
[[email protected] ~]# useradd -u 60000 -g www -c "Run The Nginx Service" -s /sbin/nologin -M www
[[email protected] ~]# id www
uid=60000(www) gid=60000(www) groups=60000(www)
[[email protected] ~]# grep "\bwww\b" /etc/passwd
www:x:60000:60000:Run The Nginx Service:/home/www:/sbin/nologin
  • 安装nginx
[[email protected] ~]# tar xvfz nginx-1.12.2.tar.gz -C /usr/local/src/
[[email protected] ~]# cd /usr/local/src/nginx-1.12.2/
[[email protected] nginx-1.12.2]# ./configure --prefix=/usr/local/nginx-1.12.2 --sbin-path=/usr/local/nginx-1.12.2/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --user=www --group=www --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_image_filter_module --with-http_geoip_module --with-pcre=/usr/local/src/pcre-8.39/ --with-zlib=/usr/local/src/zlib-1.2.7/ --with-openssl=/usr/local/src/openssl-1.0.2a/
[[email protected] nginx-1.12.2]# make && make install
[[email protected] nginx-1.12.2]# ln -s /usr/local/nginx-1.12.2/ /usr/local/nginx
[[email protected] nginx-1.12.2]# ls -l /usr/local/nginx
lrwxrwxrwx 1 root root 24 Feb  9 17:27 /usr/local/nginx -> /usr/local/nginx-1.12.2/
  • 配置文件支持语法检查
[[email protected] ~]# mkdir -p ~/.vim/syntax
[[email protected] ~]# ls -ld ~/.vim/syntax
drwxr-xr-x 2 root root 6 Feb  9 17:28 /root/.vim/syntax
[[email protected] ~]# mv nginx.vim ~/.vim/syntax
[[email protected] ~]# cat >> ~/.vim/filetype.vim<<EOF
au BufRead,BufNewFile /etc/nginx/nginx.conf,/usr/local/nginx/conf/vhost/* set ft=nginx
EOF

编写Nginx控制脚本

[[email protected] ~]# 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=/var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

编辑Nginx主配置文件

[[email protected]e1 ~]# vim /etc/nginx/nginx.conf
user  www www;
worker_processes  8;
worker_cpu_affinity  00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile  65535;
pid        /var/run/nginx.pid;

events {
    use epoll;
    worker_connections  65535;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    charset       utf-8;
    server_names_hash_bucket_size  128;
    server_names_hash_max_size  512;
    large_client_header_buffers  4 64k;
    client_max_body_size  64m;
    client_body_buffer_size  1024k;
    client_header_timeout  15;
    client_body_timeout  15;
    send_timeout  30;
    server_tokens  off;
    sendfile  on;
    tcp_nopush  on;
    tcp_nodelay  on;
    keepalive_timeout  60;

    log_format  json  ‘{"@timestamp":"$time_iso8601", ‘
                ‘"remote_addr": "$remote_addr", ‘
                ‘"remote_user": "$remote_user", ‘
                ‘"body_bytes_sent": "$body_bytes_sent", ‘
                ‘"bytes_sent": "$bytes_sent", ‘
                ‘"request_method": "$request_method", ‘
                ‘"request": "$request", ‘
                ‘"status": "$status", ‘
                ‘"request_time": "$request_time", ‘
                ‘"http_referrer": "$http_referer", ‘
                ‘"http_x_forwarded_for": "$http_x_forwarded_for", ‘
                ‘"http_user_agent": "$http_user_agent", ‘
                ‘"country_code": "$geoip_city_country_code", ‘
                ‘"country_code3": "$geoip_city_country_code3", ‘
                ‘"city_country_name": "$geoip_city_country_name", ‘
                ‘"region_name": "$geoip_region", ‘
                ‘"city_name": "$geoip_city", ‘
                ‘"city_continent_code": "$geoip_city_continent_code", ‘
                ‘"longitude": "$geoip_longitude", ‘
                ‘"latitude": "$geoip_latitude"} ‘;

    gzip  on;
    gzip_min_length  20k;
    gzip_buffers  8 32k;
    gzip_http_version  1.1;
    gzip_comp_level  6;
    gzip_types  text/plain application/x-javascript text/javascript application/javascript text/css application/x-httpd-php image/jpeg image/gif image/png application/xml text/xml application/rss+xml application/octet-stream application/x-rar-compressed;
    gzip_vary  on;
    gzip_disable  "MSIE [1-6]\.";

    geoip_country  /etc/nginx/conf.d/GeoIP.dat;
    fastcgi_param  GEOIP_COUNTRY_CODE $geoip_country_code;
    fastcgi_param  GEOIP_COUNTRY_CODE3 $geoip_country_code3;
    fastcgi_param  GEOIP_COUNTRY_NAME $geoip_country_name;

    geoip_city     /etc/nginx/conf.d/GeoLiteCity.dat;
    fastcgi_param  GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
    fastcgi_param  GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
    fastcgi_param  GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
    fastcgi_param  GEOIP_REGION $geoip_region;
    fastcgi_param  GEOIP_CITY $geoip_city;
    fastcgi_param  GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
    fastcgi_param  GEOIP_LONGITUDE $geoip_longitude;
    fastcgi_param  GEOIP_LATITUDE $geoip_latitude;

    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;

    open_file_cache  max=65535 inactive=20s;
    open_file_cache_min_uses  1;
    open_file_cache_valid  60s;

    server {
        listen  80 default_server;
        server_name  _;
        return  500;
    }

    include  /etc/nginx/conf.d/vhost/*.conf;
}
[[email protected] ~]# mkdir /etc/nginx/conf.d/vhost -p
[[email protected] ~]# ls -ld /etc/nginx/conf.d/vhost/
drwxr-xr-x 2 root root 6 Mar 16 14:53 /etc/nginx/conf.d/vhost/
[[email protected] ~]# gunzip GeoIP.dat.gz
[[email protected] ~]# gunzip GeoLiteCity.dat.gz
[[email protected] ~]# mv GeoIP.dat GeoLiteCity.dat /etc/nginx/conf.d/

启动Nginx并设置开机启动

[[email protected] ~]# systemctl start nginx
[[email protected] ~]# systemctl enable nginx
[[email protected] ~]# systemctl status nginx

以上是关于Nginx-1.12.2编译安装的主要内容,如果未能解决你的问题,请参考以下文章

RedHat 7 编译安装Nginx 1.12并配置WEB站点

[运维笔记] Nginx编译安装

Nginx编译安装和平滑升级

Fpm 之 Nginx rpm 包制作

Nginx的安装

[nginx]安全优化