Edited 2 weeks ago by ExtremeHow Editorial Team
DebianLAMPServer SetupApacheMySQLPHPWeb DevelopmentLinuxOpen SourceSystem Administration
This content is available in 7 different language
LAMP is a popular software stack used for web development. This acronym stands for Linux, Apache, MySQL (or MariaDB), and PHP. This guide will show you how to set up a LAMP server on the Debian operating system. By following these instructions, you will turn a simple Debian system into a working server ready to host websites and applications.
Before proceeding, make sure your Debian system is up-to-date. You need a user with sudo privileges to install the required packages. Also, having a basic understanding of terminal commands can be helpful.
Start by updating the package repositories to ensure you have the latest versions of the software available. Open the terminal and type:
sudo apt update
This command fetches updates for the packages available in the repositories. Next, upgrade the system like this:
sudo apt upgrade
Type 'Y' when prompted to confirm installation of the updates.
Apache is a widely used open source web server software. To install Apache on your Debian system, enter the following command:
sudo apt install apache2
After the installation is complete, verify that Apache is running by checking its status:
sudo systemctl status apache2
If Apache is running correctly, you will see an Active status. You can also test this in your browser by entering your server's IP address. You should see the default Apache Debian page.
Next, you'll need a database management system. MySQL is the most popular choice, but MariaDB is also a great option. You can use any of these; this guide will use MariaDB.
Install MariaDB using the following command:
sudo apt install mariadb-server mariadb-client
Once installed, start the MariaDB service and enable it to start on boot:
sudo systemctl start mariadb sudo systemctl enable mariadb
You should now secure your MariaDB installation:
sudo mysql_secure_installation
Follow the on-screen instructions to set the root password and remove anonymous users, disallow root logins remotely, delete the test database, and reload the privilege tables.
PHP is a server-side scripting language designed primarily for web development. Install PHP along with some commonly used modules:
sudo apt install php libapache2-mod-php php-mysql
Once PHP is installed, you can test it by creating a PHP file in the root directory of the web server:
sudo nano /var/www/html/info.php
Type in the text editor:
<?php phpinfo(); ?>
Save the file and exit the editor. Restart Apache so it can recognize the changes:
sudo systemctl restart apache2
Now go to http://your_server_ip/info.php
in your browser. This page will show all the information about your PHP installation.
It is important to configure the firewall to allow web traffic. If you have UFW (Uncomplicated Firewall) installed, you can allow HTTP and HTTPS traffic with the following command:
sudo ufw allow 'Apache Full' sudo ufw enable
Confirm the current firewall rules by typing the following:
sudo ufw status
This command will check that both HTTP and HTTPS traffic is allowed.
Just as you can host multiple websites on the same server with unique domain names, virtual hosts allow you to configure Apache to serve different websites from the same server. By default, Apache comes with a default virtual host file located at /etc/apache2/sites-available/000-default.conf
. You can modify this file or create new configuration files for each site you host on your server.
Create a new configuration file using the example below:
sudo nano /etc/apache2/sites-available/your_domain.conf
Add the following ingredients to it:
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName your_domain.com ServerAlias www.your_domain.com DocumentRoot /var/www/your_domain <Directory /var/www/your_domain/> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Enable the new virtual host with the command:
sudo a2ensite your_domain.conf
Disable the default site if you don't need it and restart Apache:
sudo a2dissite 000-default.conf sudo systemctl restart apache2
Create a directory for your domain in the /var/www
directory and give it the appropriate permissions:
sudo mkdir /var/www/your_domain sudo chown -R $USER:$USER /var/www/your_domain sudo chmod -R 755 /var/www/your_domain
You can now place your website files in /var/www/your_domain
.
Double-check the Apache configuration for syntax errors:
sudo apache2ctl configtest
It should return "Syntax OK". Restart Apache if no errors are found:
sudo systemctl restart apache2
To make sure all the components are working together, go back to your info.php test file in a browser. If everything is set up correctly, you have successfully deployed a LAMP server.
This guide has introduced you to the process of setting up a LAMP server on Debian. This is just the beginning; LAMP servers are highly versatile, and there are countless ways to extend and customize your setup. You can add features like SSL for secure connections, configure additional databases, or discover new web applications to deploy. Enjoy the vast possibilities that LAMP servers on Debian offer to developers.
If you find anything wrong with the article content, you can