Installing a LAMP Stack (PHP, MySQL, Apache) on CentOS 7
- Ubuntu 22.04
- Ubuntu 20.04
- Ubuntu 18.04
- Debian 8
- Debian 11
- Debian 10
- CentOS 8
- Arch Linux
- Deprecated guides:
- Ubuntu 9.10
- Ubuntu 9.04
- Ubuntu 8.04
- Ubuntu 16.04
- Ubuntu 14.04
- Ubuntu 12.04
- Ubuntu 11.10
- Ubuntu 11.04
- Ubuntu 10.10
- Gentoo
- Fedora 20
- Fedora 19
- Fedora 15
- Fedora 14
- Fedora 13
- Fedora 12
- Fedora 11
- Debian 7
- Debian 6
- Debian 5
- CentOS 6
- CentOS 5
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
A LAMP stack is a particular bundle of software packages commonly used for hosting web content. The bundle consists of Linux, Apache, MariaDB, and PHP (LAMP). This guide shows you how to install a LAMP stack on a CentOS 7 Linode.
Before You Begin
Ensure that you have followed the Getting Started and Securing Your Server guides. Ensure that the Linode’s hostname is set.
Check your Linode’s hostname. The first command should show your short hostname and the second should show your fully qualified domain name (FQDN).
hostname hostname -f
Update your system:
sudo yum update
Note This guide is written for a non-root user. Commands that require elevated privileges are prefixed withsudo
. If you’re not familiar with thesudo
command, you can check our Users and Groups guide.
Apache
Install and Configure
Install Apache using CentOS’s package manager.
Install Apache 2.4:
sudo yum install httpd
Enable Apache to start at boot and start the Apache service:
sudo systemctl enable httpd.service sudo systemctl start httpd.service
Create a
httpd-mpm.conf
file and add the code in the example to turn off KeepAlive and adjust the resource use settings. The settings shown below are a good starting point for a Linode 2GB:Note As a best practice, you should create a backup of your Apache configuration file, before making any configuration changes to your Apache installation. To make a backup in your home directory:
cp /etc/httpd/conf/httpd.conf ~/httpd.conf.backup
- File: /etc/httpd/conf.modules.d/httpd-mpm.conf
1 2 3 4 5 6 7 8 9
KeepAlive Off <IfModule prefork.c> StartServers 4 MinSpareServers 20 MaxSpareServers 40 MaxClients 200 MaxRequestsPerChild 4500 </IfModule>
Configure Name-based Virtual Hosts
There are different ways to set up virtual hosts; however, the method below is recommended.
Create the directories to store your site files and logs. Replace
example.com
with your own site’s domain name.sudo mkdir -p /var/www/html/example.com/{public_html,logs}
Create the directories to store your site’s virtual hosts files:
sudo mkdir -p /etc/httpd/sites-available /etc/httpd/sites-enabled
Edit Apache’s configuration file to let it know to look for virtual host files in the
/etc/httpd/sites-enabled
directory. Add the example line to the bottom of yourhttpd.conf
file:- File: /etc/httpd/conf/httpd.conf
1
IncludeOptional sites-enabled/*.conf
Navigate to your
/var/www/html/example.com
directory if you are not already there:cd /var/www/html/example.com
Using your preferred text editor create a virtual hosts file. Copy the basic settings in the example below and paste them into the file. Replace all instances of
example.com
with your domain name:- File: /etc/httpd/sites-available/example.com.conf
1 2 3 4 5 6 7 8 9 10
<Directory /var/www/html/example.com/public_html> Require all granted </Directory> <VirtualHost *:80> ServerName example.com ServerAdmin webmaster@localhost DocumentRoot /var/www/html/example.com/public_html ErrorLog /var/www/html/example.com/logs/error.log CustomLog /var/www/html/example.com/logs/access.log combined </VirtualHost>
Create a symbolic link from your virtual hosts file in the
sites-available
directory to thesites-enabled
directory. Replaceexample.com.conf
with the name of your own virtual hosts file.sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
Reload to apply your new configuration:
sudo systemctl reload httpd.service
Note If you receive an error when trying to reload yourhttpd
service, follow the steps in the Configure SELinux to Allow HTTP section and then reattempt to reload the service.Additional domains can be added to the
example.com.conf
file as needed.Note ErrorLog
andCustomLog
entries are suggested for more fine-grained logging, but are not required. If they are defined (as shown above), thelogs
directories must be created before you restart Apache.
Configure SELinux to Allow HTTP
SELinux is enabled by default on CentOS 7 Linodes. Its default setting is to restrict Apache’s access to directories until explicit permissions are granted.
Without these steps, Apache does not start and may give the following error:
Jun 21 17:58:09 example.com systemd[1]: Failed to start The Apache HTTP Server.
Jun 21 17:58:09 example.com systemd[1]: Unit httpd.service entered failed state.
Jun 21 17:58:09 example.com systemd[1]: httpd.service failed.
Use
chown
to makeapache
the owner of the web directory:sudo chown apache:apache -R /var/www/html/example.com/
Modify the permissions for files and directories:
cd /var/www/html/example.com/ find . -type f -exec sudo chmod 0644 {} \; find . -type d -exec sudo chmod 0755 {} \;
Use SELinux’s
chcon
to change the file security context for web content:sudo chcon -t httpd_sys_content_t /var/www/html/example.com -R sudo chcon -t httpd_sys_rw_content_t /var/www/html/example.com -R
Enable Apache to start at boot and restart the service for the above changes to take place:
sudo systemctl enable httpd.service sudo systemctl restart httpd.service
sudo setsebool -P httpd_can_network_connect on
command.Configure FirewallD to Allow HTTP Connections
FirewallD is enabled for CentOS 7 Linodes, but HTTP is not included in the default set of services.
View the default set of services:
sudo firewall-cmd --zone=public --list-services
ssh dhcpv6-client
To allow connections to Apache, add HTTP as a service:
sudo firewall-cmd --zone=public --add-service=http --permanent sudo firewall-cmd --zone=public --add-service=http
Visit your domain or public IP to test the Apache server and view the default Apache page.
Note Rename Apache’s default welcome page. When this file is present it takes precedence over other configurations via the
LocationMatch
directive.sudo mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.bk
Once Apache has been installed and configured on your server, it is time to install PHP and the MySQL database on your CentOS 7 Linode.
MariaDB
Install and Configure
MariaDB is a relational database management system (RDBMS), and is a popular component of many applications.
Install the MariaDB-server package:
sudo yum install mariadb-server
Set MariaDB to start at boot and start the daemon for the first time:
sudo systemctl enable mariadb.service sudo systemctl start mariadb.service
Run
mysql_secure_installation
to secure MariaDB. You will be given the option to change the MariaDB root password, remove anonymous user accounts, disable root logins outside of localhost, and remove test databases and reload privileges. It is recommended that you answer yes to these options:sudo mysql_secure_installation
Create a MariaDB Database
Log in to MariaDB:
mysql -u root -p
Enter MariaDB’s root password. You will get the MariaDB prompt.
Create a new database and user with permissions to use it:
create database webdata; grant all on webdata.* to 'webuser' identified by 'password';
In the above example
webdata
is the name of the database,webuser
the user, andpassword
a strong password.Exit MariaDB
quit
With Apache and MariaDB installed, you are now ready to install PHP on your CentOS 7 Linode to provide scripting support for your web pages. CentOS needs PHP to connect to the MySQL to get information.
PHP
Install and Configure
Install PHP:
sudo yum install php php-pear php-mysqlnd
Edit
/etc/php.ini
for better error messages and logs, and upgraded performance. These modifications provide a good starting point for a Linode 2GB:- File: /etc/php.ini
1 2 3
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR error_log = /var/log/php/error.log max_input_time = 30
Note Ensure that all lines noted above are uncommented. A commented line begins with a semicolon (;).Create the log directory for PHP and give the Apache user ownership:
sudo mkdir /var/log/php sudo chown apache:apache /var/log/php
Reload Apache:
sudo systemctl reload httpd.service
Optional: Test and Troubleshoot the LAMP Stack
In this section, you’ll create a test page that shows whether Apache can render PHP and connect to the MariaDB database. This can be helpful in locating the source of an error if one of the elements of your LAMP stack is not communicating with the others.
Paste the following code into a new file,
phptest.php
, in thepublic_html
directory. Modifywebuser
andpassword
to match the information entered in the Create a MariaDB Database section above:- File: /var/www/html/example.com/public_html/phptest.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<html> <head> <title>PHP Test</title> </head> <body> <?php echo '<p>Hello World</p>'; // In the variables section below, replace user and password with your own MariaDB credentials as created on your server $servername = "localhost"; $username = "webuser"; $password = "password"; // Create MariaDB connection $conn = mysqli_connect($servername, $username, $password); // Check connection - if it fails, output includes the error message if (!$conn) { die('<p>Connection failed: </p>' . mysqli_connect_error()); } echo '<p>Connected successfully</p>'; ?> </body> </html>
Navigate to
example.com/phptest.php
from your local machine. If the components of your LAMP on CentOS 7 are working correctly, the browser displays a “Connected successfully” message. If not, the output is an error message.Remove the test file:
sudo rm /var/www/html/example.com/public_html/phptest.php
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on