WEB Server with Raspberry PI
This tutorial covers configuring a web server with a Raspberry PI. We will use NGINX instead of Apache due to lower resource usage.
MATERIALS LIST:
- Raspberry PI 3 or higher
- MicroSD memory of at least 16Gb (best quality you can get)
- RaspberryPI-OS installed (ref: Raspberry PI Installation)
- Root access to our raspberry (a
sudo su -is enough)
NGINX INSTALLATION
Once we ensure that our Raspberry is fully configured, we will verify that apache is not installed, using:dpkg-query --list |grep apache
Now we will proceed to install NGINX as a web server (replacing Apache) and enable PHP support.apt -y install nginx
Once installed, we will verify with our browser that the web server is working, that is, in the address bar we type http://IP-OF-THE-RASPBERRY, which should display a welcome page. If it throws an error we should restart the NGINX daemon or restart the raspberry.
PHP INSTALLATION
We will use php-fpm to parse our PHP code. To install everything needed we will use the command:apt -y install php7.4-fpm php7.4-mbstring php7.4-mysql php7.4-curl php7.4-gd php7.4-curl php7.4-zip php7.4-xml
Now we will proceed to configure our PHP, editing the /etc/nginx/sites-enabled/default file
We must look for the text index index.html index.htm; and add our index.php: index index.php index.html index.htm;, at the beginning so that it always takes php as the first option.
Then we must enable PHP, so we look for this part of the configuration:
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
# fastcgi_pass unix:/var/run/php5-fpm.sock;
#}
and replace it with:
location ~ \.php$ {. This is so that NGINX processes .php files through PHP-FPM.
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
Then restart the NGINX service and we can test by generating a .php file containing the phpinfo() directive.
Finally, to enable php short tags, we must find the php configuration files (php.ini) and ensure they are enabled (short_open_tag=on), and then we must restart both the php-fpm and nginx services.service php(version)-fpm restart
service nginx restart