Installation and deployment of OpenResty under Windows 10, and support running PHP and Lua at the same time

参考入门文档:https://openresty.org/cn/getting-started.html 。运行 lua 程序,响应正常。符合预期。
1. URL: http://openresty.org/cn/download.html , download and install: 64-bit Windows: openresty-1.19.9.1-win64.zip. 2. Unzip to the directory: C:\OpenResty-1.19.9.1-Win64. as shown in Figure 1
解压缩至目录:C:\openresty-1.19.9.1-win64 。
Figure 1
3. Usage documentation for 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.


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, it has also added: nginx.exe. as shown in Figure 2
查看任务管理器,除了原生的 Nginx 软件,还增加了:nginx.exe 。
Figure 2
5. Then you can use the tasklist command to view the nginx process running in the background. 2 of them are native Nginx software. as shown in Figure 3
然后可以使用 tasklist 命令查看后台运行的 nginx 进程。其中 2 个为原生的 Nginx 软件。
Figure 3


PS C:\openresty-1.19.9.1-win64> tasklist /fi "imagename eq nginx.exe"

映像名称                       PID 会话名              会话#       内存使用
========================= ======== ================ =========== ============
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"

映像名称                       PID 会话名              会话#       内存使用
========================= ======== ================ =========== ============
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 the Perl interpreter to be installed on your system and visible to 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, create a new: C:\OpenResty-1.19.9.1-Win64 . as shown in Figure 4
编辑 PATH 环境变量,新建:C:\openresty-1.19.9.1-win64 。
Figure 4
8. Restart the computer. Run the Resty script. It is planned to uninstall the native Nginx software in the future. Because the functions of the two overlap a lot. as shown in Figure 5
重启电脑。运行 resty 脚本。后续计划卸载原生的 Nginx 软件。因为两者功能重叠得厉害。
Figure 5


PS C:\WINDOWS\system32> tasklist /fi "imagename eq nginx.exe"
信息: 没有运行的任务匹配指定标准。
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"
信息: 没有运行的任务匹配指定标准。
PS C:\openresty-1.19.9.1-win64> start nginx
PS C:\openresty-1.19.9.1-win64> tasklist /fi "imagename eq nginx.exe"

映像名称                       PID 会话名              会话#       内存使用
========================= ======== ================ =========== ============
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 item of the native nginx software nginx.conf to the nginx.conf of openresty. Reload, report an error. as shown in Figure 6
迁移原生的 Nginx 软件 nginx.conf 的配置项至 openresty 的 nginx.conf。重新加载,报错。
Figure 6


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, no more errors. as shown in Figure 7
编辑 nginx.conf 后,重新加载,不再报错。
Figure 7
Before adjustment:


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:


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;
	}

}




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 go to the directory: C:\OpenResty-1.19.9.1-Win64 and execute. Otherwise, there will be some unexpected errors.


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"
信息: 没有运行的任务匹配指定标准。
PS C:\openresty-1.19.9.1-win64> start nginx
PS C:\openresty-1.19.9.1-win64> tasklist /fi "imagename eq nginx.exe"

映像名称                       PID 会话名              会话#       内存使用
========================= ======== ================ =========== ============
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 and run normally. as shown in Figure 8
运行 PHP 程序,正常运行。
8
13. Add Lua’s server


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 entry document: https://openresty.org/cn/getting-started.html . Run the Lua program and respond normally. in line with expectations. as shown in Figure 9
参考入门文档:https://openresty.org/cn/getting-started.html 。运行 lua 程序,响应正常。符合预期。
Figure 9

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.