Set Up A LAMP Web Server On A Raspberry Pi in 2024 - RaspberryTips
Set Up A LAMP Web Server On A Raspberry Pi in 2024 - RaspberryTips
com/web-server-setup-on-raspberry-pi/
HOW-TO TUTORIALS
Here are the required steps to set up a web server on a Raspberry Pi:
1 de 24 15/04/2024, 02:53 a. m.
Set Up a LAMP Web Server On A Raspberry Pi In 2024 – RaspberryTips https://raspberrytips.com/web-server-setup-on-raspberry-pi/
We’ll now learn how to complete each step in detail. I’ll also show you how to
install and configure them to work together. And I’ll end this tutorial with
some extra tips.
By the way, if you are really interested in improving your skills on Raspberry Pi,
I highly recommend to check out my e-book here. It’s a 30-days challenge
from beginner to master, with step-by-step tutorials and many projects to
practice along the way.
Note: If you want to see all these steps in action, I have a video
lesson available for the community members. You can join here and
watch it directly if you are interested (with 10+ other lessons for
Raspberry Pi and many other benefits).
2 de 24 15/04/2024, 02:53 a. m.
Set Up a LAMP Web Server On A Raspberry Pi In 2024 – RaspberryTips https://raspberrytips.com/web-server-setup-on-raspberry-pi/
Apache
The main goal for the Apache service is to answer requests on HTTP and
HTTPS ports of your Raspberry Pi.
HTTP and HTTPS are the main protocols on the Internet for web surfing.
It works because Apache is here to respond to your browser requests on ports
80 and 443.
3 de 24 15/04/2024, 02:53 a. m.
Set Up a LAMP Web Server On A Raspberry Pi In 2024 – RaspberryTips https://raspberrytips.com/web-server-setup-on-raspberry-pi/
Learn more
PHP
PHP is here to bring dynamic content to your web pages.
HTML is a static page language you can supercharge with PHP code to make
your page look different depending on external conditions (visitor language,
visitor history, time of the day, etc.).
Download now
• PHP compiles the PHP code in your page (via an Apache module).
• PHP generates a specific HTML code.
• Apache always displays static HTML code, but this time PHP
generates it before.
I’ll show you examples later in this post to better understand this part.
4 de 24 15/04/2024, 02:53 a. m.
Set Up a LAMP Web Server On A Raspberry Pi In 2024 – RaspberryTips https://raspberrytips.com/web-server-setup-on-raspberry-pi/
MySQL
MySQL is a database management system that allows you to host a
database and store any data you need to keep on your site.
For example, on RaspberryTips, there is a database with all the posts, all the
images links, all the comments, etc.
5 de 24 15/04/2024, 02:53 a. m.
Set Up a LAMP Web Server On A Raspberry Pi In 2024 – RaspberryTips https://raspberrytips.com/web-server-setup-on-raspberry-pi/
I’ll let you make your choice and skip the unnecessary parts in the following
process.
Once done, you are ready to start with the installation of the first component
of your web server: Apache.
Are you a bit lost in the Linux command line? Check this article first for the
6 de 24 15/04/2024, 02:53 a. m.
Set Up a LAMP Web Server On A Raspberry Pi In 2024 – RaspberryTips https://raspberrytips.com/web-server-setup-on-raspberry-pi/
As you can see in this page content, web pages are located under /var/
www/html on the Raspberry Pi.
You can edit index.html to change this page or add new pages in the same
folder.
You need administrator privileges to do this, so make sure to use “sudo” as a
prefix for all of your commands (more details here).
7 de 24 15/04/2024, 02:53 a. m.
Set Up a LAMP Web Server On A Raspberry Pi In 2024 – RaspberryTips https://raspberrytips.com/web-server-setup-on-raspberry-pi/
Another option is to remove this page and just upload the files you want to
share in this folder.
For example, if you just need to share files on your network, remove the
index.html and put your files directly in /var/www/html, Apache will
automatically generate a web page with links to each one.
Download now
Installation
To install PHP, we need to add two packages:
sudo apt install php libapache2-mod-php
The first one adds the core packages for PHP and the possibility to run PHP
8 de 24 15/04/2024, 02:53 a. m.
Set Up a LAMP Web Server On A Raspberry Pi In 2024 – RaspberryTips https://raspberrytips.com/web-server-setup-on-raspberry-pi/
scripts in a terminal.
The second one supercharges Apache with PHP capability, allowing us to put
PHP code in our HTML pages.
Follow these steps to create a PHP file and test it in your browser:
If you see this page, everything is OK, Apache and PHP servers are installed
correctly, and you can use HTML and PHP code in your pages.
Move to the next section if you need to use MySQL too.
9 de 24 15/04/2024, 02:53 a. m.
Set Up a LAMP Web Server On A Raspberry Pi In 2024 – RaspberryTips https://raspberrytips.com/web-server-setup-on-raspberry-pi/
The first package (mariadb-server) installs the main server for your MySQL
databases.
The second package adds the possibility to use MySQL functions in your PHP
code (like the link between Apache and PHP).
I have a complete tutorial about MariaDB only if you want to learn more about
this. But I’ll give you the quick version here.
For a quick start, we’ll create a MySQL superuser to use in our PHP code later.
But you can create as many users as you want with different privileges:
10 de 24 15/04/2024, 02:53 a. m.
Set Up a LAMP Web Server On A Raspberry Pi In 2024 – RaspberryTips https://raspberrytips.com/web-server-setup-on-raspberry-pi/
If you’re new with these database words, imagine the database as an Excel
spreadsheet and a table as a tab in this spreadsheet. We’ll store data in this
table.
• Go back to the MySQL console, but with the “webuser” this time:
mysql -uwebuser -ppassword test
Obviously, this is a basic useless table, if you want to use anything else
you can.
Press ENTER after pasting the request.
11 de 24 15/04/2024, 02:53 a. m.
Set Up a LAMP Web Server On A Raspberry Pi In 2024 – RaspberryTips https://raspberrytips.com/web-server-setup-on-raspberry-pi/
We add a first line in your table, with the test sentence in the data field.
Yes, I’m very creative with this.
• Exit:
quit
12 de 24 15/04/2024, 02:53 a. m.
Set Up a LAMP Web Server On A Raspberry Pi In 2024 – RaspberryTips https://raspberrytips.com/web-server-setup-on-raspberry-pi/
}
?>
You may need to edit the first line to fit your first user login and
password.
I won’t do a PHP lesson here, but basically, this script tries to connect to
your MySQL server and read the “test” table. If successful, it displays all
the content of the “test” table in your web browser.
• Save and Exit (CTRL+O, CTRL+W).
• Switch to your web browser and check the URL: http://<RASPBERRY
IP>/test-mysql.php.
This should display the content of your database table.
Here we are, you know how to install a basic web server and create a first page
to connect to MySQL and display something from your database.
Feel free to adapt the database and the PHP code to fit your needs.
Extra tips
13 de 24 15/04/2024, 02:53 a. m.
Set Up a LAMP Web Server On A Raspberry Pi In 2024 – RaspberryTips https://raspberrytips.com/web-server-setup-on-raspberry-pi/
I’ll give you two extra tips to improve your web server or to make it easier to
manage.
PHPMyAdmin
Previously in this post, we made all the changes in the MySQL database
manually, doing the SQL requests directly to create users, grant access or
insert data.
But there is a popular tool to do this faster and intuitively: PHPMyAdmin.
It’s a free tool that provides a web interface to manage your MySQL server.
14 de 24 15/04/2024, 02:53 a. m.
Set Up a LAMP Web Server On A Raspberry Pi In 2024 – RaspberryTips https://raspberrytips.com/web-server-setup-on-raspberry-pi/
Virtual Hosts
If you want to host several websites or apps in your Raspberry Pi, you
have two choices:
• Use one folder for each app, like http://192.168.1.1/app1 and http://
192.168.1.1/app2.
• Use virtual hosts to have an address like http://app1.domain.com and
http://app2.domain.com.
For the first option, just create as many folders as you need in /var/www/html.
For the second one, you need to:
Download now
This tutorial doesn't work anymore? Report the issue here, so that I can update it!
15 de 24 15/04/2024, 02:53 a. m.
Set Up a LAMP Web Server On A Raspberry Pi In 2024 – RaspberryTips https://raspberrytips.com/web-server-setup-on-raspberry-pi/
Conclusion
I hope this article will help you to install a basic web server on your Raspberry
Pi.
I think this is rather a beginner how-to guide, and I could write a lot more on
this topic.
If you are interested, I’ll create other posts on Apache/PHP/MySQL to help you
master these main services on Linux.
Also, if your website is kind of essential, like with real applications that need to
be accessible at all time, and remote users, it’s probably a good idea to put
something in place to monitor it. You can read my article on the topic here:
The Definitive Guide To Monitor A Website On Raspberry Pi
Whenever you’re ready, here are other ways I can help you:
16 de 24 15/04/2024, 02:53 a. m.
Set Up a LAMP Web Server On A Raspberry Pi In 2024 – RaspberryTips https://raspberrytips.com/web-server-setup-on-raspberry-pi/
You can also find all my recommendations for tools and hardware
on this page.
Patrick Fromaget
17 de 24 15/04/2024, 02:53 a. m.