Reference Server
In this tutorial we will learn to install LAMP stack (Linux, Apache, MySQL and Php) on a CentOS server.
A LAMP stack is a collection of open source software that allows us to make our server host dynamic websites. A LAMP stack actually means that we have a Linux operating system and Apache web server. MySQL database is used to save user data and Php is used to create dynamic web pages and process the dynamic content.
When you get the server, for example Amazon AWS or Linode, you will get options in the admin dashboard that will allows you to pick a version of Linux OS and install it on the server.
Assuming that you have a new server and you have installed a version of Linux operating system and you have the root access. So, open the terminal and login to the server as the root user.
Lets start the installation process...
Note! we will be using sudo in our commands. This will give us root privileges.
Its a best practice to first clean up and update yum and then perform installation.
Clean up yum by typing the following in terminal.
# sudo yum clean all
Now perform the update
# sudo yum -y update
Once the update is done we are ready to install Apache.
Apache is one of the most widely used web server and to install it we need to run the following command.
# sudo yum -y install httpd
Once we have Apache installed we need to start the Apache web server. For this run the following command.
# sudo systemctl start httpd
Note!
To make sure that our Apache web server starts at bootup we have to running the following command.
# sudo systemctl enable httpd
To check the status of the Apache web server run the following command.
# sudo systemctl status httpd
In case you want to stop the Apache web server type in the following command.
# sudo systemctl stop httpd
And to restart Apache run the following command.
# sudo systemctl restart httpd
Now, its time to install MySQL database on the server that will allow to save data.
To install MySQL run the following command.
# sudo yum install mysql-server
After installing MySQL we need to start it. For this we use the following command.
# sudo systemctl start mysqld
Or, you can use
# sudo /sbin/service mysqld start
Setting up MySQL by running the following command. This will help you to create a login password for MySQL.
# sudo mysql_secure_installation
Or, you can use the following
# sudo /usr/bin/mysql_secure_installation
The prompt will ask you to enter the current root password. Since we installed MySQL a moment ago so, there will be no root password. So, you can leave it by pressing Enter.
Now, you will be asked whether you want to set a password for the root user. It is always recommended to set a password. So, enter Y and follow the given instructions:
New password: password
Re-enter new password: password
Password updated successfully!
Reloading privilege tables..
... Success!
Note!
To make MySQL start at boot use the following command.
# sudo systemctl enable mysqld
To stop MySQL use the following command.
# sudo systemctl stop mysqld
To restart MySQL use the following command.
# sudo systemctl restart mysqld
Php is one of the most popular server side programming language to create dynamic websites. In this step we will be installing php.
Install php using the following command.
# sudo yum install php php-pear
Since we are using MySQL database so, we will need MySQL support for php. Using the following command for that.
# sudo yum install php-mysql
Now reload Apache web server.
# sudo systemctl reload httpd
Checking our system configuration for php
Create an info.php file inside /var/www/html/ directory and write the following php code.
Change directory to /var/www/html/
# cd /var/www/html
Create info.php file using vi command
# vi info.php
Press i key to enter into INSERT mode and write the following code in info.php file.
<?php
phpinfo();
?>
Now press the ESC key to exit INSERT mode of vi. Now save the file by typing the following :wq
Now to check whether our server can correctly display php generated content open a web browser like Google Chrome or Mozilla Firefox and type in your server IP address followed by the info.php file.
Example, http://your_server_IP_address/info.php
Our if you have a domain name then open info.php file accordingly.
Example, http://www.example.com/info.php
If php was configured properly then you will see a similar result.
This will tell you about your server. Once you have done this testing remove the info.php file by using the following command.
# sudo rm /var/www/html/info.php
It is important to remove the info.php file as it exposes your server information to unauthorized users.
And this brings us to the end. You have successfully installed LAMP stack on CentOS server. Have fun coding!
ADVERTISEMENT