Each component must be installed separately and in a specific order. In this guide, we will go through all the steps of installing LAMP Stack on Debian 10.
For the purposes of this guide, we assume that you have a server installed and configured with:
- SSH access as root
- Debian 10 or 11
- Terminal/command line access
Installing Apache Server
Apache is a popular open-source web server that can be used in conjunction with PHP. Apache has excellent documentation on the internet and has been one of the leaders for most of the internet's history. Apache servers are widely used on all types of hosting. To get started, make sure your apt cache is up to date with:sudo apt update
sudo apt install apache2
http://<your_server_ip>
Finding your server's public IP address:ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
Enter the address in your browser, and you will see the default Apache web page, which is there for informational and testing purposes. It should look something like this:
If this page appears, your web server is now correctly installed and accessible on the internet.
Installing MySQL MariaDB
Once you have started the web server, it is time to install MariaDB, a database server. This server will organize and provide access to databases where your website can store information. To install MariaDB, we will again use apt to install the server:sudo apt install mariadb-server
This command will also display a list of the packages that will be installed, along with the amount of disk space they will occupy. Type Y and then ENTER to confirm and continue.
Once the installation is complete, run the security script that comes with MySQL, which will remove some dangerous defaults and lock down access to the database system. Run the script through the console and follow the instructions on the screen:
sudo mysql_secure_installation
sudo mariadb
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 74
Server version: 10.3.15-MariaDB-1 Debian 10
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
Note that you did not need to provide a password to log in as root. This works because the default authentication method for the MariaDB root user is via unix_socket instead of a password. Although this may seem like a security issue at first, it actually makes the database server more secure, as the only users allowed to log in as the MariaDB root user are system users with sudo privileges, who log in from the terminal or through an application running with those privileges.
The root user role is administrative only, so you cannot connect your PHP application.
To increase security, we will set up dedicated user accounts with appropriate privileges for each database. To demonstrate this setup, we will create a database named example_database and a user named example_user. You can replace these names with any names you wish.
To create a new database, run the following command from the MariaDB console:
CREATE DATABASE example_database;
Now we will create a new user and grant them full privileges on the database we created. The following command sets this user's password to PASSWORD, but you should replace this value with a secure and strong password:
GRANT ALL ON example_database.* TO 'example_user'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
The command will grant the user example_user full privileges on the database example_database, while preventing this user from creating or modifying other databases on the server.
You can check the privileges to ensure that they are saved and available without having to restart:
FLUSH PRIVILEGES;
You can then exit MariaDB:
exit
You can check if the new user has the appropriate permissions by logging back into the MariaDB console with the username and password you entered earlier:
mariadb -u example_user -p
Note: Using -p in this command will prompt you for the password you chose when creating the example_user user. After logging into the MariaDB console, confirm that you have access to the example_database database:
SHOW DATABASES;
You will receive the following output:
+--------------------+
| Database |
+--------------------+
| example_database |
| information_schema |
+--------------------+
2 rows in set (0.000 sec)
To exit MariaDB, type:
exit
At this point, the database system is configured and you can move on to installing PHP, the final component of the LAMP server.
Installing PHP on LAMP
PHP is a programming language for displaying dynamic websites. It can run scripts, connect to MariaDB databases to receive and send information from the web server and back, and display it to the user. We will use apt again to install PHP, along with a few other packages that will ensure that PHP code can run under the Apache server and communicate with the MariaDB database:sudo apt install php libapache2-mod-php php-mysql
This should install PHP without any problems, but we will check that everything is working properly.
In most cases, it is a good idea to change the Apache settings to make the index.php file accessible first, rather than index.html. Currently, if a user requests a directory from the server, Apache will first look for a file named index.html. We want to tell the web server to prefer PHP files over others, so we need to configure Apache to look for the index.php file.
To do this, type the following command to open the dir.conf file in a text editor with root privileges:
sudo nano /etc/apache2/mods-enabled/dir.conf
The file will look like this:
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Move the index.php file (highlighted above) to the first position after DirectoryIndex, like this:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Finally, save the file. If you are using nano, you can do this by pressing CTRL + X and then Y and ENTER to confirm.
Now reload Apache with:
sudo systemctl reload apache2
You can check the status of apache2:
sudo systemctl status apache2
apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-07-08 12:58:31 UTC; 8s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 11948 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 11954 (apache2)
Tasks: 6 (limit: 4719)
Memory: 11.5M
CGroup: /system.slice/apache2.service
├─11954 /usr/sbin/apache2 -k start
├─11955 /usr/sbin/apache2 -k start
├─11956 /usr/sbin/apache2 -k start
├─11957 /usr/sbin/apache2 -k start
├─11958 /usr/sbin/apache2 -k start
└─11959 /usr/sbin/apache2 -k start
At this point, your LAMP server is configured. Now you can take care of the rest of the settings, such as vhosts.