⬆️ ⬇️

Host personal website on the router

Good day.



A couple of weeks ago I decided to create a personal site with several pages about myself, my achievements, goals and contacts. Of course, one of the stages of its creation was the question of choosing a hosting. I didn’t want to give money for hosting a website on someone’s server, and leaving my home computer on all the time.



Upon reflection, I remembered an interesting article about setting up the Asterisk ip-telephony server on the Mikrotik router. Since I have a router of this manufacturer installed at my home, without hesitation, I decided to pick up the nginx web server on it.



So, we have a registered domain name, a “white” static ip-address and a Mikrotik RB751G-2HnD marshutor with a great MetaRouter function. Let's start by choosing the openwrt image for our router. We go to the router through the winbox program and look at its architecture. The model RB751G-2HnD mipbse architecture:

')





We download the latest openwrt image for mipbse from here . Copy to the router in Files:







We import the openwrt image into a meta-router, and add the network interface for our virtual image:

/metarouter import-image file-name=openwrt-mr-mips-rootfs-31411-basic.tar.gz memory=24 enabled=yes /metarouter interface add virtual-machine=mr2 type=dynamic dynamic-bridge=bridge_local 


Then we enter the image console and first of all we set a password for the root user.







We edit the network settings for receiving network details via dhcp from the router itself:

 vi /etc/config/network config interface lan option ifname eth0 option proto dhcp 


Add network service to autoload and restart:

 /etc/init.d/network enable /etc/init.d/network restart 


We received network details, and now we have access to the system via ssh.







Edit the package manager configuration file to update the repository and install the necessary programs:

 vi /etc/opkg.conf src/gz snapshots http://openwrt.wk.cz/trunk/mr-mips/packages dest root / dest ram /tmp lists_dir ext /var/opkg-lists option overlay_root /overlay 


Next, update the repository and install nginx and php:

 opkg update opkg install nginx php5 php5-fastcgi 


We proceed to configure the web server.

 mv /etc/nginx/nginx.conf /etc/nginx/nginx_example.conf vi /etc/nginx/nginx.conf user nobody nogroup; worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } http { include mime.types; index index.php index.html index.htm; default_type text/html; sendfile on; keepalive_timeout 65; gzip on; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; gzip_min_length 1k; server { listen 80; #   server_name 172.16.0.12; #   ip-  fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 32k; fastcgi_buffers 4 32k; fastcgi_busy_buffers_size 32k; fastcgi_temp_file_write_size 32k; client_body_timeout 10; client_header_timeout 10; send_timeout 60; output_buffers 1 32k; postpone_output 1460; root /srv/www; #     location ~ \.php$ { fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; if (-f $request_filename) { fastcgi_pass 127.0.0.1:1026; } } } } 


Editing php parameters:

 vi /etc/php.ini doc_root = "srv/www" cgi.force_redirect = 1 cgi.redirect_status_env = "yes"; 




And check the fascgi parameters:

 vi /etc/nginx/fastcgi_params fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; 


Add services to autoload and run them:

 /etc/init.d/nginx enable /etc/init.d/nginx start /etc/init.d/php5-fastcgi enable /etc/init.d/php5-fastcgi start 


Copy the site files to the server in the specified folder and check the performance of the site within the local network.

Do not forget to change the http port of our router and forward the 80th port of the server to the external one:

 /ip service set port=8080 2 disabled=yes # or no /ip firewall nat add chain=dstnat action=dst-nat dst-address=_IP- protocol=tcp dst-port=80 to-ports=80 to-addresses=172.16.0.12 in-interface=ether2 disabled=no # in-interface=WAN  


At this setting is over.



Conclusion



Thus, we received conditionally (availability of a router) unplugged hosting for a personal site with minimal power consumption and consumption of router resources.

Source: https://habr.com/ru/post/248009/



All Articles