Skip to content

Instantly share code, notes, and snippets.

@QasimTalkin
Last active March 29, 2023 22:50
Show Gist options
  • Select an option

  • Save QasimTalkin/9c727739653ceab0f50156548d94a833 to your computer and use it in GitHub Desktop.

Select an option

Save QasimTalkin/9c727739653ceab0f50156548d94a833 to your computer and use it in GitHub Desktop.
How to Set Up a Web Server with Apache, PHP, MySQL, and PHPMyAdmin on a Raspberry Pi

Set up php-mysql sever on Raspberry pi

Update your Pi apt-get

Before you can begin setting up a web server on your Raspberry Pi, you need to make sure it's up-to-date.

  • sudo apt-get update -> give you latest software packages and security updates.

Install Apache

Installing Apache on your Raspberry Pi allows you to serve web pages to anyone who visits your website.

  • sudo apt-get install apache2 -y

Install PHP

PHP is a server-side scripting language that is used to create dynamic web pages.

  • sudo apt install libapache2-mod-php

Change directory to Public Html and Grant ownership to pi user

When you install Apache, it creates a user called "www-data" that owns the files in the Public Html directory. However, you want to be able to edit these files, so you need to grant ownership of the directory to the "pi" user.

cd /var/www
sudo chown pi: html

Install MySQL

Installing MySQL on your Raspberry Pi allows you to create and manage databases that your web applications can use.

sudo apt install mariadb-server php-mysql -y

Create User Query

When you create a database in MySQL, you need to create a user that has permission to access that database. The command CREATE USER creates a new user with a username and password.

  • run my sql sudo mysql

  • CREATE USER 'qasim'@'localhost' IDENTIFIED BY 'password';

Grant User Permissions

After creating a new user, you need to grant that user permissions to access the database. The command GRANT ALL PRIVILEGES grants the user full access to all databases and tables.

  • GRANT ALL PRIVILEGES ON *.* to 'qasim'@'localhost' WITH GRANT OPTION;

The WITH GRANT OPTION clause in the GRANT command allows the user to grant their permissions to other users.

Install PHPMyAdmin

PHPMyAdmin will provide a graphical user interface for managing MySQL databases.

  • sudo apt-get install phpmyadmin

Edit Apache Config

In order to use PHPMyAdmin, you need to tell Apache where to find the PHPMyAdmin configuration files tells Apache to include myphpadmin files when it starts up.

run this -> sudo nano /etc/apache2/apache2.conf go to last line add->Include /etc/phpmyadmin/apache.conf

  • ctrl-x
  • Y

Restart Apache Service

  • sudo /etc/init.d/apache2 restart

Reboot Raspberry Pi

Finally, rebooting your Raspberry Pi ensures that all changes are loaded

  • sudo reboot

test it by going to localhost and then localhost/phpmyadmin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment