We host a personal website on the router

Good day.

A couple of weeks ago I decided to create a personal website 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 the site on someone’s server, and leave my home computer always on too.

Upon reflection, I remembered an interesting article about setting up an Asterisk IP telephony server on a Mikrotik router. Since I installed the router of this particular vendor at home, without hesitation, I decided to raise the nginx web server on it.

So, we have a registered domain name, a “white” static ip-address and a Mikrotik RB751G-2HnD router with a wonderful 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. Model RB751G-2HnD has mipbse architecture:

Download the latest openwrt image for mipbse from the site from here . Copy to the router in Files:

Import the openwrt image into the 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 set the password for the root user.

We edit the network settings to receive network details by dhcp from the router itself:
vi /etc/config/network
config interface lan
option ifname eth0
option proto dhcp

Add the network service to startup and restart:
/etc/init.d/network enable
/etc/init.d/network restart

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

We edit the configuration file of the package manager 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; # имя или 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) {

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 startup and start them:
/etc/init.d/nginx enable
/etc/init.d/nginx start
/etc/init.d/php5-fastcgi enable
/etc/init.d/php5-fastcgi start

We copy the site files to the server in the specified folder and check the site’s performance inside the local network.
Do not forget to change the http port of our router and forward the port 80 of the server to the outside:
/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= in-interface=ether2 disabled=no # in-interface=WAN порт

This completes the setup.


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

Also popular now: