Install Web Server (nginx + PHP7.3 + MySQL) on Raspberry Pi 3B+
Hello, in this tutorial, you will learn how to install lightweight web server nginx + newest php 7.3 (at the moment) + MySQL database.
1. To be able to install newest php, let’s start with adding source list files to Raspberry Pi.
wget -q https://packages.sury.org/php/apt.gpg -O- | sudo apt-key add -
echo "deb https://packages.sury.org/php/ stretch main" | sudo tee /etc/apt/sources.list.d/php.list
2. Now you can update to be sure everything is included.
sudo apt update
3. Last – the 1 line installation :). Of curse, additional libraries are not required for now, you can use simply
sudo apt-get install nginx php7.3-fpm php7.3-mysql php7.3-common mysql-server
and add them later if you need them (for instance php7.3-curl php7.3-gd php7.3-cli php7.3-mbstring php7.3-common php7.3-xml php7.3-intl php7.3-ldap php7.3-zip).
4. Testing nginx. Lets first be sure, that server is started with:
sudo /etc/init.d/nginx start
Then, you can try to access http://127.0.0.0 or http://localhost or just type localhost in your web browser. A message “Welcome to nginx!” should be appeared. If so, you have successfully installed simple html web server.
5. Simple configuration for nginx to support php files:
sudo nano /etc/nginx/sites-enabled/default
This is the default website configuration. I deleted mine, so i have to write it by memory :D. First, you have to replace index file to be index.php instead of index.html. Just find the line starting with index (around 43 line):
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
and change it to:
index index.php;
I believe, you wont be using index.html and index.htm :), if so write them at the end. Then, you need to “attach” the php to nginx by finding next lines (around 54 line):
# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}
and change it to:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
}
The default php installation for this version will be in /var/run/php/php7.3-fpm.sock, so everything is good. Now restart the server with the new configuration you just made:
sudo /etc/init.d/nginx restart
6. Last thing is to check if nginx is really working with php. You have to create new index file in the default www directory:
sudo nano /var/www/html/index.php
and add simple phpinfo inside:
<?php echo phpinfo(); ?>
Save with Ctrl+X, then Y. Now, you can refresh your browser and see the php info about the server.
7. Checking and configuration MySQL server. You can start by typing:
sudo mysql_secure_installation
Change the root password, and then answer Y to everything (removing anonymous user, disabling remote connection and deleting test database). Now, lets connect to the MySQL server:
sudo mysql -u root -p
Write down the password for the root, and then the following commands (don’t forget to put semicolon ; after each line):
DROP USER 'root'@'localhost';
You must remove the root user, so you can create new root user with another username for safety purposes :). Now write the new username and if you want, the same password:
CREATE USER 'newusername'@'localhost' IDENTIFIED BY 'newpassword';
Now you need to grant all permissions to this user to the MySQL server:
GRANT ALL PRIVILEGES ON *.* TO 'newusername'@'localhost';
And last, reload privileges for the new user:
FLUSH PRIVILEGES;
You can safely close the server:
QUIT;
Lets just test that everything works with:
sudo mysql -u root -p
After entering some random password, you should get error message ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES). Good, user root is deleted. Now, login with the new newusername/newpassword you just created:
sudo mysql -u newusername -p
If everything is OK, then you have successfully managed to install a working web server :). Everything else is additional configuration.
Hope that I helped 🙂
Photo by Vishnu R Nair