How to install Linux, Apache, MariaDB, PHP stack (LAMP) on CentOS 7

A LAMP (Linux, Apache, MySQL, PHP) stack is commonly used to prepare servers for hosting web content. In this guide we will show on how to install it on CentOS 7 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 control panel installed.

One of the main distinctions of  CentOS 7 version is systemd which causes different ways to start various services. Systemd was developed to replace the init system in use since the days of proprietary Unix, and it allows, for example, more efficient loading of services during the boot process.

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

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



1. System update and password change

First we need to clean up yum, then update the operation system to the latest version using:

yum clean all
yum update


Yum is a CentOS package installer, each time we use it, we see this window:



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

Force a SELinux filesystem relabel at next boot:

restorecon -r /



You may 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. 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 VPS or IMPI for dedicated server.

Password change is done using passwd command. Hostname can be checked using hostname command:



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

reboot



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

uname -r
cat /etc/redhat-release




You may find a rare minor bug after reboot which does not affect further stability:



Just start the service again running:

systemctl start systemd-journald.service

Status of each systemd process can be checked similarly to CentOS6 init but using systemctl:

systemctl status




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 7 repositories include PHP 5.4.16 version. However, this php version is already considered to be outdated, up to date version is 5.6. We suggest checking scripts you are going to install to consider what php version you need. Also, installation of a 'fresh' php version is different from changing 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 CentOS 7 PHP5.4 (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 php version.

Installation of standard CentOS 7 PHP5.4 (no php version pre-installed)

yum install php php-mysql

Successful installation looks like in the screenshot below. After an installation we checked 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://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm




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

5.5-7.0 versions include bundled Zend Opcache module, so it will be reasonable to install it too. Depends on the 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 by confirming yes.

The example of successful installation is shown below. We installed PHP5.6 here and then checked php version using php -v:



Changing 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 the 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://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.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 default CentOS7 PHP version PHP5.4 to PHP5.6. If you have another version installed or want to upgrade to another PHP version you need to modify it:

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

where:

php-common – 5.4 native version
php55w-common – 5.5
php56w-common – 5.6
php70w-common – 7.0

You can see some examples below.

From default CentOS6 5.4 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.4:

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 installed php version with php -v command.

Additional PHP modules installation

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

yum search php

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

yum search php56w

Yum will show all modules starting from php:



To install a module you need enter:

yum install

where is full name of module to install:



In order 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 main Apache configuration file and finally start it. We will use nano text editor, but, you can use any other of your choice.

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

Use keyboard errors to scroll down the file. 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

Make sure that there is no comment (# sign) at the beginning of these two lines:



Start Apache server:

systemctl start httpd

And enable autoload of Apache server after the server startup:

systemctl enable httpd



Configure firewall daemon to allow Apache 80 and 443 ports and restart it:

firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --reload




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




4. Install MariaDB server

We use the same Yum installer. On CentOS7 it is reasonable to install native MariaDB package instead of standard MySQL. The goal for MariaDB is to be a drop-in replacement for MySQL – with more features and better performance:

yum install mariadb-server

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

systemctl start mariadb
systemctl enable mariadb




mysql -V



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

/usr/bin/mysql_secure_installation

As we do not have any MySQL password set yet, you need to hit Enter when it asks for the current root password, and set a new one. MariaDB has its own root user as well:



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]

In the end it you should see Thanks for using MariaDB!.



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. You better not use space in the name. In this example we use wp database and wpuser user:



Create user and grant it all permissions: GRANT ALL ON databasename.* TO user@localhost IDENTIFIED BY 'password';

Change databasename, sqluser and password to your own ones. 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 it to connect your website to MySQL database.
Database you created can be listed using:

show databases;



Type quit to close MariaDB console.


5. Add your website or virtual hosts setup

Basically, we need to create a folder to upload a specific website 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 used nctest.com.

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



Next we need to modify virtual hosts and edit main Apache configuration file.

Create two directories where our virtual hosts will be stored in:

mkdir /etc/httpd/sites-available
mkdir /etc/httpd/sites-enabled


And include sites-enabled in httpd.conf file. Use the same nano editor:

nano /etc/httpd/conf/httpd.conf

Apache configuration file is huge, however, you can hit Ctrl+V to jump to the next page, Ctrl+W combination allows searching.

It is a great idea to enable .htaccess file. To do that you need to find these lines at the beginning of the file and change nano to All:
 
<Directory />
 AllowOverride All  
 Require all denied
</Directory>



And the end of the file add:

IncludeOptional sites-enabled/*.conf



Now hit Ctrl+O on your keyboard to save, then Ctrl+X to exit Nano editor screen.

After that create a configuration file for your domain name. Again, replace 'domain.com' with the actual domain name of yours:

nano /etc/httpd/sites-available/domain.com.conf

<VirtualHost *:80>
ServerAdmin admin@domain.com
DocumentRoot /var/www/domain.com/public_html
ServerName domain.com
ErrorLog /var/www/domain.com/public_html/error_log
CustomLog /var/www/domain.com/public_html/access_log common

</VirtualHost>

In our example it is /var/www/nctest.com/public_html:



Again, hit Ctrl+O and Ctrl+X.

The last configuration step is enabling our new virtual host by creating a symbolic link:

ln -s /etc/httpd/sites-available/domain.com.conf /etc/httpd/sites-enabled/domain.com.conf



'domain.com' should be replaced to the domain name you create a virtual host for.

In case you need to add another virtual host, you need to create a virtual host again along with another symbolic link.

And just restart Apache once more:

apachectl restart

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

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

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

<html>
<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 also test some PHP template like WordPress. Change directory to your website directory which was set previously:

cd /var/www/domain.com/public_html


Make sure that you removed index.html file in case you created a test page previously. 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 default .htaccess file using: nano .htaccess

And replace its content with these default WordPress rewrite rules:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# 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 following 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