How to Install a LAMP Stack on Debian 11
- Ubuntu 22.04
- Ubuntu 20.04
- Ubuntu 18.04
- Fedora 38
- Debian 8
- Debian 10
- CentOS 8
- CentOS 7
- Arch Linux
- Deprecated guides:
- Ubuntu 9.10
- Ubuntu 9.04
- Ubuntu 8.04
- Ubuntu 16.04
- Ubuntu 14.04
- Ubuntu 12.04
- Ubuntu 11.10
- Ubuntu 11.04
- Ubuntu 10.10
- Gentoo
- Fedora 20
- Fedora 19
- Fedora 15
- Fedora 14
- Fedora 13
- Fedora 12
- Fedora 11
- Debian 7
- Debian 6
- Debian 5
- CentOS 6
- CentOS 5
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
A LAMP stack is a particular bundle of software packages commonly used for hosting web content. The bundle consists of Linux, Apache, MariaDB, and PHP. This guide shows you how to install a LAMP stack on Debian 11 (Bullseye).
Before You Begin
Prior to installing your LAMP stack:
Ensure that you have followed the Getting Started and Securing Your Server guides. Ensure that the Linode’s hostname is set.
Check your Linode’s hostname. The first command should show your short hostname and the second should show your fully qualified domain name (FQDN).
hostname hostname -f
Note If you have a registered domain name for your website, then add the domain to the Linode server on which you plan to install the LAMP stack. If you do not have a registered domain name, then replaceexample.com
with the IP address of the Linode server in the following instructions.Update your system:
sudo apt-get update && sudo apt-get upgrade
Note This guide is written for a non-root user. Commands that require elevated privileges are prefixed withsudo
. If you’re not familiar with thesudo
command, you can check our Users and Groups guide.
Apache
Install and Configure Apache
Install Apache 2.4:
sudo apt-get install apache2
Open
/etc/apache2/mods-available/mpm_prefork.conf
in your text editor and edit the values as needed. The following is optimized for a 2GB Linode:Note As a best practice, you should create a backup of your Apache configuration file before making any configuration changes to your Apache installation. Run the following command to make a backup in your home directory:
cp /etc/apache2/apache2.conf ~/apache2.conf.backup
- File: /etc/apache2/mods-available/mpm_prefork.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# prefork MPM # StartServers: number of server processes to start # MinSpareServers: minimum number of server processes which are kept spare # MaxSpareServers: maximum number of server processes which are kept spare # MaxRequestWorkers: maximum number of server processes allowed to start # MaxConnectionsPerChild: maximum number of requests a server process serves <IfModule mpm_prefork_module> StartServers 4 MinSpareServers 20 MaxSpareServers 40 MaxRequestWorkers 200 MaxConnectionsPerChild 4500 </IfModule> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Note These settings are good starting points, but they should be adjusted to best suit your deployment’s needs.Enable the firewall to allow web traffic. This guide lists the commands to enable web traffic if you configured UFW on your server.
a. If necessary, install UFW. Once UFW is installed, check the ports that are enabled for the
WWW Full
profile:sudo apt install ufw sudo ufw app info "WWW Full"
Ports
80
and443
should be listed as enabled for theWWW Full
profile.b. To allow incoming HTTP and HTTPS traffic for the
WWW Full
profile:sudo ufw allow in "WWW Full"
On Debian 11, the event module is enabled by default. This should be disabled, and the prefork module enabled:
sudo a2dismod mpm_event sudo a2enmod mpm_prefork
Restart Apache:
sudo systemctl restart apache2
Configure Name-Based Virtual Hosts
There can be as many virtual hosts files as needed to support the amount of domains hosted on the Linode.
Create directories for your websites and websites’ logs, replacing
example.com
with your own domain name:sudo mkdir -p /var/www/html/example.com/{public_html,logs}
Repeat the process if you intend on hosting multiple websites on your Linode.
Create a copy of the default Apache configuration file for your site:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
Edit the
example.com.conf
file in/etc/apache2/sites-available
with your text editor, replacing instances ofexample.com
with your own domain URL in both the configuration file and in the file name:- File: /etc/apache2/sites-available/example.com.conf
1 2 3 4 5 6 7 8 9 10 11
<Directory /var/www/html/example.com/public_html> Require all granted </Directory> <VirtualHost *:80> ServerAdmin webmaster@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/html/example.com/public_html ErrorLog /var/www/html/example.com/logs/error.log CustomLog /var/www/html/example.com/logs/access.log combined </VirtualHost>
Repeat this process for any other domains you host:
- File: /etc/apache2/sites-available/example.org.conf
1 2 3 4 5 6 7 8 9 10 11 12
<Directory /var/www/html/example.org/public_html> Require all granted </Directory> <VirtualHost *:80> ServerAdmin webmaster@example.org ServerName example.org ServerAlias www.example.org DocumentRoot /var/www/html/example.org/public_html ErrorLog /var/www/html/example.org/logs/error.log CustomLog /var/www/html/example.org/logs/access.log combined </VirtualHost>
Assign ownership of
public_html
directory to the userwww-data
:sudo chown -R www-data:www-data /var/www/html/example.com/public_html
Set the permissions for the
public_html
directory:sudo chmod -R 755 /var/www/html/example.com/public_html
Link your virtual host file from the
sites-available
directory to thesites-enabled
directory:sudo a2ensite example.com sudo a2ensite example.org
Note If you need to disable a site, you can use issue the following command:
sudo a2dissite example.com
Disable the default virtual host to minimize security risks:
sudo a2dissite 000-default.conf
Restart Apache:
sudo systemctl restart apache2
MariaDB
MariaDB is a relational database management system (RDBMS) and is a popular component of many applications.
Install MariaDB
Install MariaDB:
sudo apt-get install mariadb-server
Input a secure password when prompted by the installation.
Run
mysql_secure_installation
to remove the test database and any extraneous user permissions added during the initial installation process:sudo mysql_secure_installation
It is recommended that you select yes (
y
) for all questions. If you already have a secure root password, you do not need to change it.
Set Up a MariaDB Database
Next, you can create a database and grant your users permissions to use databases.
Log in to MariaDB:
sudo mysql -u root -p
Enter MariaDB’s root password when prompted.
Create a database and grant your users permissions on it. Substitute the database name (
webdata
), username (webuser
), and password (password
) with your own:create database webdata; grant all on webdata.* to 'webuser' identified by 'password';
Exit MariaDB:
quit
PHP
PHP makes it possible to produce dynamic and interactive pages using your own scripts and popular web development frameworks.
Install PHP
Install PHP 7.4, the PHP Extension and Application Repository, Apache support, and MySQL support:
sudo apt install php7.4 libapache2-mod-php7.4 php-mysql
Optionally, install additional cURL, JSON, and CGI support:
sudo apt install php-curl php-json php-cgi
Configure PHP
Open
/etc/php/7.4/apache2/php.ini
in your text editor and edit the following values. These settings are optimized for the 2GB Linode:- File: /etc/php/7.4/apache2/php.ini
1 2
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR max_input_time = 30
Note Ensure the above values are uncommented by making sure they do not start with a semicolon (;).Create the log directory for PHP and give ownership to the Apache user (
www-data
):sudo mkdir /var/log/php sudo chown www-data /var/log/php
Note If you plan on using your LAMP stack to host a WordPress server, install additional PHP modules:
sudo apt install php-gd php-mbstring php-xml php-xmlrpc
Restart Apache:
sudo systemctl restart apache2
Visit your site’s domain (or IP address). You should see Apache’s default welcome page. Your LAMP stack should be installed and is ready to host your site files.
Optional: Test and Troubleshoot the LAMP Stack
In this section, you’ll create a test page that shows whether Apache can render PHP and connect to the MariaDB database. This can be helpful in locating the source of an error if one of the elements of your LAMP stack is not communicating with the others.
Paste the following code into a new file,
phptest.php
, in thepublic_html
directory. Modifywebuser
andpassword
to match the information entered in the Set Up a MariaDB Database section above:- File: /var/www/html/example.com/public_html/phptest.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<html> <head> <title>PHP Test</title> </head> <body> <?php echo '<p>Hello World</p>'; // In the variables section below, replace user and password with your own MariaDB credentials as created on your server $servername = "localhost"; $username = "webuser"; $password = "password"; // Create MariaDB connection $conn = mysqli_connect($servername, $username, $password); // Check connection - if it fails, output will include the error message if (!$conn) { die('<p>Connection failed: </p>' . mysqli_connect_error()); } echo '<p>Connected successfully</p>'; ?> </body> </html>
Navigate to
example.com/phptest.php
from your local machine. If the components of your LAMP stack are working correctly, the browser will display a “Connected successfully” message. If not, the output will be an error message.Remove the test file:
sudo rm /var/www/html/example.com/public_html/phptest.php
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on