Apache (HTTPD) and PHP remain one of the most popular combinations for web development. This guide walks you through setting up a fully functional web server with Apache and PHP on a Linux system, from preparation to security optimization.
Before beginning the configuration, ensure your system is up to date. Use the following commands to update Ubuntu or CentOS:
sudo apt update && sudo apt upgrade # Ubuntu
sudo yum update # CentOS
On Ubuntu, use this command to install Apache:
sudo apt install apache2
For CentOS, run:
sudo yum install httpd
After installation, start the Apache service and enable it to launch on boot:
sudo systemctl start httpd
sudo systemctl enable httpd
To enable PHP support in Apache, edit the main configuration file. For CentOS:
/etc/httpd/conf/httpd.conf
For Ubuntu:
/etc/apache2/apache2.conf
Make sure to add the following line to support PHP file handling:
AddType application/x-httpd-php .php
On Ubuntu, install PHP and necessary modules using:
sudo apt install php libapache2-mod-php php-mysql
On CentOS:
sudo yum install php php-mysql
To verify your PHP setup, create a test file in Apache's default root directory:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Then open http://your_server_ip/info.php in your browser. If you see the PHP configuration page, your setup was successful.
After installation, enhance your system's security and performance with these practices:
By following these steps, you have successfully set up Apache and PHP on a Linux server. This configuration is suitable for most web hosting and development needs. For long-term stability and security, keep your system updated and follow best practices for optimization.