一、yum安装
1.yum install nginx -y
当前默认版本为1.20.1
2.nginx -V
查看当前nginx配置模块
3.操作命令
systemctl start nginx.service
-------------- 启动
systemctl stop nginx.service
-------------- 停止
systemctl restart nginx.service
-------------- 重启
systemctl reload nginx.service
-------------- 重新加载配置
systemctl enable nginx.service
-------------- 设置开机自启动
4.配置stream模块
1.下载一个同版本可编译的nginx
cd /opt wget http://nginx.org/download/nginx-1.20.1.tar.gz
tar -zxvf nginx-1.20.1.tar.gz && cd nginx-1.20.1
2.备份原nginx文件
mv /usr/sbin/nginx /usr/sbin/nginx.bak
cp -r /etc/nginx /etc/nginx.bak
3.安装依赖
yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmakepcre-develnanowget gcc gcc-c++ ncurses-devel per redhat-rpm-config.noarch
4.重新编译nginx文件
在第2步查到的模块配置后追加
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --with-stream
5.如编译时缺少依赖可执行以下命令
yum -y install libxml2 libxml2-dev libxslt-devel
yum -y install gd-devel
yum -y installperl-devel perl-ExtUtils-Embed
yum -y install GeoIP GeoIP-devel GeoIP-data
yum -y install pcre-devel
yum -y install openssl openssl-devel
6.如遇到一下错误可以按下面方式解决
错误1:./configure: error: the invalid value in --with-ld-opt="-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E"
解决方法:yum -y install redhat-rpm-config.noarch
错误2:./configure: error: the HTTP image filter module requires the GD library.
解决方法: yum -y install gd-devel
错误3:./configure: error: perl module ExtUtils::Embed is required
解决方法:yum -y install perl-devel perl-ExtUtils-Embed
7.编译通过,继续验证
注:编译完成make一下就可以了,不要make install
/opt/software/nginx-1.20.1/objs/nginx -t
/opt/software/nginx-1.20.1/objs/nginx -V
8.替换nginx文件并重启
cp /opt/software/nginx-1.20.1/objs/nginx /usr/sbin/
systemctl start nginx
9.添加stream配置,并验证(conf文件:/etc/nginx/nginx.conf)
stream {
upstream back{
server 192.168.208.1:3000;
}
server {
listen 2000 udp;
proxy_connect_timeout 5s;
proxy_timeout 300s;
proxy_pass back;
}
}
二、编译安装
1、下载可编译的nginx,目前稳定版本 1.20.1
cd /opt wget http://nginx.org/download/nginx-1.20.1.tar.gz
tar -zxvf nginx-1.20.1.tar.gz && cd nginx-1.20.1
2、编译nginx,并配置stream模块
./configure --with-stream
3、安装nginx
make && make install
4、校验
安装完成后,会在/usr/local目录下生成nginx文件夹
5、命令
/usr/local/nginx/sbin/nginx
----启动nginx
/usr/local/nginx/sbin/nginx -s reload
—重新加载配置文件
6、添加stream配置,conf文件位置(/usr/local/nginx/nginx.conf)
stream {
upstream back{
server 192.168.208.1:3000;
}
server {
listen 2000 udp;
proxy_connect_timeout 5s;
proxy_timeout 300s;
proxy_pass back;
}
}