How to create a blog on Raspberry Pi 3B+

Hello, this is my first post and I will explain how I created this blog.
We will be using NGINX web server with PHP 7.3 and MySQL for database storage + certbot to create SSL certificates.

NOTE: To create SSL certificate: 1. you must own the domain and 2. the domain must be forwarded to your Raspberry Pi IP Address.

I explained in this post how to install them.

Of course blog.apostolov.info should be replaced with your blog directory name / domain 😀

1. First, you need to create main directory:

sudo mkdir -p /var/www/blog.apostolov.info/public_html

2. Then you need to create the index file, so you don’t get 403 Forbidden page from the server when try to access it:

sudo nano /var/www/blog.apostolov.info/public_html/index.php

Add some random text like “hello world from blog”. This will be the welcome message. Save with Ctrl+X then Y.


3. Next, you need to create nginx server block file:

sudo nano /etc/nginx/sites-available/blog.apostolov.info

Add the following code inside:

server {

        listen 80;
        listen [::]:80;
        server_name blog.apostolov.info;

	root /var/www/blog.apostolov.info/public_html;
	index index.php index.html index.htm;

	# Error & Access logs
	error_log /var/www/blog.apostolov.info/error.log error;
	access_log /var/www/blog.apostolov.info/access.log;

	location / {
		index index.php index.html index.htm;
	}

	location ~ /.well-known {
		allow all;
	}

	location ~ [^/].php(/|$) {
		fastcgi_split_path_info ^(.+?.php)(/.*)$;
		fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
		fastcgi_index index.php;
		include fastcgi_params;
	}

}

Save with Ctrl+X then Y. Note that this is without SSL certificates. After you activate the website and you can see the welcome message, you will come back and edit the file again to use port 443 and not 80. If you don’t want to set SSL certificates, you can skip part 6.


4. Now you need to link the server block file with nginx:

sudo ln -s /etc/nginx/sites-available/blog.apostolov.info /etc/nginx/sites-enabled/blog.apostolov.info

and restart the server to activate it:

sudo systemctl restart nginx

5. You are done now and you can try to access http://blog.apostolov.info, and see “hello world from blog” welcome message.

Again, if you don’t wan’t to set SSL certificates, you can skip next part 6.


6. Again, to create SSL certificate: 1. you must own the domain and 2. the domain must be forwarded to your Raspberry Pi IP Address. Installing SSL certificate is easy:

sudo certbot certonly --agree-tos --webroot -w /var/www/blog.apostolov.info/public_html -d blog.apostolov.info

After that, you need to edit nginx server block that you created in step 3:

sudo nano /etc/nginx/sites-available/blog.apostolov.info

New file should look like this:

server {
        listen 80;
        listen [::]:80;
        server_name blog.apostolov.info;
        return 301 https://$server_name$request_uri;
}

server {

	listen 443 ssl;
	listen [::]:443 ssl;

	server_name blog.apostolov.info;

	ssl_certificate          /etc/letsencrypt/live/blog.apostolov.info/fullchain.pem;
	ssl_certificate_key      /etc/letsencrypt/live/blog.apostolov.info/privkey.pem;

	root /var/www/blog.apostolov.info/public_html;
	index index.php index.html index.htm;

	# Error & Access logs
	error_log /var/www/blog.apostolov.info/error.log error;
	access_log /var/www/blog.apostolov.info/access.log;

	location / {
		index index.php index.html index.htm;
	}

	location ~ /.well-known {
		allow all;
	}

	location ~ [^/].php(/|$) {
		fastcgi_split_path_info ^(.+?.php)(/.*)$;
		fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
		fastcgi_index index.php;
		include fastcgi_params;
	}

}

Save with Ctrl+X then Y. You added one 301 Redirect to 80 port server, so the redirect can safely go to 443 port. Then you closed the server rule and opened new one and added 443 ports and SSL certificates paths in it. Other remains the same.

Now you just need to restart nginx:

sudo systemctl restart nginx

and check if http://blog.apostolov.info will redirect you to https://blog.apostolov.info with green locker icon before the url address.


7. Now you can install WordPress with the following commands:

cd /var/www/blog.apostolov.info/public_html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar xfz latest.tar.gz
cd wordpress
sudo cp -R * ../
cd ..
sudo rm latest.tar.gz 
sudo rm -R wordpress/

You should set owner rights to root directory to nginx for wordpress can write config files and upload files.

sudo chown -R www-data:www-data /var/www/blog.apostolov.info
sudo chmod -R 770 /var/www/blog.apostolov.info

You now can open https://blog.apostolov.info and check if you can see the WordPress installation wizard.

Now, you connect to mysql server with -u and our root username (which couldn’t be root, like I said in the install post):

sudo mysql -u root -p

Enter your root password and then create wordpress user with password and database:

CREATE USER 'wordpressusername'@'localhost' IDENTIFIED BY 'wordpresspassword';

CREATE DATABASE wordpressdatabase CHARACTER SET utf8 COLLATE utf8_general_ci;

GRANT ALL PRIVILEGES ON wordpressdatabase.* TO 'wordpressusername'@'localhost';

FLUSH PRIVILEGES;

Then add this same settings you just created in Wodpress installation wizard.

And voilà! Your blog is ready to use :).


Photo by: unsplash-logoNick Morrison

You may also like...

Leave a Reply

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