- Nginx 安装
- 检查GCC、G++安装情况
(1)检查gcc是否安装
gcc -v
(2)检查g++ 是否安装
g++ --version
-
- 先到有网的机器上下载依赖包
sudo yum install --downloadonly --downloaddir=/usr/download/gcc gcc
sudo yum install --downloadonly --downloaddir=/usr/download/g++ gcc-c++
sudo yum install --downloadonly --downloaddir=/usr/download/telnet-server telnet-server
sudo yum install --downloadonly --downloaddir=/usr/download/xinetd xinetd
sudo yum install --downloadonly --downloaddir=/usr/download/telnet telnet
-
- 把离线依赖包拷贝到无网机器上,分别切换到指定目录进行安装, 安装指令为
rpm -ivh *.rpm
-
- 其他依赖包
将nginx安装包*.tar.gz,*.rpm上传到/usr/local/src目录下
pcre,用于url rewrite
zlib,用于gzip压缩
openssl,用于后续可能升级到https时使用
(1)pcre查看是否安装,如果已安装则略过
在linux命令行下输入shell指令:rpm -qa pcre
如果没有安装,则安装pcre,执行下列命令
tar -zxvf pcre-8.37.tar.gz
cd pcre-8.37/
./configure
make
make install
cd ..
- 查看是否安装,如果已安装则略过
在linux命令行下输入shell指令:rpm -qa pcre-devel
如果没有安装,则安装pcre-devel,执行下列命令
rpm -ivh pcre-devel-8.32-17.el7.x86_64.rpm
- 是否安装,如果已安装则略过
在linux命令行下输入shell指令:yum list |grep zlib*
如果没有安装,则安装zlib,执行下列命令
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8/
./configure
make && make install
cd ..
- openssl是否安装,
在linux命令行下输入shell指令:openssl version -a
执行下列命令
tar -zxvf openssl-1.1.1d.tar.gz
cd openssl-1.1.1d/
./config --prefix=/usr/local shared zlib
make
make install
cd ..
如果make install没有权限,就用下面命令(应该是/usr/local文件夹需要管理员权限才能安装)
sudo make install
查看:openssl version -a
如果版本还是旧版(1.0.2k),找到当前的启动openssl(/usr/bin/openssl)和安装的openssl(/usr/local/bin/openssl),进行替换(注意备份,网上做的软链接,但后续安装nginx会有很多问题)
mkdir /usr/local/src/bak
cd /usr/local/src/bak
cp /usr/bin/openssl openssl.1.0.2k
cp /usr/local/bin/openssl openssnew
cp -r /usr/local/bin/openssl /usr/bin
如果你安装的路径不是/usr/local,则用命令查找:find / -name openssl
如果出现如下错误:
openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory
cp /usr/local/lib64/libssl.so.1.1 /usr/lib64/
cp /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/
cd ..
libssl.so.1.1和libcrypto.so.1.1都有这个问题
这是由于openssl库的位置不正确造成的,将/usr/local/lib64/下的对应文件复制到/usr/lib64/下;
再次查看:openssl version -a
OpenSSL 1.1.1d 10 Sep 2019
built on: Tue Mar 3 09:16:36 2020 UTC
platform: linux-x86_64
options: bn(64,64) rc4(16x,int) des(int) idea(int) blowfish(ptr)
compiler: gcc -fPIC -pthread -m64 -Wa,--noexecstack -Wall -O3 -DOPENSSL_USE_NODELETE -DL_ENDIAN -DOPENSSL_PIC -DOPENSSL_CPUID_OBJ -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DKECCAK1600_ASM -DRC4_ASM -DMD5_ASM -DVPAES_ASM -DGHASH_ASM -DECP_NISTZ256_ASM -DX25519_ASM -DPOLY1305_ASM -DZLIB -DNDEBUG
OPENSSLDIR: "/usr/local/ssl"
ENGINESDIR: "/usr/local/lib64/engines-1.1"
Seeding source: os-specific
-
- nginx编译安装
- 上传nginx-1.16.1.tar.gz 到/usr/local/src
- 解压并编译安装
注:./configure 只有最后一个生效
tar -zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1/
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre=/opt/pcre-8.37 --with-zlib=/opt/zlib-1.2.8 --with-openssl=../openssl-1.1.1d
./configure --with-pcre=/opt/pcre-8.37 --with-zlib=/opt/zlib-1.2.8
./configure --with-http_stub_status_module --with-pcre --with-http_ssl_module --with-openssl=../openssl-1.1.1d
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-pcre --with-http_ssl_module --with-openssl=../openssl-1.1.1d
make
make install #注意,如果已有nginx,不需要执行这条指令
-
- 启动
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx -s reload
-
- 设置开机启动
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
-
- 优化配置
(1)最大打开文件数的限制
vi /etc/security/limits.conf
最后添加
# End of file
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
(2)用户进程限制
vi /etc/security/limits.d/20-nproc.conf
#加大普通用户限制 也可以改为unlimited
* soft nproc 40960
root soft nproc unlimited
(3)内核参数
vi /etc/sysctl.conf
net.ipv4.conf.default.rp_filter = 1
net.ipv4.ip_forward = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_max_tw_buckets = 262144
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 200000
net.core.rmem_default = 67108864
net.core.wmem_default = 67108864
net.core.rmem_max = 67108864
net.core.wmem_max = 67108864
net.ipv4.tcp_rmem = 4096 87380 6291456
net.ipv4.tcp_wmem = 4096 65536 4194304
net.ipv4.tcp_mem = 3097431 4129911 6194862
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.ip_forward = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_keepalive_time = 120
vm.overcommit_memory = 1
fs.file-max = 1048576
保存后运行 sysctl -p后配置生效
sysctl -p
部分参数说明
net.ipv4.tcp_syncookies = 1 表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_fin_timeout = 720 表示如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间。