产品服务AMH 免费服务器主机面板SSL证书 免费SSL证书申请 担保单 免费安全、零费率交易平台编程助手免费智能写代码、翻译AMYSQL 免费MySQL管理工具

AMH 社区首页

 AMH社区 - 开放自由有价值的社区

[求助帖] Nginx模块编写教程

894849635
金牌会员
15129.94 价值分

894849635 发表于 2013-11-16 18:24:58
本帖最后由 894849635 于 2013-11-16 18:48 编辑

好吧,我要离开了。为了能够二本逆袭一本 ,为明年高考胜利添加几分,我要断网了。博客也会停板7个月左右。临走之前,把我写Nginx模块的经验发出来,希望可以帮助大家。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
对于amh_module_info();amh_module_admin();amh_module_status)这三个函数不再讲解,如有
不理解的,可以查看我的另一篇教程。
##AMH模块教程-插件型模块的编写##
~~~~~~~~~~~~~~~~~~~~~正文开始~~~~~~~~~~~~~~~~~~
一、FAQ
1.为什么Nginx模块要单独列出来?
答:Nginx模块与之前的插件型模块不同,PHP本身有phpize来加载extension,Apache也具有类似动态加载模块的功能,而Nginx不具备这样的功能,所以在安装模块和卸载模块会麻烦一些。
2.Nginx模块主要注意有哪些?
答:Nginx模块的安装是需要重新编译,平滑升级的,所以,你需要查看Nginx编译时的参数,并且要导出该参数来在一会安装模块时才不会导致之前安装的模块失效.而且,因为AMH版本不同时,Nginx的版本略有不同,所以,在安装模块前要先将Nginx升级到我们需要的版本。
3.Nginx模块如何使用?
答:Nginx模块大多与conf文件有关,所以,安装好模块后需要手动在/usr/local/nginx/conf/vhost/下的相应配置文件中修改,添加相应字段。
二、代码讲解

Nginx模块的编写需要定义新的function,一般为upgrade
#upgrade
function upgrade()
{ # 获取Nginx安装参数,导出到fancyIndex模块下,生成nginx_configure和nginx_configure.bak文件。
#nginx_configure文件存放的原始参数,含Nginx版本号等信息,安装完会删除。
#nginx_configure.bak文件存放的是以 “--prefix=xxx”形式的正式参数,卸载时会用到。
nginx_configure='';
#判断Nginx文件是否存在&&导出配置信息&&grep获取编译命令行并导出bak文件&&删除升级产生的Nginx_configure文件
以上就是Nginx模块中的升级代码,有了升级代码,可以使模块可以适配多个AMH版本。

安装代码与升级代码大同小异,只是在编译的时候添加了相应的模块信息
#install
function amh_module_install()
{
if amh_module_status ; then
exit;
else
#调用升级代码
upgrade;
#获取CPU线程数
Cpunum=`cat /proc/cpuinfo |grep 'processor'|wc -l`;
#获取Nginx安装盘命令
nginx_configure=`/usr/local/nginx/sbin/nginx -V 2> /root/amh/modules/ngx-fancyindex/nginx_configure && cat /root/amh/modules/ngx-fancyindex/nginx_configure | grep 'configure arguments' | cut -d: -f2 && rm -f /root/amh/modules/ngx-fancyindex/nginx_configure`;
new_nginx_configure=$nginx_configure;
if ! echo "$new_nginx_configure" | grep 'add-module=/usr/local/ngx-fancyindex' > /dev/null; then
#添加模块信息
new_nginx_configure="${new_nginx_configure} --add-module=/usr/local/ngx-fancyindex"
fi;
#如果编译命令不同(模块信息添加成功),开始编译
if [ "$nginx_configure" != "$new_nginx_configure" ]; then
cd /usr/local/;
wget http://mirror.ixiqin.com/zip/ngx-fancyindex-master.zip;
unzip ngx-fancyindex-master.zip;
mv ngx-fancyindex-master ngx-fancyindex;
wget http://code.amysql.com/files/nginx-1.2.9.tar.gz;
tar -zxf nginx-1.2.9.tar.gz;

cd nginx-1.2.9;
./configure $new_nginx_configure;
make -j $Cpunum;
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-old;
#平滑升级
\cp -a ./objs/nginx /usr/local/nginx/sbin/;
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`;

cd /usr/local/;
rm -rf nginx-1.2.9.tar.gz nginx-1.2.9 /usr/local/nginx/sbin/nginx-old ngx-fancyindex-master.zip ;
fi;

touch /root/amh/modules/ngx-fancyindex/InstallComplete;
sleep 3;
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`;

amh nginx reload;
amh_module_status;
fi;
接下来是卸载代码
#uninstall
function amh_module_uninstall()
{
if amh_module_status ; then

#获取CPU线程数
Cpunum=`cat /proc/cpuinfo |grep 'processor'|wc -l`;
nginx_configure=`/usr/local/nginx/sbin/nginx -V 2> /root/amh/modules/ngx-fancyindex/nginx_configure && cat /root/amh/modules/ngx-fancyindex/nginx_configure | grep 'configure arguments' | cut -d: -f2 && rm -f /root/amh/modules/ngx-fancyindex/nginx_configure`;
new_nginx_configure=$nginx_configure;
#处理安装命令行
if echo "$new_nginx_configure" | grep 'add-module=/usr/local/ngx-fancyindex' > /dev/null; then
new_nginx_configure=`echo $new_nginx_configure | sed "s|--add-module=/usr/local/ngx-fancyindex||"`;
fi;

if [ "$nginx_configure" != "$new_nginx_configure" ]; then
cd /usr/local/;
wget http://code.amysql.com/files/nginx-1.2.9.tar.gz;
tar -zxf nginx-1.2.9.tar.gz;
cd nginx-1.2.9;
./configure $new_nginx_configure;
make -j $Cpunum;
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-old;
\cp -a ./objs/nginx /usr/local/nginx/sbin/;
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`;
cd /usr/local/;
rm -rf nginx-1.2.9 nginx-1.2.9.tar.gz* /usr/local/nginx/sbin/nginx-old;
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`;
fi;
#删除安装文件夹
cd /usr/local/;
rm -rf ngx-fancyindex;
rm -f /root/amh/modules/ngx-fancyindex/InstallComplete;
amh nginx reload;
echo '[OK] ngx-fancyindex Uninstall successful.';
else
exit;
fi;
}

三、其他
前些日子在群里看到了不一样的模块安装代码,标记一下。
#status
function amh_module_status()
{
if grep -q '\[calendar\.so\]' /etc/php.ini; then
echo '[OK] Calendar is already installed.';
return 0;
else
echo '[Notice] Calendar is not installed.';
return 1;
fi;
}

code by 如梦令/快乐飞扬博客
评价: 这个问题有价值吗?  没有 (0分)   有, 感谢 (1分)
2013-11-16 18:24:58 1

894849635
金牌会员
15129.94 价值分

坛友们,我走了。七月之后,再会!
希望到时候AMH模块能突破80个!
评价: 这个答复有价值吗?  没有 (0分)   有, 感谢 (0分)
回复  2013-11-16 18:26:01 2

ali
银牌会员
3123.65 价值分

快乐飞扬博客?换博客了?
评价: 这个答复有价值吗?  没有 (0分)   有, 感谢 (0分)
回复  2013-11-16 18:38:39 3

894849635
金牌会员
15129.94 价值分

引用:
ali 发表于 2013-11-16 18:38
快乐飞扬博客?换博客了?


群友:如梦令的博客。代码就是他的。
评价: 这个答复有价值吗?  没有 (0分)   有, 感谢 (0分)
回复  2013-11-16 18:49:11 4

ali
银牌会员
3123.65 价值分

引用:
894849635 发表于 2013-11-16 18:49
群友:如梦令的博客。代码就是他的。


知道鸟。。
评价: 这个答复有价值吗?  没有 (0分)   有, 感谢 (0分)
回复  2013-11-17 12:59:46 5

zmyamh
金牌会员
6028.52 价值分

支持一下!
加油吧,少年!
评价: 这个答复有价值吗?  没有 (0分)   有, 感谢 (0分)
回复  2013-11-17 13:22:29 6

amysql
创始人
99532.51 价值分

引用:
894849635 发表于 2013-11-16 18:26
坛友们,我走了。七月之后,再会!
希望到时候AMH模块能突破80个!


加油,愿高考顺利。
大学就有时间了。
评价: 这个答复有价值吗?  没有 (0分)   有, 感谢 (0分)
回复  2013-11-17 18:07:48 7

AMH面板 - 好用高效低占用、安全可靠极稳定

logan
铝牌会员
329.00 价值分

引用:
894849635 发表于 2013-11-16 18:26
坛友们,我走了。七月之后,再会!
希望到时候AMH模块能突破80个!


期待你的归来
评价: 这个答复有价值吗?  没有 (0分)   有, 感谢 (0分)
回复  2013-11-17 18:38:43 8
 1  (总1页)
AMH社区列表
用户服务中心