This is a simple guide on how to host your websites (including WordPress) on your Raspberry Pi.
Before we begin with the setup of the web server, I would like to clarify on the few basic things we will be using.
What is a LEMP Stack?
The LEMP software stack is a group of software that can be used to serve dynamic websites and web applications. LEMP is an acronym, which refers to a Linux operating system, with Nginx web server. MySQL database to store backend data and PHP to handle the dynamic processing.
Prerequisites
You will need a RaspberryPi with Raspbian OS installed on it. You can refer to this guide (Step 1) to set up Raspbian OS on your RaspberryPi. Now we can start with setting up the rest of the components.
Step 1. Install the Nginx Web Server
First, we will look for updates for our installed OS.
sudo apt-get update sudo apt-get upgrade
Now, we will install the web server with this simple command.
sudo apt-get install nginx
The above command will download and install all the necessary files and will set nginx web server on your Pi.
You can verify the installation by typing in the local IP assigned to your Raspberry Pi in the browser of any other devices on the same network (maybe your PC).
http://192.168.xxx.xxx (or) http://10.xxx.xxx.xxx
If you see this page in your browser, you have successfully installed Nginx on your Pi.
Step 2: Install MySQL for your database
Now that we have successfully installed a web server on our pi, we will need a software to store and manage our database. So we need to install MySQL.
You can do this by using this simple command.
sudo apt-get install mysql-server
During installation, you will be prompted to set up a password for the MySQL administrative “root” user. Do remember this password for future management of the databases.
The MySQL software is now installed. We will configure the software now to make it more secure.
We will use this command to run the MySQL security script.
sudo mysql_secure_installation
You will be prompted to enter the “root” password which you have provided during the installation. You can simply skip the step to change the root password.
You have to press “Y"
, followed by the “ENTER"
key for all the further prompts made by the script. These responses remove the anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately accepts the changes.
MySQL is securely set up on your Pi. It’s time to set up PHP.
Step 3: Install PHP
PHP will be used alongside nginx and MySQL to generate and serve dynamic contents.
We will install php5-fpm (fastCGI process manager for PHP5) as nginx does not contain native PHP. We will tell Nginx to pass PHP requests to this software for processing. We’ll also install an additional helper package that will allow PHP to communicate with our MySQL database backend. This installation will pull in the necessary PHP core files to make that work.
First, we will start by updating the sources.
sudo apt-get update
Then we will install the php5-fpm and php5-mysql packages.
sudo apt-get install php5-fpm php5-mysql
PHP modules are successfully installed on your RaspberryPi. Now we will secure the installation by making a small change in the php.ini file.
Open the php-fpm configuration file with root privileges.
sudo nano /etc/php5/fpm/php.ini
Now scroll down to the “Paths and Directories” section of the file and look for
;cgi.fix_pathinfo=1
This setting allows users to execute scripts that they shouldn’t be allowed to run.
We will change these conditions by removing “;” and making the value to “0”. The new setting will look like this.
cgi.fix_pathinfo=0
After making the changes, save and close the file.
We will restart the PHP processor for the changes to be implemented.
sudo systemctl restart php5-fpm
At this point, all the LEMP stack components are successfully installed on your Raspberry Pi.
Step 4: Configure nginx to use the PHP processor
We will open the default server configuration file.
sudo nano /etc/nginx/sites-available/default
We will make the following change to the server block in the configuration file. We will remove few comments and add few lines to the file.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
//add index.php
index index.php index.html index.htm index.nginx-debian.html;
//add your server ip (192.168.xxx.xxx) or your public IP
server_name your_server_ip;
location / {
try_files $uri $uri/ =404;
}
//remove the comments from these codes
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
After making these changes, save and close the file.
Now you can test the configuration file for syntax error if any.
sudo nginx -t
You should get “Success” message. If any errors are reported, you can go back to the configuration file and recheck the changes made.
We will now reload the nginx server for the all the changes to be implemented.
sudo systemctl reload nginx
At this point, your LEMP stack is completely set up and configured on your Raspberry Pi. We will now validate if nginx can correctly use PHP processor for .php files.
We can create a simple PHP file in our root doc to test this.
sudo nano /var/www/html/test.php
Simply add the following codes in the open text editor.
<?php
phpinfo();
?>
Now save and close the file.
You can visit the page by typing in the local IP assigned to your Raspberry Pi in the browser of any other devices on the same network (maybe your PC).
http://your_local_ip/test.php
You will get a web page that has been generated by PHP with your server information. If you get a page like this, you’ve set up PHP processing with Nginx successfully.
You can now delete this page as it gives out information to unauthorized users about your server.
sudo rm /var/www/html/test.php
Congratulations! You now have a LEMP stack configured on your Raspberry Pi.
This paragraph presents clear idea for the new visitors of
blogging, that truly how to do blogging.