Install LAMP Stack on CentOS 7

Install LAMP Stack on CentOS 7

One of the most simple and barebones solutions you can have for having a fully fledged and widely supported web stack with both server-side code and database functionality, is with the LAMP Stack.
 There are four primary components of the LAMP stack:
    Linux: the operating system environment
    Apache: webserver service to handle HTTP requests
    MySQL: the database engine
    PHP: scripting language that executes server-side
This guide goes over how to install the LAMP stack with a basic configuration on CentOS 7 and is intended for Hostwinds clients with a Cloud VPS or Dedicated Server.

Install Apache
To install Apache on CentOS 7, you will want to install the httpd package using the yum package manager:

yum install httpd

After installing Apache, you will want to make sure that it is started by running:

systemctl start httpd

You can then set Apache to automatically start on boot by running:

systemctl enable httpd

Install MySQL
To install the latest release of MySQL (at the time of writing this article, this is version 8.0), first we need to add the MySQL repo for yum to reference:

sudo yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

Then we can install MySQL:

yum install mysql-community-server

Next, start and enable MySQL at boot:

systemctl start mysqld
systemctl enable mysqld

Finally, with the installation, a temporary password was given to the root MySQL user. To get this password run:

grep 'temporary password' /var/log/mysqld.log

You should receive output like such:

2019-03-28T23:06:39.680264Z 1 [Note] A temporary password is generated for root@localhost: ************

Copy or write down the password and then run the following command to specify a new password:

mysql_secure_installation

You will be prompted to provide the old, temporary password followed by providing a new password for the root user.

Install PHP

To install the latest version of PHP (which at the time of writing this article is PHP 7.3), first you must install/enable the Remi and EPEL yum repositories:

yum install epel-release
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

Once enabled, then you can install PHP:

yum --enablerepo=remi-php73 install php

Or you can install the default version included in the base yum repos (PHP 5.4) just by running yum install php. However, as PHP 5.4 has reached its End of Life, it is typically not recommended to use this version.

Configuration    Create Directory for Your Site

We will need a location to store the files for the site. The default location for sites is /var/www/, so let’s create a subdirectory in their for our site:

mkdir /var/www/example.com

Then let’s create subdirectories specifically for our site’s public facing files, and a directory to contain logs for our site.

mkdir /var/www/example.com/public_html
mkdir /var/www/example.com/logs

Create VirtualHost

To now make your site accessible publicly using Apache, you need to create an Apache VirtualHost configuration for it.

First, navigate to the folder /etc/httpd/conf.d/ and create a new .conf file for your site (i.e. example.com.conf). Then add the following configuration details to the file, replacing ‘example.com’ with your domain.

<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /var/www/example.com/public_html
  ErrorLog /var/www/example.com/logs/error.log
  CustomLog /var/www/example.com/logs/requests.log combined
 
  # OPTIONAL
  # Enables directory listing when no index file is found
  <Directory /var/www/example.com/public_html>
    AllowOverride all
    Options +Indexes
  </Directory>
</VirtualHost>

ServerName and ServerAlias will specify what domains are set to use this configuration. In this example, we have the base domain and the www subdomain.

DocumentRoot specifies the folder location where the site’s files can be found. This should be the folder you created to store your site’s files in the previous step.

ErrorLog and CustomLog specify log files for any errors or requests to be logged in. These should go into the log directory that was created in the previous step.

The shown <Directory> tag is optional, and just enables listing directory contents, instead of giving a 403 error, when no index file is found. The path specified in the tag should match the DocumentRoot.

Restart Apache

To reload Apache with this new configuration in place, simply run:

apachectl restart

If that gives you any errors, double check your configuration file and folder paths to make sure the folders exist and there are no typos. You can also view the error report by running:

systemctl status httpd

Upload Site Files

Now that your LAMP stack is fully configured, all that is left to do is create/upload the files to your site into the document root folder that was specified in your configuration.

As a test, you can create a file called index.php  within  /var/www/example.com/public_html  and add the following code to it:

<?php
  phpinfo();
?>

Then when you navigate to your IP or domain (if your domain’s DNS is pointed to the server), you will be shown a PHP Info page detailing the PHP configuration on the server.

 

  • 3 Users Found This Useful
Was this answer helpful?

Related Articles

How can I configure a ip range in CentOS7?

You have to use a range file Edit /etc/sysconfig/network-scripts/ifcfg-enp0s3 and make...

Adding a Public IPv6 Address to a Linux Server (CentOS 7)

To configure an additional public IPv6 address in CentOS 7, proceed as follows:...

How To Install EPEL Repository On CentOS And RHEL 7

How to install EPEL for Centos 7 [root@localhost ~]# cat  /etc/centos-release CentOS Linux...

Common Commands (CentOS 7)

Basic Configuration Query or System Locale...