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.


1. System update and password change
2. Apache and PHP installation
3. Starting Apache
4. Install MySQL server
5. Add your website or virtual hosts setup



1. System update and password change

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:

uname -r
cat /etc/redhat-release






2. Apache and PHP installation

In order to install Apache server, run the command below:

yum install httpd

After it is completed, you should see this message:



Then we will install PHP with MySQL support.

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:

a. Installation of stantard CentOS6 PHP5.3 (no PHP version pre-installed)
b. Installation of another PHP version (no PHP version pre-installed)
c. Changing already installed PHP version

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.

Installation of the stantard CentOS6 PHP5.3 (no PHP version pre-installed)

yum install php php-mysql

Successful installation looks like in the screenshot below. After the installation we checked the PHP version installed using:

php -v





Installation of another PHP version (no PHP version pre-installed)

First, we need to install Webtatic Yum repository which is a CentOS/RHEL repository containing updated web-related packages and Extra Packages for Enterprise Linux (or EPEL):

rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm




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:

PHP v5.5: yum install php55w php55w-opcache php55w-mysql
PHP v5.6: yum install php56w php56w-opcache php56w-mysql
PHP v7.0: yum install php70w php70w-opcache php70w-mysql

Packages are outside default CentOS repositories, during the installation you’ll need to import a security key:



The example of successful installation. We've installed PHP5.6 here and then checked PHP version using php -v:





Changing an already installed PHP version

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.

First, we need to install Webtatic Yum repository which is a CentOS/RHEL repository containing updated web-related packages and Extra Packages for Enterprise Linux (or EPEL):

rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm




If you know what you are doing, you can upgrade PHP by running:

yum install yum-plugin-replace

The example of command below will upgrade the default CentOS6 PHP version PHP5.3 to PHP5.6. If you have another version installed or wish to upgrade to another PHP version, you need to modify it:

yum replace php-common --replace-with=php56w-common

where:
php-common – 5.3 native version
php54w-common – 5.4
php55w-common – 5.5
php56w-common – 5.6
php70w-common – 7.0

You can see some examples below:

From the default CentOS6 5.3 version to PHP7.0:

yum replace php-common --replace-with=php70w-common

From installed PHP 5.5 to PHP 5.6:

yum replace php55w-common --replace-with=php56w-common

From installed PHP7.0 to default CentOS6 PHP5.3:

yum replace php70w-common --replace-with=php-common

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:

yum install php-cli.x86_64 php-common.x86_64 php-dba.x86_64


3. Starting Apache

This time we need to edit the main Apache configuration file and finally start it. We will use nano text editor, but you can use any other of your choice.

The Apache main configuration file is /etc/httpd/conf/httpd.conf, so we open it using:

nano /etc/httpd/conf/httpd.conf

Use keyboard arrows to scroll down the file. The section we need is Main server configuration. Change two lines: ServerAdmin and ServerName.

ServerAdmin can be any email address you wish to send server notifications to, ServerName should be the hostname you set with 80 http port.

We used ServerName server1.nctest.com:80



Start Apache server:

service httpd start

And enable autoload of the Apache server after the server startup:

chkconfig httpd on



In case the domain name is pointed to the server, you can check Apache start page. Otherwise, you can enter the server IP in a browser:




4. Install MySQL server

We use the same Yum installer:

yum install mysql-server

Once done, check the installed MySQL version and start MySQL service:

mysql -V
service mysqld start
chkconfig mysqld on






MySQL server has its own root user with a different password. So we need to set the root MySQL password:

/usr/bin/mysql_secure_installation

As we do not have any MySQL password set yet, you need to press Enter when it asks for the current root password and set a new one:



Answer y when it prompts you to:

Set root password? [Y/n]
New password: Enter
Re-enter new password: enter
Remove anonymous users? [Y/n]
Disallow root login remotely? [Y/n]
Remove test database and access to it? [Y/n]
Reload privilege tables now? [Y/n]

You should see Thanks for using MySQL! at the end of the installation process:



You can launch MySQL command line using this command:

mysql -u root -p

And enter MySQL password which you have just set:



Here are the most useful MySQL commands:

Create database:

CREATE DATABASE databasename;

databasename should be a database you wish to create. It's better not to use a space in the name. In this example, we are using 'wp_357' database and 'wp' user:



Create a user and grant it all permissions:

GRANT ALL ON databasename.* TO user@localhost IDENTIFIED BY 'password';

Change databasename, sqluser and password to your own. The command GRANT ALL assigns all privileges (read, delete, modify or add data) to that user for only this database:



Do not forget to save username and password. You will need them to connect your website to MySQL database.

The database you created can be listed using:

show databases;



Type quit to close MySQL console.



5. Add your website or virtual hosts setup

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:

cd /
mkdir -p /var/www/domain.com/public_html
sudo chown -R apache:apache /var/www/domain.com/public_html
chmod 755 /var/www


In the example below we are using the nctest.com domain name.

ps aux | egrep '(apache|httpd)' command was used to double-check the correct Apache user:



Next we need to modify the main Apache configuration file:

nano /etc/httpd/conf/httpd.conf

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_log
CustomLog – 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:

rm -f index.html

Then run these commands:

wget http://wordpress.org/latest.tar.gz
tar --strip-components=1 -xvf latest.tar.gz
rm -f latest.tar.gz


Create a default .htaccess file using:

nano .htaccess

And replace its content with these default WordPress rewrite rules:

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress


Set correct ownership for the website folder again as wget uploaded files with root permissions instead of Apache ones:

sudo chown -R apache:apache /var/www/domain.com/public_html

Then you can access your website at the link like http://nctest.com/wp-admin/setup-config.php and complete the installation by filling MySQL database details.

That’s it! You can build your websites now.

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.

Need help? We're always here for you.

× Close