ubuntu18-openresty-二进制安装

Source

1、安装依赖

sudo apt update
sudo apt install -y libgd3
sudo apt install -y libgd-dev
sudo apt install -y build-essential libpcre3 libpcre3-dev libssl-dev perl openssl ruby zlib1g zlib1g-dev libxslt1-dev libxml2-dev libbrotli1 libbrotli-dev libncurses5-dev libgeoip-dev

2、创建用户及用户组

groupadd www
useradd -g www -s /sbin/nologin -M www

3、创建目录

mkdir -p /data/{nginx,wwwroot,file}
mkdir -p /data/wwwroot/error

4、上传文件

openresty-1.27.1.1.tar.gz #上传到/data/file目录下面,同样也可以在/data/file目录下面在线下载 wget https://openresty.org/download/openresty-1.27.1.1.tar.gz
index.html #上传到/data/wwwroot目录下面
401.html、403.html、404.html、50x.html #上传到/data/wwwroot/error

5、解压并进入目录编译安装

tar -zxf openresty-1.27.1.1.tar.gz #解压文件
cd openresty-1.27.1.1/ #进入目录
./configure --prefix==/usr/local/openresty --with-luajit #不带参数编译
./configure --prefix=/usr/local/openresty --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module=dynamic --with-stream_ssl_preread_module --with-compat --with-pcre-jit #带参数编译
make && make install #编译安装

6、添加环境变量

vi /etc/profile
export OPENRESTY_PATH=/usr/local/openresty
export LUAJIT_PATH=/usr/local/openresty/luajit
export NGINX_PATH=/usr/local/openresty/nginx
export PATH=$PATH:$LUAJIT_PATH/bin:$OPENRESTY_PATH/bin:$NGINX_PATH/sbin
source /etc/profile

7、修改nginx配置文件

vi /usr/local/openresty/nginx/conf/nginx.conf
user www;
worker_processes 4;
worker_cpu_affinity auto;
error_log /data/nginx/error.log;
worker_rlimit_nofile 65535;
pid  /data/nginx/nginx.pid;
events {
        worker_connections  10240;
        use epoll;
        multi_accept on;
}
http {
        include       mime.types;
        default_type  application/octet-stream;
        map $time_iso8601 $logdate {
                '~^(?<ymd>\\d{4}-\\d{2}-\\d{2})' $ymd;
                default                       'date-not-found';
        }
        map $time_iso8601 $request_times {
                '~^(?<ymdhms>\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2})' $ymdhms;
                default                       'date-not-found';
        }
        log_format access-upstream '{"host":"$server_addr",'
                '"clientip":"$remote_addr",'
                '"size":$body_bytes_sent,'
                '"responsetime":$request_times,'
                '"http_host":"$host",'
                '"url":"$uri",'
                '"agent":"$http_user_agent",'
                '"status":"$status"}';
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        server_names_hash_bucket_size 128;
        server_names_hash_max_size 512;
        keepalive_timeout  1000s;
        client_header_timeout 15s;
        client_body_timeout 15s;
        send_timeout 1000s;
        proxy_cache_path /data/nginx levels=1:2 keys_zone=nginx-cache:20m max_size=50g inactive=168h;
        client_body_buffer_size 512k;
        client_header_buffer_size 256k;
        client_max_body_size 1024m;
        large_client_header_buffers 2 8k;
        proxy_connect_timeout 1000s;
        proxy_send_timeout 1000s;
        proxy_read_timeout 1000s;
        proxy_buffer_size 128k;
        proxy_buffers 8 256k;
        proxy_busy_buffers_size 256k;
        proxy_temp_file_write_size 256k;
        proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;
        fastcgi_intercept_errors on;
        fastcgi_ignore_client_abort on;
        fastcgi_connect_timeout 1000s;
        fastcgi_send_timeout 1000s;
        fastcgi_read_timeout 1000s;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 8 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        gzip on;
        gzip_min_length 1k;
        gzip_buffers 4 32k;
        gzip_http_version 1.1;
        gzip_comp_level 6;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss                                   text/javascript;
        gzip_vary on;
        gzip_disable "MSIE [1-6].";
        include vhost/*.conf;
}

8、创建nginx默认配置

mkdir -p /usr/local/openresty/nginx/conf/vhost
vi /usr/local/openresty/nginx/conf/vhost/localhost_80.conf
server {
        listen        80 default_server;
        server_name _;
        error_log /data/nginx/localhost_error.log crit;
        access_log /data/nginx/localhost_acess_$logdate.log access-upstream;
        error_page   401 /401.html;
        error_page   403 /403.html;
        error_page   404 /404.html;
        error_page      500 502 503 504 /50x.html;
        location / {
                return 401;
        }
        location = /401.html {
                root "/data/wwwroot/error";
        }
        location = /403.html {
                root "/data/wwwroot/error";
        }
        location = /404.html {
                root "/data/wwwroot/error";
        }
        location = /50x.html {
                root "/data/wwwroot/error";
        }
}

9、开机启动配置

vi /etc/systemd/system/openresty.service
[Unit]
Description=openresty service
After=network.target
[Service]
Type=forking
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

10、加入/移除开机启动

systemctl enable openresty.service #加入
systemctl disable openresty.service #移除

11、授权访问

chown -R www:www /data/wwwroot
chown -R www:www /data/nginx
chown -R www:www /usr/local/openresty

12、开放80端口

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

13、常用命令

systemctl start openresty.service #启动
systemctl stop openresty.service #停止
systemctl restart openresty.service #重启
ps -ef | grep openresty #验证是否启动启动成功