LNMP下使用Openresty替换Nginx

前言

LNMP、宝塔可能是大部分人日常用的一种便捷方式。然而有一些东西,现有的环境似乎不太能满足,因此想换点新的环境组合方式。当然,更换完后本质上还是LNMP。

简略说明

系统:Debian 10
环境:LNMP1.9 (lnmp.org)

安装 LNMP

首先安装screen,apt -y install screen 防止安装过程连接中断。

然后创建一个窗口,如screen -S lnmp

再来选择一个lnmp安装脚本,比如这里选择的是1.9版本:

#下载版:推荐海外VPS或空间较小用户使用
wget http://soft.vpser.net/lnmp/lnmp1.9.tar.gz -cO lnmp1.9.tar.gz && tar zxf lnmp1.9.tar.gz && cd lnmp1.9 && ./install.sh lnmp
#完整版:推荐国内VPS用户使用
wget http://soft.vpser.net/lnmp/lnmp1.9-full.tar.gz -cO lnmp1.9-full.tar.gz && tar zxf lnmp1.9-full.tar.gz && cd lnmp1.9-full && ./install.sh lnmp
 
#后续步骤参见https://lnmp.org/install.html
等待部署完成, 你就可以使用 lnmp 命令了

安装 Openresty

Openresty+Redis的缓存组合能绕过php,再通过Nginx SRcache拓展直达Redis。

#访问官网,页面下拉,选择对应的安装包
https://openresty.org/cn/download.html
#这里选择当前最新的1.21.4.1版本来演示(需要其它版本的请移步官网,替换对应的版本号)
wget https://openresty.org/download/openresty-1.21.4.1.tar.gz
#解压
tar -zxvf openresty-1.21.4.1.tar.gz
#进入目录
cd openresty-1.21.4.1
如果你的站点不打算使用CDN,那么可以使用下面的构建编译语句。
#开始手动编译,并添加两个模块
./configure --with-http_stub_status_module --with-http_v2_module
#安装
make && make install
如果你决定使用CloudFlare的CDN,不妨再编译个模块:http_realip_module,这样能获取到访客的真实ip,而不是CloudFlare的ip地址。
#构建
./configure --with-http_stub_status_module --with-http_v2_module --with-http_realip_module
#编译安装
make && make install

关于报错

上面的 ./configure 构建过程若有错误,那肯定是没有按步骤来先安装lnmp。此时可以直接使用 apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev  安装一下,然后再重新执行相关构建命令。

查看模块

检查必须的模块是否安装正确,后续主要用到的4个模块:srcache-nginx-module 、redis2-nginx-module 、redis-nginx-module 、set-misc-nginx-module ,以及lnmp脚本命令需要用到的2个模块:http_stub_status_module 和http_v2_module 。查看指定模块命令:

#能输出以上6个模块即可。
/usr/local/openresty/nginx/sbin/nginx -V 2>&1 | grep 'srcache-nginx-module\|redis2-nginx-module\|redis-nginx-module\|set-misc-nginx-module\|http_stub_status_module\|http_v2_module' -o

替换Nginx为Openresty

nano /etc/init.d/nginx
#将原先的“/usr/local/nginx”替换为openresty的实际路径“/usr/local/openresty/nginx”
#改好就是:
NGINX_BIN='/usr/local/openresty/nginx/sbin/nginx'
CONFIG='/usr/local/openresty/nginx/conf/nginx.conf'

完成了之后,接下来还需要修改其它相关的文件。反正最终的效果就是要替换完nginx,还能照常使用lnmp的全部指令~

后续的4个操作(重要!)

本内容需要密码才能查看

测试 openresty

#重启openresty的nginx
#方式一
lnmp nginx restart
#方式二
systemctl restart nginx
#方式三
/etc/init.d/nginx restart

可能用到的命令

本内容需要登录后查看
阅读剩余
THE END