theboyaply
theboyaply
发布于 2022-01-05 / 670 阅读
0
0

Linux安装nginx

安装基础工具包

NginxRedis一样,都是C语言开发的,都需要在Linux上使用C语言编译后才能使用,所以得先安装用于编译的C环境。
安装Redis时,只需要安装下面第一项,而Nginx则需要安装4项:

yum install -y gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

要是上面这4条语句不好用,可以参照下面的方法:

安装gcc-c++

yum install -y gcc automake autoconf libtool make
yum install -y gcc gcc-c++

安装PCRE库

http://www.pcre.org/查看最新安装包。

下载编译和安装:

wget https://github.com/PhilipHazel/pcre2/releases/download/pcre2-10.39/pcre2-10.39.tar.gz
tar -zxvf pcre2-10.39.tar.gz
cd pcre2-10.39
./configure
make
make install

安装zlib库

http://zlib.net/查看最新安装包。

下载编译和安装:

wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install

安装openssl

https://www.openssl.org/source/查看最新安装包。

wget https://www.openssl.org/source/openssl-3.0.0.tar.gz
tar -zxvf openssl-3.0.0.tar.gz

安装Nginx

https://nginx.org/download/查看最新安装包。

下载编译和安装,默认安装到目录/usr/local/nginx

wget https://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
./configure
make
make install

configure时可以指定参数,例如:

./configure --sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-file-aio \
--with-http_realip_module \
--with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.44 \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-openssl=/usr/local/src/openssl-1.1.1g

修改nginx配置

vim /usr/local/nginx/conf/nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    # 设置request请求大小
    client_max_body_size 20m;

    server {
        listen       80;
        server_name  localhost;
        charset utf-8;

	location / {
        root   /data/app/frontCode/dist;
	    try_files $uri $uri/ /index.html;
        index  index.html index.htm;
    }

	location /stage-api/ {
		proxy_set_header Host $http_host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header REMOTE-HOST $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # rewrite ^.+stage-api/?(.*)$ /$1 break;
		proxy_pass http://localhost:8080/;
	}

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

启停

启动:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
停止:/usr/local/nginx/sbin/nginx -s stop
重新载入配置文件:/usr/local/nginx/sbin/nginx -s reload

添加https模块

如果在安装时,没有添加https模块(即configure时没有加上--with-http_ssl_module参数),可以再次使用configure命令编译安装新的nginx启动程序。

进入configure目录,重新执行并添加--with-http_ssl_module参数,然后再执行make命令。运行完成后,会在nginx安装包目录下生成objs文件夹,里面有个新的nginx,使用这个替换当前运行的nginx,然后重新启动nginx就可以了。

# 查看nginx安装参数
> ./nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_ssl_module

– end –


评论