How to Install Apache, MySQL, and PHP (LAMP) Stack on Fedora 34

Updated on June 10, 2021
How to Install Apache, MySQL, and PHP (LAMP) Stack on Fedora 34 header image

Introduction

The LAMP stack is a software bundle composed of Linux, Apache, MySQL or MariaDB, and PHP. These free open-source software applications drive dynamic web applications such as WordPress, Joomla, Magento, and more.

In this guide, you'll install Apache as the HTTP server, MySQL or MariaDB as a relational database management system, and PHP as the server-side scripting language. On the Linux part, you'll use Fedora 34 operating system. After completing this guide, your Apache web server will run a PHP script, connect to a database, and return a successful response.

Prerequisites

Before you begin, ensure you have the following:

1. Install Apache Web Server

SSH to your server and ensure your system is up to date.

$ sudo dnf -y upgrade

In Fedora, the Apache HTTP server runs as an httpd daemon. Install the package by running the command below.

$ sudo dnf -y install httpd

Start the httpd service.

$ sudo systemctl start httpd

Visit your server's domain name or public IP address in a web browser to test the installation.

http://192.0.2.1

You should now see a Fedora Web Server Test Page as shown below.

Fedora Web Server Test Page

Enable the web server to start automatically.

$ sudo systemctl enable httpd

You may also find these control command useful:

  • To stop the web server when performing maintenance:

      $ sudo systemctl stop httpd
  • To stop the web server momentarily and restart it after changing the configuration files:

      $ sudo systemctl restart httpd
  • To reload the web server's configurations without interrupting active connections:

      $ sudo systemctl reload httpd

After installing Apache, you can locate the httpd service main configuration file from this location.

/etc/httpd/conf/httpd.conf

When working in a system architecture that calls for separation of concerns, you can include different configuration files under the directory below.

/etc/httpd/conf.d/

By default, Apache serves all requests from /var/www/html.

2. Install MySQL/MariaDB Server

When setting up a LAMP stack, you have a choice of either MySQL or MariaDB server. Both are compatible with most popular content management systems.

Please note: MariaDB is a fork of the MySQL package, and installing both packages on the same server causes conflicts.

Option 1: Install the MariaDB Server

To set up the MariaDB server, run the command below.

$ sudo dnf install -y mariadb-server

After installation, the MariaDB server runs under the daemon mariadb. Start the mariadb service.

$ sudo systemctl start mariadb

Enable the service to start automatically when your server boots.

$ sudo systemctl enable mariadb

You can find the main MariaDB configuration file in the location below.

/etc/my.cnf

You can add more configuration files that load when the MariaDB server starts in /etc/my.cnf.d/.

If you make any changes to the MariaDB configuration file, you must always restart the mariadb service using the command below.

$ sudo systemctl restart mariadb

To stop the mariadb service, use the command below.

$ sudo systemctl stop mariadb

To continue testing this guide, make sure the MariaDB server is running.

$ sudo systemctl start mariadb

Option 2: Install the MySQL Server

If you have a particular need for MySQL server or prefer it over the MariaDB server, follow these installation steps.

To install the MySQL server, pull the community-mysql-server package from the main Fedora repository.

$ sudo dnf install -y community-mysql-server

After completing the installation, start the MySQL service. It runs under the service - mysqld

$ sudo systemctl start mysqld

Enable the MySQL server to run automatically when your server boots.

$ sudo systemctl enable mysqld

You can locate the main MySQL configuration file in the location below.

/etc/my.cnf.d/community-mysql-server.cnf

Also, you can place fragmented configuration files under the directory below.

/etc/my.cnf.d/

Remember to restart the mysqld service if you make any configuration changes.

$ sudo systemctl restart mysqld

To stop the MySQL server at any time, run the command below.

$ sudo systemctl stop mysqld

To proceed with this guide, ensure the MySQL server is running.

$ sudo systemctl start mysqld

Secure the Database Server

Secure the database server by running the command below. This applies to both MySQL and MariaDB.

$ sudo mysql_secure_installation

Answer the prompts depending on the package that you're configuring. Replace EXAMPLE_PASSWORD with a strong value for the root user. For this guide, you may skip setting up the validate_password component that validates the strength of passwords in the MySQL server. However, in a production environment, you can enable it to avoid using weak passwords.

After you've finished securing MySQL/MariaDB server, log in to the database server as a root user.

$ sudo mysql -u root -p

Enter your root password for the MySQL/MariaDB server and press Enter to proceed. Then, issue the command below to create a sample_db database and a test_user user.

  • MySQL server.

      mysql> CREATE DATABASE sample_db;
             CREATE USER 'test_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'EXAMPLE_PASSWORD';
             GRANT ALL PRIVILEGES ON sample_db.* TO 'test_user'@'localhost';
             FLUSH PRIVILEGES;
  • MariaDB server.

      MariaDB> CREATE DATABASE sample_db;
               GRANT ALL PRIVILEGES on sample_db.* TO 'test_user'@'localhost' identified by 'EXAMPLE_PASSWORD';

Output:

Query OK, 1 row affected (0.00 sec)
...

Exit from the database server command-line interface.

  • MySQL server.

      mysql> QUIT;
  • MariaDB server.

      MariaDB> QUIT;

3. Install PHP

Install the php package.

$ sudo dnf install -y php

Install some common PHP extensions required to create dynamic websites and web applications.

$ sudo dnf install -y php-cli php-fpm php-common php-mbstring php-curl php-gd php-mysqlnd php-json php-xml php-intl php-pecl-apcu php-opcache

You can locate the main PHP configuration file in this location.

/etc/php.ini

In case you make any changes to the PHP configuration file, remember to reboot the Apache web server. PHP also scans the directory below for configuration files.

/etc/php.d

Restart the httpd service to load the PHP package.

$ sudo systemctl restart httpd

Install nano text editor and open a new /var/www/html/test.php to test PHP connectivity to the MySQL/MariaDB database.

$ sudo dnf install -y nano
$ sudo nano /var/www/html/test.php

Paste the content below into the file.

<?php

$con = new mysqli('localhost', 'test_user', 'EXAMPLE_PASSWORD', 'sample_db');

if ($con->connect_error) {
    die("Failed to connect to the database: " . $con->connect_error);
}

echo "Connection to the database was successful";

Save the file by pressing Ctrl + X, then Y and Enter.

Visit your web server's domain name or IP address in a web browser.

http://192.0.2.1/test.php

You should see a success message. Your PHP script is now able to connect to the MySQL/MariaDB database.

Connection to the database was successful

Your Apache web server is serving the web content from the /var/www/html directory. Up to this point, your LAMP stack is working as expected.

Conclusion

In this tutorial, you've installed a LAMP stack on your Fedora 34 server. With this setup in place, you can now upload your website/web application or probably install a content management system like WordPress.