How to install Linux, Apache, MySQL, PHP stack (LAMP) on CentOS 6

A LAMP (Linux, Apache, MySQL, PHP) stack is commonly used to prepare servers for hosting web content. In this guide we will show how to install it on the CentOS 6 version. Unfortunately, we do not provide technical assistance with VPS/dedicated servers without cPanel. However, this detailed guide will be handy if you are planning to manage the server without cPanel installed.

All commands should be performed under root access on a 'clean' operating system. VPS packages come with pre-configured LAMP image by default, and you need to bypass 2-4 steps and continue with adding the website. If you need to upgrade the default PHP version, this part will be useful too – Changing already installed PHP version. In this case, it is better to upgrade a 'clean' installation passing all steps.

Update the operating system to the latest version using the yum update command.

Yum is a CentOS package installer, each time we use it, we see this window. You just need to confirm a download typing y:

If you see Complete! at the end running yum update, everything is okay:

You may also want to change the default root password provided in the Hosting welcome email. Make sure that the password is secure enough, and you keep it in a safe place. The hostname should be some subdomain you do not plan to use for the website. By default, it is set as server1.yourdomain.tld, but you can change it anytime using
SolusVM panel for a VPS or IMPI for a dedicated server.

The password change is done using the passwd command.

Hostname can be checked using the hostname command:

In this example, we are planning to build the http://nctest.com website and our hostname is server1.nctest.com.

Reboot the server:

reboot -h now

After the server is rebooted, we can check the upgraded kernel and CentOS version:

At the moment, default CentOS 6 repositories include the PHP 5.3.3 version. However, this PHP version is already considered to be outdated, the up-to-date version is 5.6. We suggest checking scripts you are going to install to consider what PHP version you need. Also, the installation of a 'fresh' PHP version is different from changing an existing one.

There are three possible options depending on your needs and installed software. You need to follow one of these steps:

We recommend double-checking whether PHP is installed or not by using:

php –v

If there is no PHP support on the server, you should see command not found, so you can use install PHP from the scratch ("no PHP version pre-installed" options). Otherwise, use the last option to change the current PHP version.

Now, when checking with the command yum search php, we find that we've installed new PHP packages like php54w, php55w, php56w and even new php70w.

For example, to install PHP5.4 instead of default 5.3 on CentOS6 we run:

yum install php54w php54w-mysql

The same for other PHP versions which will be relevant to both CentOS versions. 5.5-7.0 versions include bundled Zend Opcache module, so it will be reasonable to install it too. Depending on your PHP version run:

Note that it is quite risky to perform such an upgrade on the existing production server. It is much safer to try how it works on some test server or perform a PHP installation from scratch on a fresh install instead.

It will likely give you a message WARNING: Unable to resolve all providers … It is fine, and you can continue by typing y. You will be given a chance to see what packages will be installed and removed before you will be asked to confirm the changes:

Again, you can check the installed PHP version using the php -v command.

Additonal PHP modules installation

If you find that some required modules missing, you can list available modules using:

yum search php

In case you have other than 5.3 PHP version, it will be reasonable to exclude modules for another version by changing PHP to PHPXX depending on what version you use. For example, for PHP 5.6 you need:

yum search php56w

Yum will show all modules starting from PHP:

To install a module you need run:

yum install

where there is a full name of module to install:

To install several modules at once, you need to separate the name of each module with a space like:

Basically, we need to create a folder to upload a specific website's files and tell Apache where specific host files are located. Replace 'domain.com' in each command listed below with your actual domain name.

Switch to the root directory of the server and create a website folder. Once done, we need to change ownership permissions to the Apache user (each service has its own user) and set appropriate www folder permissions of 755:

It is also a great idea to enable the .htaccess file. To do that, you need to find these lines at the beginning of the file:

<Directory />
Options FollowSymLinks
AllowOverride All
</ Directory>

And set AllowOverride to All:

The Apache configuration file is huge, next we need to modify only the part called VirtualHosts (end of the file).

You can hit Ctrll+V to jump to the next page, Ctrl+W combination allows searching.

Scroll down until you see these lines:

First, you need to uncomment #NameVirtualHost *:80 part marked in the screenshot, just remove # sign.

We see the example on how virtual hosts file should look commented. Add the same lines, but without the comment symbol (#) at the end of the file and modify them with your details.

<VirtualHost *:80> means that any IP address going through port 80 will be a virtual host. In case your server has more that one IPs, you can replace the * symbol with the server IP you wish to set for this website.

ServerAdmin – sets the contact address that the server includes in any error messages it returns.
Document root – the folder you create to upload files for your website. Below we create /var/www/nctest.com/public_html
Servername – your website hostname, in our example it is nctest.com
ErrorLog – the directive sets the name of the file to which the server will log any errors it encounters /var/www/nctest.com/error_logCustomLog – the directive is used to log requests to the server. It is used with ‘common’ argument. /var/www/nctest.com/access_log common

In our example the file looks like:

Now hit Ctrl+O on your keyboard to save followed by Ctrl+X and Enter to exit Nano editor screen.

In case you need to set another website, the process is the same: we create another folder for it and add another part to httpd.conf:

Finally, stop the Apache service and all its processes and start it again:

apachectl -k stop
service httpd start

That is all! You can place some index file to the folder you created for the website and check the page in the browser:

nano /var/www/domain.com/public_html/index.html

And put some html code like this code for testing purposes:

<head>
<title>My first website on CentOS</title>
</head>
<body>
<h1>You have set up your first Virtual Host</h1>
<p>Test page for domain.com</p>
</body>
</html>

We can test some PHP template like WordPress. Change the directory to your website directory which was set previously:

cd /var/www/nctest.com/public_html

Make sure that you deleted the index.html file in case you created a test page previously. The file can be removed by using:

Comments

We welcome your comments, questions, corrections and additional information relating to this article. Your comments may take some time to appear. Please be aware that off-topic comments will be deleted.

If you need specific help with your account, feel free to contact our Support Team. Thank you.