Raspberry Pi is a simple computer that people can use to learn computer science for fun and hobbies. Sometimes you may need to host a web server on Raspberry Pi. This can seem intimidating since Raspberry Pi has limited hardware. But it is a lot easier than it seems to be. In this article, we will learn how to install Http Server in Raspberry Pi.
How to Install Http Server in Raspberry Pi
For our setup, we will use Raspbian OS, and install NGINX, PHP, MySQL Server. This is because every website needs a database like MySQL, a backend programming language interpreter like PHP and a web server like NGINX. You can also use Apache web server instead of NGINX. We have assumed that Raspbian is already installed on your system.
1. Install NGINX
Open terminal and run the following command to install NGINX on your Raspberry Pi.
$ sudo apt-get update $ sudo apt-get install nginx
If you see any prompts, enter Yes to proceed.
2. Install MySQL
Next, you can install MySQL server with the following command.
$ sudo apt-get install mysql-server
You will see a prompt to enter root password, so enter a secure root password. If you leave it blank, your root account will not have a password.
After installing MySQL server, you can run the following command to secure the installation.
$ sudo mysql_secure_installation
You will see a set of prompts. Enter Yes for all prompts, except the one which asks you if you want to change root password.
3. Install PHP
Run the following command to install PHP 7.4. You can change the version by replacing 7.4 with the PHP version of your choice. If you use a different version of PHP, you will need to replace all occurrences of 7.4 with the version of your PHP, even in next steps.
$ sudo apt-get install php7.4-fpm php7.4-mysql
Once installation is complete, open PHP configuration file in text editor.
$ sudo vi /etc/php7.4/fpm/php.ini
In the above file, look for the following line.
#cgi.fix_pathinfo=1
and change it to
cgi.fix_pathinfo=1
Save and close the file. Next, restart PHP with the following command.
$ sudo systemctl restart php7.4-fpm
4. Configure NGINX with PHP
Next, we will configure NGINX to work with PHP. For this, open NGINX configuration file.
$ sudo vi /etc/nginx/sites-available/default
Edit the configuration file so that it looks like the following. Replace [your public IP] with the public IP of your Raspberry Pi server. You can google it from your browser to determine your system’s public IP.
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; server_name [your public IP]; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php7.4-fpm.sock; } location ~ /\.ht { deny all; } }
Run the following command to test NGINX configuration and re-load NGINX server.
$ sudo nginx -t $ sudo systemctl reload nginx
5. Set up port forwarding
Open your wi-fi router’s admin interface and setup port forwarding as shown below.
Service Port: 80 Internal Port: 80 IP Address: [your Raspberry Pi's IP address] Protocol: TCP Common Service Port: HTTP
Replace [your Raspberry Pi’s IP address] with your Pi’s IP address. You can find it with command hostname -I. If the above configuration does not work for you, try replacing TCP protocol with RCP protocol.
In this article, we have learnt how to install web server and database server on your Raspberry Pi.
Also read:
How to Configure DNS Nameserver in Ubuntu
How to Configure LDAP Client for Ubuntu
How to Copy File to Clipboard in Ubuntu
How to Assign Command Output to Shell Variable
How to Disable Package Updates in Yum/Dnf
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.