Back

How to install LAMP on CentOS 6

Introduction

LAMP is an acronym for Linux, Apache, MySQL, and PHP. All of these components together allow obtaining a full web-server. Each of them is responsible for a certain component of web-hosting.

Preparation

All server settings are to be carried out with root privileges. To do this, run the command:

sudo su -

In the beginning, create a user under which we will work with site files. To do this, run the command:

useradd -m username

This command will also create a home directory in /home/username. Website catalogues of the server will be stored inside the user's home directory.

For your convenience, the root directory of each site will be called by its name. Create a root directory for our future site. After that, make our user the owner of the directory.

mkdir /home/username/my-first-domain.com
chown -R username:username /home/username/my-first-domain.com

Installing Apache

yum install httpd

Next, we set up the automatic loading at startup for the Apache server using chkconfig:

chkconfig httpd on

run Apache:

service httpd start
Starting httpd: [OK]

Configuring Apache

Basic settings

At this stage, it is necessary to make some changes in the Apache configuration files.

To do this, you will need a text editor such as nano. To install it, run the command

yum install nano

Let's start with the main Apache configuration file:

nano /etc/httpd/conf/httpd.conf

Find the following construction and leave a comment:

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

Insert the following construction below:

<Directory /home/username/>
AllowOverride All
</Directory>

This will allow the server to work with the catalogue /home/username/ and support .htaccess.

Adding a website

The most popular method is creating a virtual host file in the conf.d/ directory. The advantage is that the catalogue already exists, and the file httpd.conf contains the following line:

Include conf.d/*.conf

These instructions include all files with the ".conf" extension in the conf.d / directory. It means that any files in this directory that have the ".conf" extension will be treated as Apache configuration files. The files are processed in alphabetical order.

Run the command:

nano /etc/httpd/conf.d/my-first-domain.com.conf

Example of a filled virtual host file my-first-domain.com.conf:

<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 – is the IP address of your server.

  • ServerName - the name of the virtual host must be set to the fully qualified domain name (FQDN), in our case my-first-domain.com determines which domain name serves this virtual host.

  • ServerAdmin - contact e-mail address of the domain administrator is included in the reports on the web server errors. It is recommended to have a separate mailbox for this purpose.

  • ServerAlias - alias hostname, the value www.my-first-domain.com is obligatory, so that your site would work with both www, and without.

  • DocumentRoot - the root folder of the virtual host. Specify the directory of the site placement / home / username / my-first-domain.com

After connecting a website, make sure that no mistakes have been made, and configuration files do not contain errors:

service httpd configtest
Syntax OK

After that, restart Apache to apply changes:

service httpd restart

Checking performance 

Put a PHP script in the root directory:

<?php
echo "It works !" ;
?>

Set the correct script rights with help of chmod chown

Open your website in a browser. If your domain is not directed to the IP address of the server, you can always add a line:

Server-IP my-first-domain.com

- Server-IP is the IP address of your server.

To the file hosts on your computer that you use for settings. In Windows, it is usually located in C:\Windows\System32\drivers\etc\, and in nix systems it’s located in /etc/

MySQL Installation

When installing the MySQL database server, according to the specified dependencies perl-DBI, perl-DBD-MySQL, mysql, and mysql-server will be installed as well.

Installation:

yum install mysql-server

Set MySQL to run automatically when the system launches:

chkconfig mysqld on

Run MySQL:

service mysqld start

Setting up MySQL

By default, after installation, we can connect to our database server under the root without entering the password. Therefore, we assign a password and make a few more settings by running the script:

/usr/bin/mysql_secure_installation

This script (if you answer yes to all the questions) will set a new root password, remove anonymous users, prohibit connection from remote machines as root, and remove the test database. Please note that the root MySQL password should be stored in a safe place.

You can connect using a MySQL client:

mysql -u root -p

PHP Installation

yum install php

Installing a MySQL module for PHP:

yum install php-mysql

Basic installation includes a set of standard PHP modules. To see the list of available modules, run the command:

php -m

Installing extra modules (if you need them):

yum install php-common php-mbstring php-mcrypt php-devel php-xml php-gd

Adding a remi repository 

If the repositories that are specified in the system are missing some packages, add a remi repository:

wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
sudo rpm -Uvh remi-release-6*.rpm

Remi repository will be turned off by default, but the packages can be installed using '--enablerepo=remi' for example:

sudo yum --enablerepo=remi install php-xml

It can also be activated in the file /etc/yum.repos.d/remi.repo

sudo nano /etc/yum.repos.d/remi.repo

Change enabled=0 to enabled=1

name=Les RPM de remi pour Enterprise Linux $releasever - $basearch
#baseurl=http://rpms.famillecollet.com/enterprise/$releasever/remi/$basearch/
mirrorlist=http://rpms.famillecollet.com/enterprise/$releasever/remi/mirror
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
failovermethod=priority

Suggested Articles

  • Linux administration

    How to install Munin on Ubuntu and CentOS

  • Linux administration

    How to install Zabbix on CentOS 6.8 and Ubuntu 12.04