一、安装相关构建工具
1、ubuntu
apt-get install build-essential
2、centos
yum install -y gcc gcc-c++
创建用户
useradd www-data -M -s /usr/sbin/nologin
二、下载nginx及支持库
1)nginx
下载页面:
http://nginx.org/en/download.html
下载代码:
wget http://nginx.org/download/nginx-1.18.0.tar.gz
2)pcre
下载页面:
https://ftp.pcre.org/pub/pcre/
下载代码:
wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
3)zlib
下载页面:
http://www.zlib.net/
下载代码:
wget http://www.zlib.net/zlib-1.2.11.tar.gz
4)openssl
下载页面:
https://www.openssl.org/source/
下载代码:
wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
5)echo-nginx-module(建议安装)
下载页面:
https://github.com/openresty/echo-nginx-module/releases
下载代码:
wget https://github.com/openresty/echo-nginx-module/archive/v0.62.tar.gz -O echo-nginx-module-0.62.tar.gz
三、编译及安装nginx
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/sbin/nginx --pid-path=/run/nginx.pid --user=www-data --group=www-data --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gzip_static_module --with-http_sub_module --with-openssl=../openssl-1.1.1g --with-zlib=../zlib-1.2.11 --with-pcre=../pcre-8.44 --add-module=../echo-nginx-module-0.62
make -j2 make install
四、配置文件
备份配置文件
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
修改配置文件
vim /usr/local/nginx/conf/nginx.conf
根据服务器配置调整:
user www-data;
worker_processes 1;
worker_cpu_affinity 1;
error_log /mnt/logs/nginx/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /run/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
types_hash_max_size 2048;
server_tokens off;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 200m;
##
# Fastcgi Settings
##
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;
##
# Proxy Settings
##
#proxy_connect_timeout 300;
#proxy_send_timeout 300;
#proxy_read_timeout 300;
#proxy_buffering on;
#proxy_buffer_size 32k;
#proxy_buffers 4 128k;
#proxy_busy_buffers_size 256k;
#proxy_max_temp_file_size 256k;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_min_length 1000;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.0;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# SSL Settings
##
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 60m;
##
# Virtual Host Configs
##
include /usr/local/nginx/conf/conf.d/*.conf;
include /usr/local/nginx/conf/sites-enabled/*;
} log_format main escape=json '{'
'"timestamp": "$time_iso8601", '
'"remote_addr": "$remote_addr", '
'"remote_user": "$remote_user", '
'"request_method": "$request_method", '
'"content_type": "$content_type", '
'"request_uri": "$request_uri", '
'"request_protocol": "$server_protocol", '
'"request_length": $request_length, '
'"request_time": $request_time, '
'"request_body": "$request_body", '
'"response_status": $status, '
'"body_bytes_sent": $body_bytes_sent, '
'"bytes_sent": $bytes_sent, '
'"http_referer": "$http_referer", '
'"http_user_agent": "$http_user_agent", '
'"http_x_forwarded_for": "$http_x_forwarded_for", '
'"http_host": "$http_host", '
'"server_name": "$server_name", '
'"upstream_response_time": $upstream_response_time, '
'"upstream_addr": "$upstream_addr", '
'"upstream_status": $upstream_status'
'}';配置CPU亲和性
# 4 CPU(4 Core) + 4 worker_processes(每个worker_processes 使用1个CPU) worker_processes 4; worker_cpu_affinity 0001 0010 0100 1000; # 8 CPU(8 Core) + 8 worker_processes(每个worker_processes 使用1个CPU) worker_processes 8; worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; # 16 CPU(16 Core) + 16 worker_processes(每个worker_processes 使用1个CPU) worker_processes 16; worker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000; # 2 CPU(2 Core) + 8 worker_processes(每个worker_processes 使用1个CPU) worker_processes 8; worker_cpu_affinity 01 10 01 10 01 10 01 10; # 8 CPU(8 Core) +2 worker_processes(每个worker_processes 使用1个CPU) worker_processes 2; worker_cpu_affinity 10101010 01010101;
获取前端真实IP
set_real_ip_from xxx.xxx.0.0/16; set_real_ip_from xxx.xxx.0.0/16; real_ip_header X-Forwarded-For;
创建相关配置目录
mkdir conf.d sites-available sites-enabled cert
创建网站配置文件
vim /usr/local/nginx/conf/sites-available/default
server {
listen 80 default;
server_name localhost;
charset utf-8;
access_log /mnt/logs/nginx/default.access.log;
error_log /mnt/logs/nginx/default.error.log;
root /mnt/wwwroot/default/;
location / {
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
#关闭favicon.ico不存在时记录日志
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# 不允许访问隐藏文件例如 .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}启用网站配置
ln -s /usr/local/nginx/conf/sites-available/default /usr/local/nginx/conf/sites-enabled/default
测试配置文件
nginx -t
五、重启nginx服务
service nginx restart
六、注释
1、You need Perl 5 错误解决方法
https://www.cpan.org/src/
wget https://www.cpan.org/src/5.0/perl-5.34.0.tar.gz tar -xzf perl-5.34.0.tar.gz cd perl-5.34.0 ./Configure -des -Dprefix=$HOME/localperl make make test make install