LAMP (Linux, Apache, MySQL, PHP) on CentOS
LAMP is a popular stack that includes Linux, Apache, MySQL, and PHP, which together form a complete web server environment. Each component has a specific role:
- Linux: The operating system
- Apache: The web server
- MySQL: The database management system
- PHP: The programming language for dynamic web content
This guide will walk you through installing and configuring LAMP on CentOS 6 step by step.
Step 1: Preparation
Switch to Root User
To perform administrative tasks, switch to the root user:
sudo su -
Create a New User
It’s recommended to create a separate user for managing website files:
useradd -m username
This command will create a home directory at /home/username
.
Set Up a Directory for Your Website
For organization, create a directory where your website files will be stored:
mkdir /home/username/my-first-domain.com
chown -R username:username /home/username/my-first-domain.com
Now, let’s move on to installing Apache.
Step 2: Installing and Configuring Apache
Install Apache
Use yum
to install Apache:
yum install httpd -y
Enable Apache to Start on Boot
chkconfig httpd on
Start Apache
service httpd start
If successful, you should see:Starting httpd: [OK]
Step 3: Configuring Apache
Install a Text Editor (Nano)
If you don’t have nano
, install it:
yum install nano -y
Modify Apache Configuration File
Open the main Apache configuration file:
nano /etc/httpd/conf/httpd.conf
Find this section and comment it out (add #
at the beginning of each line):
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
Then, add the following lines to allow access to your home directory:
<Directory /home/username/>
AllowOverride All
</Directory>
Save and exit the file (CTRL + X
, then Y
, then ENTER
).
Step 4: Adding a Website (Virtual Host)
Create a virtual host configuration file:
nano /etc/httpd/conf.d/my-first-domain.com.conf
Add the following content (replace with your details):
<VirtualHost Server-IP:80>
ServerName my-first-domain.com
ServerAlias www.my-first-domain.com
ServerAdmin mail@my-first-domain.com
DocumentRoot /home/username/my-first-domain.com
</VirtualHost>
- Server-IP: Your server’s IP address
- ServerName: The main domain
- ServerAlias: Additional names for the site
- DocumentRoot: The folder where site files are stored
Check for Errors and Restart Apache
Run the following command to check for syntax errors:
service httpd configtest
If everything is correct, you should see:Syntax OK
Restart Apache to apply the changes:
service httpd restart
Step 5: Testing Apache and PHP
Create a test PHP file in your website directory:
echo "<?php echo 'It works!'; ?>" > /home/username/my-first-domain.com/index.php
Set proper permissions:
chown username:username /home/username/my-first-domain.com/index.php
chmod 644 /home/username/my-first-domain.com/index.php
Now, open a browser and visit:
http://my-first-domain.com/index.php
If you see It works!
, Apache and PHP are working correctly.
Step 6: Installing MySQL
Install MySQL Server
yum install mysql-server -y
Enable MySQL to Start on Boot
chkconfig mysqld on
Start MySQL
service mysqld start
Step 7: Securing MySQL
Run the MySQL security script to set a root password and improve security:
/usr/bin/mysql_secure_installation
Follow the prompts:
- Set a root password (Yes)
- Remove anonymous users (Yes)
- Disallow root login remotely (Yes)
- Remove the test database (Yes)
Now, connect to MySQL using:
mysql -u root -p
Enter the root password you just created.
Step 8: Installing PHP and Modules
Install PHP
yum install php -y
Install MySQL Support for PHP
yum install php-mysql -y
Check Installed PHP Modules
php -m
Install Additional PHP Modules (if needed)
yum install php-common php-mbstring php-mcrypt php-devel php-xml php-gd -y
Step 9: Adding Remi Repository (Optional for Latest PHP Versions)
Enable the Remi Repository
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
sudo rpm -Uvh remi-release-6*.rpm
By default, Remi is disabled. To enable it permanently:
sudo nano /etc/yum.repos.d/remi.repo
Find the line:
enabled=0
Change it to:
enabled=1
Now, you can install PHP from Remi:
sudo yum --enablerepo=remi install php-xml -y
Final Step: Restart Apache
For changes to take effect, restart Apache one last time:
service httpd restart
Conclusion
You have successfully installed and configured LAMP (Linux, Apache, MySQL, PHP) on CentOS.
Now you can:
✅ Host websites and applications
✅ Manage databases with MySQL
✅ Use PHP to create dynamic web pages