141 lines
3.2 KiB
Markdown
141 lines
3.2 KiB
Markdown
# 编译安装OpenResty
|
||
|
||
> wandoubaba / 2023-07-21
|
||
|
||
## 系统环境
|
||
|
||
以Debian11为例
|
||
|
||
## 安装依赖
|
||
|
||
```sh
|
||
apt-get install wget libpcre3-dev libssl-dev perl make build-essential curl libgd-dev lsb-release
|
||
```
|
||
|
||
## 安装postgres模块依赖
|
||
|
||
我们这要安装驱动,而不是安装一个pg数据库服务,所以只需要安装一个`libpq-dev`就够了。
|
||
|
||
```sh
|
||
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
|
||
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
|
||
sudo apt-get update
|
||
sudo apt-get -y install libpq-dev
|
||
```
|
||
|
||
## 创建www用户和组
|
||
|
||
查看www用户是否存在
|
||
|
||
```sh
|
||
id www
|
||
```
|
||
|
||
创建www分组和www用户,并且不允许登录系统
|
||
|
||
```sh
|
||
groupadd www
|
||
useradd -g www -s /sbin/nologin www
|
||
```
|
||
|
||
再查看www用户
|
||
|
||
```sh
|
||
id www
|
||
```
|
||
|
||
## 下载源码包
|
||
|
||
```sh
|
||
wget https://openresty.org/download/openresty-1.21.4.2.tar.gz
|
||
```
|
||
|
||
## 解压&编译&安装
|
||
|
||
```sh
|
||
tar zxvf openresty-1.21.4.2.tar.gz
|
||
cd openresty-1.21.4.2
|
||
./configure \
|
||
--prefix=/www/server/openresty \
|
||
--with-pcre-jit \
|
||
--with-http_iconv_module \
|
||
--with-http_postgres_module \
|
||
--user=www --group=www \
|
||
--with-http_ssl_module \
|
||
--with-http_v2_module \
|
||
--with-http_realip_module \
|
||
--with-http_image_filter_module \
|
||
--with-http_addition_module \
|
||
--with-http_stub_status_module \
|
||
--with-stream \
|
||
--with-stream_ssl_module \
|
||
--with-stream_ssl_preread_module \
|
||
--with-http_gzip_static_module \
|
||
--with-http_gunzip_module \
|
||
--with-http_sub_module \
|
||
--with-http_flv_module \
|
||
--with-http_mp4_module \
|
||
--with-http_dav_module \
|
||
--with-openssl-opt=-g \
|
||
--with-pcre-opt=-g \
|
||
--with-pcre
|
||
gmake
|
||
gmake install
|
||
```
|
||
|
||
## 注册系统服务和开机自启
|
||
|
||
在`/etc/systemd/system/`目录下创建`nginx.service`文件,内容如下:
|
||
|
||
```ini
|
||
[Unit]
|
||
Description=Nginx HTTP Server
|
||
After=network.target
|
||
|
||
[Service]
|
||
Type=forking
|
||
PIDFile=/www/server/openresty/nginx/logs/nginx.pid
|
||
ExecStartPre=/www/server/openresty/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
|
||
ExecStart=/www/server/openresty/nginx/sbin/nginx -g 'daemon on; master_process on;'
|
||
ExecReload=/www/server/openresty/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
|
||
ExecStop=/www/server/openresty/nginx/sbin/nginx -g 'daemon on; master_process on;' -s quit
|
||
PrivateTmp=true
|
||
Restart=always
|
||
|
||
[Install]
|
||
WantedBy=multi-user.target
|
||
```
|
||
|
||
保存文件并退出后,执行下面命令:
|
||
|
||
```sh
|
||
systemctl daemon-reload
|
||
systemctl enable nginx
|
||
```
|
||
|
||
现在已完成系统服务注册和开机自启,下面是服务控制命令:
|
||
|
||
```sh
|
||
systemctl start nginx # 启动nginx服务
|
||
systemctl stop nginx # 停止nginx服务
|
||
systemctl restart nginx # 重启nginx服务
|
||
systemctl reload nginx # 重新加载nginx配置文件
|
||
systemctl status nginx # 检查nginx服务状态
|
||
```
|
||
|
||
## 测试
|
||
|
||
正确启动服务后,使用`ps`命令可以查看nginx服务进程
|
||
|
||
```sh
|
||
ps aux | grep nginx
|
||
```
|
||
|
||
使用`ss`命令可以查看端口监听情况
|
||
|
||
```sh
|
||
ss -tnlp | grep 80
|
||
```
|
||
|
||
在浏览器中访问`http://ip`应该可以看到OpenResty的默认欢迎页`Welcom to OpenResty`。
|