{"id":41756,"date":"2024-10-02T07:22:01","date_gmt":"2024-10-01T23:22:01","guid":{"rendered":"https:\/\/server.hk\/cnblog\/41756\/"},"modified":"2024-10-02T07:22:02","modified_gmt":"2024-10-01T23:22:02","slug":"apache%e6%96%b0%e6%89%8b%e6%95%99%e7%a8%8b%ef%bc%9a%e4%bd%bf%e7%94%a8mod_lua%e8%a8%ad%e5%ae%9aapache-2","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/41756\/","title":{"rendered":"Apache\u65b0\u624b\u6559\u7a0b\uff1a\u4f7f\u7528mod_lua\u8a2d\u5b9aApache"},"content":{"rendered":"<h1 id=\"apache%e6%96%b0%e6%89%8b%e6%95%99%e7%a8%8b%ef%bc%9a%e4%bd%bf%e7%94%a8mod_lua%e8%a8%ad%e5%ae%9aapache-emgtJxPsrJ\">Apache\u65b0\u624b\u6559\u7a0b\uff1a\u4f7f\u7528mod_lua\u8a2d\u5b9aApache<\/h1>\n<p>Apache HTTP Server\u662f\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u56e0\u5176\u9748\u6d3b\u6027\u548c\u53ef\u64f4\u5c55\u6027\u800c\u53d7\u5230\u958b\u767c\u8005\u7684\u9752\u775e\u3002\u96a8\u8457\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\u7684\u9700\u6c42\u4e0d\u65b7\u589e\u52a0\uff0cApache\u4e5f\u4e0d\u65b7\u5730\u9032\u884c\u529f\u80fd\u64f4\u5c55\u3002\u5176\u4e2d\uff0cmod_lua\u6a21\u7d44\u7684\u5f15\u5165\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u4f7f\u7528Lua\u8a9e\u8a00\u4f86\u7de8\u5beb\u4f3a\u670d\u5668\u7aef\u7684\u908f\u8f2f\uff0c\u9019\u70baApache\u7684\u4f7f\u7528\u8005\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u53ef\u80fd\u6027\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afmod_lua%ef%bc%9f-emgtJxPsrJ\">\u4ec0\u9ebc\u662fmod_lua\uff1f<\/h2>\n<p>mod_lua\u662f\u4e00\u500bApache\u6a21\u7d44\uff0c\u5141\u8a31\u7528\u6236\u5728Apache\u4f3a\u670d\u5668\u4e2d\u5d4c\u5165Lua\u8173\u672c\u3002Lua\u662f\u4e00\u7a2e\u8f15\u91cf\u7d1a\u7684\u8173\u672c\u8a9e\u8a00\uff0c\u56e0\u5176\u7c21\u6f54\u548c\u9ad8\u6548\u800c\u53d7\u5230\u5ee3\u6cdb\u4f7f\u7528\u3002\u901a\u904emod_lua\uff0c\u7528\u6236\u53ef\u4ee5\u5728Apache\u4e2d\u5be6\u73fe\u52d5\u614b\u5167\u5bb9\u751f\u6210\u3001\u8acb\u6c42\u8655\u7406\u548c\u5176\u4ed6\u4f3a\u670d\u5668\u7aef\u908f\u8f2f\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9dmod_lua-emgtJxPsrJ\">\u5b89\u88ddmod_lua<\/h2>\n<p>\u5728\u4f7f\u7528mod_lua\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u78ba\u4fddApache\u5df2\u7d93\u5b89\u88dd\u4e26\u4e14mod_lua\u6a21\u7d44\u5df2\u7d93\u555f\u7528\u3002\u4ee5\u4e0b\u662f\u5b89\u88dd\u548c\u555f\u7528mod_lua\u7684\u6b65\u9a5f\uff1a<\/p>\n<pre><code>sudo apt-get update\nsudo apt-get install libapache2-mod-lua\nsudo a2enmod lua\nsudo systemctl restart apache2\n<\/code><\/pre>\n<p>\u9019\u4e9b\u547d\u4ee4\u5c07\u6703\u5b89\u88ddmod_lua\u4e26\u91cd\u555fApache\u4f3a\u670d\u5668\u4ee5\u4f7f\u66f4\u6539\u751f\u6548\u3002<\/p>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e9%85%8d%e7%bd%ae-emgtJxPsrJ\">\u57fa\u672c\u914d\u7f6e<\/h2>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u60a8\u53ef\u4ee5\u958b\u59cb\u7de8\u5bebLua\u8173\u672c\u4e26\u5c07\u5176\u6574\u5408\u5230Apache\u914d\u7f6e\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728Apache\u4e2d\u4f7f\u7528Lua\u4f86\u8655\u7406\u8acb\u6c42\uff1a<\/p>\n<pre><code>LuaSocket = require(\"socket\")\n\nfunction handle_request(r)\n    r:content_type(\"text\/plain\")\n    r:puts(\"Hello, World from Lua!\")\n    return apache2.OK\nend\n\n-- \u8a2d\u5b9aURI\u5230Lua\u8655\u7406\u51fd\u6578\napache2.register_handler(\"GET\", \"\/lua\", handle_request)\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70bahandle_request\u7684\u51fd\u6578\uff0c\u7576\u7528\u6236\u8a2a\u554fURI &#8220;\/lua&#8221; \u6642\uff0c\u8a72\u51fd\u6578\u5c07\u88ab\u8abf\u7528\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u7c21\u55ae\u7684\u6587\u672c\u97ff\u61c9\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8lua%e9%80%b2%e8%a1%8c%e6%9b%b4%e8%a4%87%e9%9b%9c%e7%9a%84%e8%99%95%e7%90%86-emgtJxPsrJ\">\u4f7f\u7528Lua\u9032\u884c\u66f4\u8907\u96dc\u7684\u8655\u7406<\/h2>\n<p>mod_lua\u4e0d\u50c5\u50c5\u9650\u65bc\u8fd4\u56de\u975c\u614b\u5167\u5bb9\uff0c\u9084\u53ef\u4ee5\u7528\u4f86\u8655\u7406\u66f4\u8907\u96dc\u7684\u908f\u8f2f\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Lua\u4f86\u8655\u7406\u8868\u55ae\u6578\u64da\u3001\u8207\u6578\u64da\u5eab\u4ea4\u4e92\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5f9eGET\u8acb\u6c42\u4e2d\u7372\u53d6\u53c3\u6578\uff1a<\/p>\n<pre><code>function handle_request(r)\n    local name = r:args()[\"name\"] or \"World\"\n    r:content_type(\"text\/plain\")\n    r:puts(\"Hello, \" .. name .. \" from Lua!\")\n    return apache2.OK\nend\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5f9e\u8acb\u6c42\u7684\u53c3\u6578\u4e2d\u7372\u53d6\u7528\u6236\u7684\u540d\u5b57\uff0c\u4e26\u6839\u64da\u8a72\u540d\u5b57\u8fd4\u56de\u4e0d\u540c\u7684\u97ff\u61c9\u3002<\/p>\n<h2 id=\"%e8%aa%bf%e8%a9%a6%e5%92%8c%e6%97%a5%e8%aa%8c-emgtJxPsrJ\">\u8abf\u8a66\u548c\u65e5\u8a8c<\/h2>\n<p>\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u8abf\u8a66\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002mod_lua\u63d0\u4f9b\u4e86\u65e5\u8a8c\u529f\u80fd\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u8ffd\u8e64\u554f\u984c\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u5c07\u65e5\u8a8c\u4fe1\u606f\u5beb\u5165Apache\u7684\u932f\u8aa4\u65e5\u8a8c\uff1a<\/p>\n<pre><code>function handle_request(r)\n    r:log(\"Handling request for \" .. r.uri)\n    -- \u5176\u4ed6\u8655\u7406\u908f\u8f2f\nend\n<\/code><\/pre>\n<p>\u9019\u6a23\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728Apache\u7684\u932f\u8aa4\u65e5\u8a8c\u4e2d\u67e5\u770b\u8acb\u6c42\u7684\u8a73\u7d30\u4fe1\u606f\uff0c\u9019\u5c0d\u65bc\u6392\u67e5\u554f\u984c\u975e\u5e38\u6709\u5e6b\u52a9\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-emgtJxPsrJ\">\u7e3d\u7d50<\/h2>\n<p>mod_lua\u70baApache\u63d0\u4f9b\u4e86\u4e00\u7a2e\u9748\u6d3b\u7684\u65b9\u5f0f\u4f86\u8655\u7406\u4f3a\u670d\u5668\u7aef\u908f\u8f2f\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5229\u7528Lua\u8a9e\u8a00\u7684\u512a\u52e2\u4f86\u5be6\u73fe\u52d5\u614b\u5167\u5bb9\u751f\u6210\u548c\u8acb\u6c42\u8655\u7406\u3002\u901a\u904e\u7c21\u55ae\u7684\u914d\u7f6e\u548c\u7de8\u78bc\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b\u4e26\u958b\u59cb\u4f7f\u7528mod_lua\u4f86\u64f4\u5c55\u60a8\u7684Apache\u4f3a\u670d\u5668\u529f\u80fd\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u7db2\u7ad9\u6258\u7ba1\u9084\u662f\u61c9\u7528\u958b\u767c\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b66\u4e60\u5982\u4f55\u4f7f\u7528mod_lua\u5728Apache\u4e2d\u8fdb\u884c\u8bbe\u7f6e\uff0c\u638c\u63e1\u57fa\u672c\u914d\u7f6e\u548c\u5b9e\u7528\u6280\u5de7\uff0c\u5feb\u901f\u63d0\u5347\u4f60\u7684Apache\u6280\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-41756","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/41756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=41756"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/41756\/revisions"}],"predecessor-version":[{"id":41757,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/41756\/revisions\/41757"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=41756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=41756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=41756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}