Installing Apache, PHP & MySQL on Linux
In this tutorial we will walk through how to install Apache, PHP, and MySQL on a Linux system, covering all the relevant configuration settings. This tutorial is aimed at developers who want to set up a local development environment on Linux.
Requirements
Before starting, make sure you have root (administrator) access to your system and that your internet connection is working to download the necessary packages. This tutorial uses Red Hat Linux as an example, but the steps are similar for most other distributions.
Installing Apache
The first step is to install the Apache web server. Open a Terminal and enter the following commands:
# Check if Apache is already installed rpm -qa | grep httpd # Install Apache rpm -i httpd-2.0.xx.rpm # Start the server service httpd start # Verify the server is running service httpd status
After starting the server, open your browser and go to http://localhost/ — if you see the Apache test page, the installation was successful.
Installing PHP
Now install PHP and link it to Apache:
# Install PHP rpm -i php-4.3.xx.rpm rpm -i php-mysql-4.3.xx.rpm # Restart Apache after installing PHP service httpd restart
To confirm PHP was installed correctly, create a file named test.php in /var/www/html/ with this content:
<?php phpinfo(); ?>
Then navigate to http://localhost/test.php — you should see the full PHP information page.
Installing MySQL
The final step is to install the MySQL database server:
# Install MySQL rpm -i mysql-4.0.xx.rpm rpm -i mysql-server-4.0.xx.rpm # Start MySQL service mysqld start # Secure the installation and set an admin password mysql_secure_installation # Log in to MySQL mysql -u root -p
Testing the PHP-to-MySQL Connection
Now you can test the connection between PHP and MySQL with this simple code:
<?php $conn = mysql_connect("localhost", "root", "your_password"); if (!$conn) { die("Connection failed: " . mysql_error()); } echo "Connected successfully!"; mysql_close($conn); ?>
If you see "Connected successfully!" then your full development environment is working correctly. Congratulations — you are now ready to develop PHP applications on Linux!