jxyblog

jxyblog

email

Building a website server with OpenWrt router, Nginx+php+Sqlite

If your router has a USB port and the flash memory is at least 32Mb, you can change the server to Nginx+php+Sqlite.

Previously, I posted a tutorial on setting up lighttpd+php+Sqlite on an openwrt router, which is roughly the same. You can refer to that earlier article first; this one mainly records the configuration file.

Setting up lighttpd+sqlite+php web server on openwrt router

Initially, I thought about expanding the entire root directory of the router, but later realized it would be difficult to back up and maintain. If the USB fails, all services would need to be reinstalled.

Currently, the website directory is placed on the USB, and the benefit of using Sqlite is that it can be stored together with the website, allowing for one-click backup and easy server migration; just change the database address, and it's done.

Now switching to nginx, all software is installed on the router, and it won't be lost during a power outage. The Nginx+php+Sqlite trio, along with some other components, only occupies about 10Mb of the router's flash memory.

It seems that nginx on openwrt can only be used with php-fpm.

Thanks again to the maintainers of openwrt; these software packages can be used directly, with only minor adjustments to the configuration files, making it manageable even for beginners like me.

When I was using lighttpd, I installed the php-cgi package, but now with nginx, that is no longer needed; just install the php-fpm package, which comes with the php8 interpreter.

Below is the configuration file used by Nginx, located at /etc/nginx/nginx.conf

worker_processes 2;

user root;

include module.d/*.module;

events {
worker_connections  1024;
}

http {
	include mime.types;
	default_type application/octet-stream;
	sendfile on;
         keepalive_timeout 65;
	client_max_body_size 100M;
	large_client_header_buffers 4 64k;

	server { 
		listen 80;
		listen [::]:80;
		server_name jxyblog.top;
                root /mnt/www;
               location / {
            index index.php index.html;
            autoindex  off;
        }

        location ~ \.php(.*)$ {
            root /mnt/www;
            fastcgi_pass   unix:/var/run/php8-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
	}

	include conf.d/*.conf;
}

Below is the Nginx pseudo-static configuration, suitable for typecho blogs. To enable it, add it to the configuration file above.

       if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php$1 last;
        }

A few points to note:

Since nginx replaces the default LUCI of the router when it starts, you should change the default access port of the router from 80 to something else, like 8080, as mentioned in the earlier tutorial.

Otherwise, it may throw an error, and the startup script does not use nginx.conf to start; we also need to modify a configuration file.

The directory is located at /etc/config/nginx, which is the file without an extension. Right-click to open nginx.

config main global
    option uci_enable 'false'    # Just change this to false, and then when the system starts, it will use our own configuration file for restarting NG.

That's it for the major announcements.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.