Running an Nginx, PHP, and MySQL Webserver on the Raspberry Pi

For my project, I am going to be hosting a website on the Raspberry Pi. I want to be able to set the alarm on the alarm clock through the website and have the C++ program interact with the MySQL database to get alarm times. Many websites are hosted using Apache, but since Apache can be a bit of a resource hog, I decided to use Nginx. There are several articles out there that explain how to set up these services, but I had to jump between documents and I thought I would just put together a comprehensive guide. Undoubtedly, I'll want to try this again in the future, and having it all written down will be handy.

Running an Nginx, PHP, and MySQL Webserver on the Raspberry Pi

I'm using the Debian package on my RPi, and supposedly the default Nginx Debian package is the wrong one. Here are the commands to install nginx:

1
2
3
4
5
6
cd
echo "deb http://nginx.org/packages/debian/ squeeze nginx" >nginx.list
echo "deb-src http://nginx.org/packages/debian/ squeeze nginx" >> nginx.list
sudo mv nginx.list /etc/apt/sources.list.d
sudo apt-get update
sudo apt-get install nginx

Next, I installed PHP and MySQL:

1
sudo apt-get install mysql-server php5-cgi php5-mysql php5-fpm php5-cli

This installation will take a while. Note that when you are installing MySQL, you will be asked to enter a password for accessing MySQL with. You will need this password again in the future, so choose something you'll remember! Or write it down.

The final step is to modify a configuration file.

1
sudo nano /etc/nginx/sites-available/default

Halfway down the first page are the lines

1
2
root /usr/share/nginx/www;
index index.html index.htm

The first line tells the server where the webpage files (.html, htm, php) are saved. The second line is a list of default index files, in order. Add index.php to the list so that the second line looks like

1
index index.php index.html index.htm

The server will first try index.php, then index.html, and finally index.htm when it tries to load the homepage. Next, scroll down a bit and find the section about the FastCGI server. Everything will be commented out, and we need to uncomment some lines. Additionally, we will add a small if-statement.

 

For more detail: Running an Nginx, PHP, and MySQL Webserver on the Raspberry Pi


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top