没有不值得去解决的问题,也没有不值得去学习的技术!

Install and Deploy OpenResty on Windows 10 to Run PHP and Lua

Refer to the getting-started documentation: https://openresty.org/cn/getting-started.html . Run the Lua program; the response is normal and meets expectations.

1. URL: http://openresty.org/cn/download.html . Download and install: 64-bit Windows: openresty-1.19.9.1-win64.zip.

2. Extract to the directory: C:\openresty-1.19.9.1-win64 . As shown in Figure 1

Extract to the directory: C:\openresty-1.19.9.1-win64 .
Figure 1

3. Usage documentation for the Windows version of OpenResty: https://github.com/openresty/openresty/blob/master/doc/README-windows.md . Start the NGINX server of the Win64 binary distribution of OpenResty.

Plaintext

PS C:\openresty-1.19.9.1-win64> start nginx
PS C:\openresty-1.19.9.1-win64>

4. Check the Task Manager; in addition to the native Nginx software, nginx.exe has been added. As shown in Figure 2

Check the Task Manager; in addition to the native Nginx software, nginx.exe has been added.
Figure 2

5. You can then use the tasklist command to view the nginx processes running in the background. Two of them belong to the native Nginx software. As shown in Figure 3

You can then use the tasklist command to view the nginx processes running in the background. Two of them belong to the native Nginx software.
Figure 3
Plaintext
PS C:\openresty-1.19.9.1-win64> tasklist /fi "imagename eq nginx.exe"

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
nginx.exe                    15816 Console                    1      7,400 K
nginx.exe                    93052 Console                    1     11,380 K
nginx.exe                    97096 Console                    1      8,848 K
nginx.exe                   117120 Console                    1      9,364 K
PS C:\openresty-1.19.9.1-win64> ./nginx -s stop
PS C:\openresty-1.19.9.1-win64> tasklist /fi "imagename eq nginx.exe"

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
nginx.exe                    15816 Console                    1     11,652 K
nginx.exe                   114616 Console                    1     15,888 K
PS C:\openresty-1.19.9.1-win64>

6. The resty command-line utility requires a Perl interpreter installed on your system and visible in your PATH environment. Any Perl distribution should work, including StrawberryPerl, ActivePerl, and MSYS2 perl. URL: https://strawberryperl.com/ . Perl for MS Windows, free! Download and install strawberry-perl-5.32.1.1-64bit.msi.

7. Edit the PATH environment variable and add a new entry: C:\openresty-1.19.9.1-win64 . As shown in Figure 4

Edit the PATH environment variable and add a new entry: C:\openresty-1.19.9.1-win64 .
Figure 4

8. Restart the computer. Run the resty script. I plan to uninstall the native Nginx software later because their functionalities overlap significantly. As shown in Figure 5

Restart the computer. Run the resty script. I plan to uninstall the native Nginx software later because their functionalities overlap significantly.
Figure 5
Plaintext
PS C:\WINDOWS\system32> tasklist /fi "imagename eq nginx.exe"
INFO: No tasks are running which match the specified criteria.
PS C:\WINDOWS\system32> cd ..
PS C:\WINDOWS> cd ..
PS C:\> cd .\openresty-1.19.9.1-win64\
PS C:\openresty-1.19.9.1-win64> tasklist /fi "imagename eq nginx.exe"
INFO: No tasks are running which match the specified criteria.
PS C:\openresty-1.19.9.1-win64> start nginx
PS C:\openresty-1.19.9.1-win64> tasklist /fi "imagename eq nginx.exe"

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
nginx.exe                    12652 Console                    1      8,972 K
nginx.exe                    17092 Console                    1      9,512 K
PS C:\openresty-1.19.9.1-win64> resty -e "ngx.say('Hello, OpenResty!')"
Hello, OpenResty!
PS C:\openresty-1.19.9.1-win64> cd ..
PS C:\> resty -e "ngx.say('Hello, OpenResty!')"
Hello, OpenResty!
PS C:\>

9. Migrate the configuration items from the native Nginx software’s nginx.conf to OpenResty’s nginx.conf. Reloading throws an error. As shown in Figure 6

Migrate the configuration items from the native Nginx software's nginx.conf to OpenResty's nginx.conf. Reloading throws an error.
Figure 6
Plaintext

PS C:\openresty-1.19.9.1-win64> nginx -s reload
nginx: [warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in ./conf/nginx.conf:48
nginx: [warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in C:/openresty-1.19.9.1-win64/conf/vhosts/msi-main.conf:14
nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 64
PS C:\openresty-1.19.9.1-win64>

10. After editing nginx.conf, reload it, and the error no longer occurs. As shown in Figure 7

After editing nginx.conf, reload it, and the error no longer occurs.
Figure 7

Before adjustment:

Nginx

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       443;
        server_name  localhost;

		client_max_body_size 200m;
		client_body_buffer_size 1024k;

		fastcgi_read_timeout 180s;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

		ssl on;
		ssl_certificate localhost.pem;
		ssl_certificate_key localhost-key.pem;

After adjustment:

Nginx

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

	server_names_hash_max_size  1024;
    server_names_hash_bucket_size  128;

    server {
        listen       443 ssl;
        server_name  localhost;

		client_max_body_size 200m;
		client_body_buffer_size 1024k;

		fastcgi_read_timeout 180s;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

		ssl_certificate localhost.pem;
		ssl_certificate_key localhost-key.pem;

        location / {
            root   E:/wwwroot;
            index  index.html index.htm index.php;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
			fastcgi_split_path_info ^(.+\.php)(/.+)$;
            root           E:/wwwroot;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

	include C:/openresty-1.19.9.1-win64/conf/vhosts/*.conf;

	## PHP-FPM Servers ##
	upstream php-fpm {
		server 127.0.0.1:9000;
	}

}

Plaintext

PS C:\openresty-1.19.9.1-win64> nginx -s reload
PS C:\openresty-1.19.9.1-win64> resty -e "ngx.say('Hello, OpenResty!')"
Hello, OpenResty!
PS C:\openresty-1.19.9.1-win64>

11. Note: all commands must be executed after navigating to the directory: C:\openresty-1.19.9.1-win64 . Otherwise, unexpected errors will occur.

Plaintext
PS C:\WINDOWS\system32> cd ..
PS C:\WINDOWS> cd ..
PS C:\> start nginx
PS C:\> php-cgi.exe -b 127.0.0.1:9000-c C:/php-7.4.6/php.ini
PS C:\> nginx -s reload
nginx: [alert] could not open error log file: CreateFile() "./logs/error.log" failed (3: The system cannot find the path specified)
2021/09/17 19:04:54 [emerg] 3708#3712: CreateFile() "./conf/nginx.conf" failed (3: The system cannot find the path specified)
PS C:\> cd .\openresty-1.19.9.1-win64\
PS C:\openresty-1.19.9.1-win64> nginx -s reload
nginx: [error] CreateFile() "./logs/nginx.pid" failed (2: The system cannot find the file specified)
PS C:\openresty-1.19.9.1-win64> tasklist /fi "imagename eq nginx.exe"
INFO: No tasks are running which match the specified criteria.
PS C:\openresty-1.19.9.1-win64> start nginx
PS C:\openresty-1.19.9.1-win64> tasklist /fi "imagename eq nginx.exe"

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
nginx.exe                     3712 Console                    1     18,068 K
nginx.exe                    13172 Console                    1     18,560 K
PS C:\openresty-1.19.9.1-win64> nginx -s reload
PS C:\openresty-1.19.9.1-win64>

12. Run the PHP program; it runs normally. As shown in Figure 8

Run the PHP program; it runs normally.
Figure 8

13. Add a Lua server

Nginx

server {
	listen 443 ssl;
	server_name morefunresty.dev.chinamcloud.cn;
	charset utf-8;
	lua_need_request_body on;

	access_log  logs/morefunresty.dev.chinamcloud.cn.access.log;
    error_log   logs/morefunresty.dev.chinamcloud.cn.error.log;

	ssl_certificate vhosts/morefunresty.dev.chinamcloud.cn.pem;
	ssl_certificate_key vhosts/morefunresty.dev.chinamcloud.cn-key.pem;

	location / {
		if ($request_method = OPTIONS ) {
			add_header Content-Length 0;
			add_header Content-Type text/plain;
			return 200;
		}
	}

	location ~ ^/lua/api/vote/([-_a-zA-Z0-9/]+) {
		default_type application/json;
		access_by_lua_file  E:/wwwroot/msi_main/resty/app/common/boot/bootstrap.lua;
		content_by_lua_file E:/wwwroot/msi_main/resty/app/mods/vote/$1.lua;
	}

	location ~ ^/lua/api/poll/([-_a-zA-Z0-9/]+) {
		default_type application/json;
		access_by_lua_file  E:/wwwroot/msi_main/resty/app/common/boot/bootstrap.lua;
		content_by_lua_file E:/wwwroot/msi_main/resty/app/mods/poll/$1.lua;
	}

	location ~ ^/lua/api/draw/([-_a-zA-Z0-9]+) {
		default_type application/json;
		access_by_lua_file  E:/wwwroot/msi_main/resty/app/common/boot/bootstrap.lua;
		content_by_lua_file E:/wwwroot/msi_main/resty/app/mods/draw/$1.lua;
	}

	location /draw/times {
		internal;
		content_by_lua_file E:/wwwroot/msi_main/resty/app/mods/draw/times_check.lua;
	}
}

14. Refer to the getting-started documentation: https://openresty.org/cn/getting-started.html . Run the Lua program; the response is normal and meets expectations. As shown in Figure 9

Refer to the getting-started documentation: https://openresty.org/cn/getting-started.html . Run the Lua program; the response is normal and meets expectations.
Figure 9

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系