How to Install LAMP on CentOS 7 / RHEL 7

The LAMP stack, an acronym for Linux, Apache, MySQL/MariaDB, and PHP, is a popular open-source software group that allows you to run a web server and host dynamic websites.

In this guide, we will walk you through the process of installing the LAMP stack on CentOS 7, RHEL 7, or Oracle Linux 7. This tutorial is designed to be simple and easy to follow, even for beginners.

By following these steps, you will be able to set up a web server and database server on your Linux operating system.

Step-by-Step Guide to Install LAMP on CentOS 7 / RHEL 7 / Oracle Linux 7

Step 1: Install Apache Web Server, MariaDB Database, and PHP Packages

To install the web server, database, and PHP packages, you need to run the following command:

sudo yum install mariadb mariadb-server httpd php php-mysql php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap -y

Step 2: Start Apache Web Server and MariaDB Database

After the installation, start the Apache web server and MariaDB database and set them to auto-start at boot with the following commands:

sudo systemctl start httpd.service
sudo systemctl enable httpd.service
sudo systemctl start mariadb
sudo systemctl enable mariadb.service

Step 3: Configure MariaDB Settings

To secure your MariaDB installation, run the following command and follow the prompts:

sudo mysql_secure_installation

This script will guide you through setting the root password, removing anonymous users, disallowing root login remotely, removing the test database, and reloading the privilege tables.

[root@centos7 ~]# sudo mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Step 4: Access Your Server

You can now access your server using your IP address in the format http://IPaddress. Before proceeding to configure the virtual host, follow the instructions in the welcome.conf file:

vi /etc/httpd/conf.d/welcome.conf

Step 5: Configure Apache Virtual Host

To configure the Apache Virtual Host, follow these steps:

See also  How to Securing MySQL Database Server on CentOS 5.8

a. Create the DocumentRoot directory for your new domain:

mkdir -p /var/www/html/yourdomain

b. Create a vhost.conf file to place the Name-based virtual host. For example, if the default domain is centos7.yourdomain.local and you want to add a new virtual host www.yourdomain.local, which points at the same IP address, you would do the following:

vi /etc/httpd/conf.d/vhost.conf

Add the following lines to the file. Please note that “NameVirtualHost *:80” is no longer valid in Apache 2.4.x:

<VirtualHost *:80>
 DocumentRoot /var/www/html
 ServerName centos7.yourdomain.local
</VirtualHost>

# for virtual domain
<VirtualHost *:80>
 DocumentRoot /var/www/html/yourdomain
 ServerName www.yourdomain.local
 ServerAdmin webmaster@yourdomain.local
 ErrorLog logs/www.yourdomain.local-error_log
 CustomLog logs/www.yourdomain.local-access_log combined
</VirtualHost>

Step 6: Debug Apache Configuration

To debug your Apache configuration, use the following command:

httpd -S

This will display the VirtualHost configuration, ServerRoot, Main DocumentRoot, Main ErrorLog, and other server details.

VirtualHost configuration:
default server centos7.webhostinggeeks.local (/etc/httpd/conf.d/vhost.conf:2)
port 80 namevhost centos7.webhostinggeeks.local (/etc/httpd/conf.d/vhost.conf:2)
port 80 namevhost centos7.webhostinggeeks.local (/etc/httpd/conf.d/vhost.conf:2)
port 80 namevhost www.webhostinggeeks.local (/etc/httpd/conf.d/vhost.conf:7)
port 80 namevhost www.webhostinggeeks.local (/etc/httpd/conf.d/vhost.conf:7)
ServerRoot: "/etc/httpd"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/etc/httpd/logs/error_log"
Mutex proxy: using_defaults
Mutex authn-socache: using_defaults
Mutex default: dir="/run/httpd/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex authdigest-opaque: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex rewrite-map: using_defaults
Mutex authdigest-client: using_defaults
PidFile: "/run/httpd/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="apache" id=48
Group: name="apache" id=48

Step 7: Get More Information About Apache

To get more information about your Apache installation, use the following command:

httpd -V

This will display the server version, build date, module magic number, server loaded, compiled using, architecture, server MPM, and other server details.

Server version: Apache/2.4.6 (CentOS)
Server built:   Jan 12 2015 13:22:31
Server's Module Magic Number: 20120211:23
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/httpd"
 -D SUEXEC_BIN="/usr/sbin/suexec"
 -D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"

Step 8: Check PHP Version

Finally, you can check your PHP version with the following command:

php -v

This will display the PHP version, build date, copyright information, and Zend Engine version.

PHP 5.4.16 (cli) (built: Oct 31 2014 12:59:36)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

Commands Mentioned

  • sudo yum install mariadb mariadb-server httpd php php-mysql php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap -y – Installs Apache web server, MariaDB database, and PHP packages
  • sudo systemctl start httpd.service – Starts Apache web server
  • sudo systemctl enable httpd.service – Sets Apache web server to auto-start at boot
  • sudo systemctl start mariadb – Starts MariaDB database
  • sudo systemctl enable mariadb.service – Sets MariaDB database to auto-start at boot
  • sudo mysql_secure_installation – Runs the script to secure MariaDB installation
  • vi /etc/httpd/conf.d/welcome.conf – Opens the welcome.conf file for editing
  • mkdir -p /var/www/html/yourdomain – Creates the DocumentRoot directory for your new domain
  • vi /etc/httpd/conf.d/vhost.conf – Opens the vhost.conf file for editing
  • httpd -S – Debugs Apache configuration
  • httpd -V – Displays more information about Apache
  • php -v – Checks PHP version
See also  How to Install Apache httpd on Linux Fedora 16 Server

Conclusion

In this guide, we have walked you through the process of installing the LAMP stack on CentOS 7, RHEL 7, or Oracle Linux 7. This process involves installing the Apache web server, MariaDB database, and PHP packages, starting and enabling the services, securing your MariaDB installation, and configuring your Apache Virtual Host. By following these steps, you can set up a web server and database server on your Linux operating system and host dynamic websites and web applications.

Remember, it’s crucial to secure your MariaDB installation to protect your data from unauthorized access. Also, configuring your Apache Virtual Host correctly is essential for hosting multiple websites on a single server.

See also  How to Setup HAProxy as Load Balancer for Apache on Ubuntu

We hope this guide has provided you with the essential guidance and insights needed to install the LAMP stack on CentOS 7, RHEL 7, or Oracle Linux 7. For more in-depth information on web servers and hosting, you can refer to our articles on the Apache, LiteSpeed, and Nginx web servers, as well as the different types of web hosting such as dedicated server, VPS server, cloud hosting, and shared hosting.

Remember, the journey to becoming a proficient webmaster or website administrator is a continuous learning process. Keep exploring, learning, and implementing.

FAQ Section

  1. What is the LAMP stack?

    The LAMP stack is an acronym for a set of open-source software that is typically installed together to enable a server to host dynamic websites and web applications. It stands for Linux (operating system), Apache (web server), MySQL or MariaDB (database server), and PHP (programming language).

  2. Why is it important to secure MariaDB installation?

    Securing your MariaDB installation is crucial to protect your data from unauthorized access. The secure installation script helps you enforce a password for the root user, remove anonymous users, disallow root login remotely, and remove the test database, making your database server more secure.

  3. What is the purpose of the DocumentRoot directory in Apache?

    The DocumentRoot directory is the directory out of which Apache will serve your domain’s files. When a visitor requests a file from your website, Apache will append the request to the path of your DocumentRoot to find the file.

  4. What is a virtual host in Apache?

    A virtual host is a feature of Apache that allows a single server to host multiple websites. You can have different settings for each site and they can have their own domain name and IP address, or share the same IP address while having different domain names.

  5. How can I check the PHP version installed on my server?

    You can check the PHP version installed on your server by running the command ‘php -v’ in the terminal. This will display the PHP version, build date, copyright information, and Zend Engine version.

Comments