Installing a LAMP stack on a server (Debian and Ubuntu)
Please use the “Print” function at the bottom of the page to create a PDF.
For Cloud Servers, migrated Cloud Servers, VPS and Dedicated Servers on which Debian 11, Debian 12, Ubuntu 18.04, Ubuntu 20.04, Ubuntu 22.04 or Ubuntu 24.04 is installed.
This article explains how to install a LAMP stack on a Cloud Server, a migrated Cloud Server, a VPS or a Dedicated Server with Debian 11, Debian 12, Ubuntu 18.04, Ubuntu 20.04, Ubuntu 22.04 or Ubuntu 24.04. A LAMP stack consists of the Linux operating system and the software applications Apache, MySQL and PHP. These are installed together to host dynamic websites and web applications on a server.
How to install a LAMP stack:
Requirements
Before installing the LAMP stack, you have ensured that your server has sufficient hardware capacity.
Proceed as follows to install Apache:
Install Apache
To check if an update is available, enter the following commands:
root@ubuntu:~# sudo apt update && sudo apt upgrade -y
To install Apache, enter the following command:
root@ubuntu:~# apt install apache2
The installation is started. The following message is displayed during the installation:
The following NEW packages will be installed:
apache2 apache2-bin apache2-data apache2-utils bzip2 libapr1 libaprutil1
libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.3-0 mailcap mime-support
ssl-cert
0 upgraded, 13 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,135 kB of archives.
After this operation, 8,486 kB of additional disk space will be used.
Do you want to continue? [Y/n]- Enter [y] and press [Enter].
Apache is being installed.
- To check whether Apache has been successfully installed and started, enter the public IP address of your server in the following format in your web browser:
http://THE-SERVER-IP-ADDRESS/
If a test page is displayed, Apache has been successfully installed.
Install Maria DB (Debian 11 and Debian 12)
Debian 11 uses MariaDB as the default replacement for MySQL. To install MariaDB, proceed as follows:
Install the MySQL Server metapackage. This package will install MariaDB as a MySQL replacement. To install the package, enter the following command:
root@localhost:~# sudo apt install default-mysql-server
The following message will then be displayed:
After this operation, 195 MB of additional disk space will be used.
Do you want to continue? [Y/n]- Enter [Y].
Start the MariaDB service. To do this, enter the following command:
root@localhost:~# sudo systemctl start mariadb
Activate the MariaDB service to start it automatically at system startup:
root@localhost:~# sudo systemctl enable mariadb
To check the status of the MariaDB service, enter the following command:
root@localhost:~# sudo systemctl status mariadb
- To end the status display, press [q].
Log in to MySQL. To do this, enter the following command:
root@localhost:~# sudo mysql -u root -p
- Enter the root password.
Enter the following command and replace the placeholder MY_NEW_PASSWORD with the desired password.
mysql> ]> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MY_NEW_PASSWORD';
To exit MySQL, enter the following command:
mysql>exit
To run a security script that removes some dangerous default settings and restricts access to the database system, enter the following command:
root@localhost:~# mysql_secure_installation
In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and haven't set the root password yet, you should just press enter here.
Enter current password for root (enter for none):Enter the root password for the MySQL database that you have set.
The following message is then displayed:
OK, successfully used password, moving on...
Setting the root password or using the unix_socket ensures that nobody can log into the MariaDB root user without the proper authorisation.
You already have your root account protected, so you can safely answer 'n'.
Switch to unix_socket authentication [Y/n]Enter [n]. The following message is then displayed:
Switch to unix_socket authentication [Y/n] n
... skipping.
You already have your root account protected, so you can safely answer 'n'.
Change the root password? [Y/n]Enter [n]. The following message is displayed:
By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created for them. this is intended only for testing, and to make the installation go a bit smoother. you should remove them before moving into a production environment.
Remove anonymous users? [Y/n]To remove anonymous users, enter [y] and press [Enter]. The following message is then displayed:
Disallow root login remotely? [Y/n]
Enter [y] and press [Enter]. The following message is then displayed:
Remove test database and access to it?
Enter [y] and press [Enter]. The following message is then displayed:
Reload privilege tables now? [Y/n]
- To reload the MySQL authorisation tables, enter [y]. To confirm the entry, press [Enter].
Install MySQL (Ubuntu 20.04, Ubuntu 22.04, Ubuntu 24.04)
To install MySQL, enter the following command:
root@ubuntu:~# apt install mysql-server
After entering the command, the following message is displayed:
Need to get 28.6 MB of archives.
After this operation, 240 MB of additional disk space will be used.
Do you want to continue? [Y/n]Enter [y] and press [Enter]. MySQL will be installed.
Log in to MySQL. To do this, enter the following command:
root@ubuntu:~# sudo mysql
Enter the following command and replace the placeholder MY_NEW_PASSWORD with the desired password.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MY_NEW_PASSWORD';
To exit MySQL, enter the following command:
mysql>exit
To run a security script that removes some dangerous default settings and restricts access to the database system, enter the following command:
root@ubuntu:~# mysql_secure_installation
After entering the command, the following message is displayed:
Securing the MySQL server deployment.
The following message is then displayed:
VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No:If you activate the VALIDATE PASSWORD PLUGIN, passwords that do not meet the specified criteria will be rejected by MySQL with an error. This can lead to problems if you use a weak password in conjunction with software that automatically configures MySQL user data. For this reason, we recommend that you do not set up this function. However, always use a strong password.
To skip the setup of the Validate Password Plugin, enter [n] and press [Enter]. The following message is displayed:
By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
Remove anonymous users? [Y/n]To remove anonymous users, enter [y] and press [Enter]. The following message is then displayed:
Disallow root login remotely? [Y/n]
Enter [y] and press [Enter]. The following message is then displayed:
Remove test database and access to it?
Enter [y] and press [Enter]. The following message is then displayed:
Reload privilege tables now?
To reload the MySQL authorisation tables, enter [y]. To confirm the entry, press [Enter].
Install PHP
To install the PHP scripting language, proceed as follows:
To install the PHP-MySQL package, enter the following command:
root@ubuntu:~# apt install php libapache2-mod-php php-mysql
The following message is displayed:
After this operation, 18.9 MB of additional disk space will be used.
Do you want to continue? [Y/n]- To continue the installation, enter [y]. Then press [Enter].
Adjust the settings in the dir.conf file
If a user does not enter a specific page in the URL, Apache first searches for the start page with the name index.html by default. To configure Apache so that the index.php file is favoured in this search, proceed as follows:
To open the dir.conf file with the vi editor, enter the following command:
vi /etc/apache2/mods-enabled/dir.conf
Notes
- The vi editor has an insert mode and a command mode. You can call up insert mode with the [i] key. In this mode, the characters entered are immediately inserted into the text. To call up command mode, press [ESC]. If you use command mode, your keyboard input is interpreted as a command.
- vi cannot be terminated in insert mode. Therefore, always enter command mode to exit vi.
Press [i] and adjust the following entry:
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>- Move the index.php entry so that it is behind the DirectoryIndex entry.
- To call up the command mode, press [ESC]. Then enter the command :wq to save the text and close the editor.
You must restart Apache for these changes to take effect. To restart Apache, enter the following command:
root@ubuntu:~# systemctl restart apache2
Install PHP modules
To extend the functionality of PHP, you can install additional modules.
To display the available options for PHP modules and libraries, pass the results of apt search to less. Less is a pager that allows you to display text files in the command line. You can also use less to move around in documents. To do this, enter the following command:
root@ubuntu:~# apt search php- | less
To scroll up or down, use the arrow keys. To exit less, press [q].
To get detailed information about a PHP module, enter the following command:
root@ubuntu:~# apt show package_name
Example:
root@ubuntu:~# apt show php-codesniffer
To install the desired PHP modules, enter the command below:
root@ubuntu:~# apt install package1 package2
Example:
root@ubuntu:~# apt install php-codesniffer php-cli
Test PHP
To test whether PHP has been installed correctly, create a script with the editor. This must be saved in the /var/www/html directory. proceed as follows to create the script and test PHP:
To create the script in the /var/www/html directory, enter the following command:
vi /var/www/html/info.php
The vi editor opens.
Press the [i] key and then enter the following PHP code:
<?php
phpinfo();
?>- To call up the command mode, press [ESC]. Then enter the command :wq to save the text and close the editor.
To test whether the contents of the PHP script are displayed, call up the corresponding URL in the following format in your web browser:
http://THE-SERVER-IP-ADDRESS/info.php
To remove the displayed page again, enter the following command:
rm /var/www/html/info.php