Hostwinds Tutorials

Search results for:


Table of Contents


Install Apache
Install MySQL
Install PHP
Configuration
Create Directory for Your Site
Create VirtualHost
Restart Apache
Upload Site Files

Install LAMP Stack on CentOS 7

Tags: CentOS Web Panel 

Install Apache
Install MySQL
Install PHP
Configuration
Create Directory for Your Site
Create VirtualHost
Restart Apache
Upload Site Files

LAMP stack is one of the most simple and barebones solutions you can have for a widely supported web stack with server-side code and database functionality.

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 installing 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 start on boot by running automatically:

systemctl enable httpd

Install MySQL

To install the latest release of MySQL (at the time of writing this article, this is version 8.0), 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 there 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 make your site accessible publicly using Apache, you need to create an Apache VirtualHost configuration.

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>
    Options +Indexes
  </Direcotry>
</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 tag is optional and 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, 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 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.

Written by Hostwinds Team  /  April 8, 2019