- Posted on
How to Install an Apache Web Server Powered by PHP and MySQL on Linux CLI Using Package Managers
This guide outlines the steps to install Apache, PHP, and MySQL (often referred to as the LAMP stack: Linux, Apache, MySQL, and PHP) on a Linux system using package managers such as APT (for Debian/Ubuntu-based distributions) and DNF (for CentOS/RHEL-based distributions).
Prerequisites
- A Linux server (Debian/Ubuntu/CentOS 8+).
- Root or
sudo
privileges to install and configure packages. - Access to the command line or SSH.
Step-by-Step Installation:
Step 1: Update the System
Before installing any packages, it’s important to ensure that your system is up to date.
For Debian/Ubuntu (APT)
sudo apt update
sudo apt upgrade -y
For CentOS 8+ (DNF)
sudo dnf update -y
Step 2: Install Apache Web Server
Apache is the most widely used web server to serve static and dynamic content.
For Debian/Ubuntu (APT)
Install Apache with the following command:
sudo apt install apache2 -y
Enable Apache to start on boot:
sudo systemctl enable apache2
Start Apache:
sudo systemctl start apache2
Verify that Apache is running:
sudo systemctl status apache2
For CentOS 8+ (DNF)
Install Apache (httpd) using the following:
sudo dnf install httpd -y
Enable Apache to start on boot:
sudo systemctl enable httpd
Start Apache:
sudo systemctl start httpd
Verify that Apache is running:
sudo systemctl status httpd
To ensure Apache is accessible through the firewall, run:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
Step 3: Install PHP
Apache alone can serve static content, but to serve dynamic content (such as web applications), we need PHP.
For Debian/Ubuntu (APT)
Install PHP and the required Apache module:
sudo apt install php libapache2-mod-php -y
Restart Apache to load the PHP module:
sudo systemctl restart apache2
For CentOS 8+ (DNF)
On CentOS 8+, PHP can be installed from the default CentOS repositories or from the Remi repository if you need a more recent version of PHP. To install PHP from the default CentOS 8+ repositories:
sudo dnf install php php-mysqlnd php-fpm -y
After installation, restart Apache to apply the PHP configuration:
sudo systemctl restart httpd
Step 4: Install MySQL (or MariaDB)
MySQL (or MariaDB, which is a drop-in replacement for MySQL) is needed to store and retrieve data for web applications.
For Debian/Ubuntu (APT)
Install MySQL:
sudo apt install mysql-server -y
Secure the MySQL installation:
sudo mysql_secure_installation
Start MySQL:
sudo systemctl start mysql
Enable MySQL to start on boot:
sudo systemctl enable mysql
Verify MySQL is running:
sudo systemctl status mysql
For CentOS 8+ (DNF)
CentOS 8 comes with MariaDB as the default MySQL-compatible database server. To install MariaDB:
sudo dnf install mariadb-server -y
Start MariaDB:
sudo systemctl start mariadb
Enable MariaDB to start on boot:
sudo systemctl enable mariadb
Secure the MariaDB installation:
sudo mysql_secure_installation
Verify MariaDB is running:
sudo systemctl status mariadb
Step 5: Verify the Installation of Apache, PHP, and MySQL
Now that Apache, PHP, and MySQL/MariaDB are installed, it’s important to verify that everything is working correctly.
Check Apache and PHP Integration
- Create a PHP info file to check if PHP is correctly integrated with Apache:
sudo nano /var/www/html/info.php
- Add the following PHP code:
<?php
phpinfo();
?>
Save and exit the file (Press
CTRL+X
, thenY
, thenEnter
).In your web browser, visit:
http://localhost/info.php
You should see a page displaying detailed PHP configuration information, confirming that Apache and PHP are working together.
Check MySQL/MariaDB Connection
To check if MySQL/MariaDB is working, log into the database:
sudo mysql -u root -p
Enter your root password when prompted, and if you see the MariaDB/MySQL prompt (MariaDB [(none)]>
), then the installation was successful.
Step 6: Configure Apache to Use PHP
If you haven’t already, ensure Apache is configured to handle PHP files.
For Debian/Ubuntu (APT)
Apache should automatically be configured to use PHP after installing the libapache2-mod-php
module. If it's not, you can enable the PHP module manually with:
sudo a2enmod php7.x # Replace 7.x with your PHP version
sudo systemctl restart apache2
For CentOS 8+ (DNF)
CentOS 8 will automatically configure Apache to work with PHP. If it's not working, you can ensure that PHP is set up with the following command:
sudo systemctl restart httpd
Step 7: Remove the PHP Info File (Optional)
After verifying PHP is working, it's a good idea to delete the info.php
file for security reasons:
sudo rm /var/www/html/info.php
Step 8: Additional Configuration (Optional)
You may want to configure Apache to serve multiple websites or adjust certain PHP settings.
Set Up Virtual Hosts (Optional)
If you want to serve multiple websites, create a configuration file for each site.
- Create a new configuration file for your site:
sudo nano /etc/httpd/conf.d/mywebsite.conf # CentOS
- Add the virtual host configuration:
<VirtualHost *:80>
ServerAdmin webmaster@mywebsite.com
DocumentRoot /var/www/mywebsite
ServerName mywebsite.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
- Create the directory for the site:
sudo mkdir /var/www/mywebsite
- Restart Apache to apply the changes:
sudo systemctl restart httpd # CentOS
Conclusion
You now have a fully functional LAMP stack (Apache, PHP, and MySQL/MariaDB) on your Linux system, ready to serve dynamic websites. Here's a recap of what we did:
Installed Apache to serve web content.
Installed PHP for dynamic content.
Installed MySQL/MariaDB for database management.
Configured Apache and PHP to work together.
Created virtual hosts for managing multiple sites (optional).
With Apache, PHP, and MySQL/MariaDB running, your server is ready for hosting web applications, whether it's for a simple website or a full-fledged content management system (CMS).