LAMP on Fedora
The LAMP stack is a set of programs for web development. It consists of Linux, Apache, MySQL and PHP. Together, they allow you to manage a web server.
This article shows you how to install and configure the Apache web server, PHP and MySQL on Fedora.
1. Install Apache
sudo dnf install httpd
sudo systemctl enable httpd.service
sudo systemctl start httpd.service
To check that the HTTP server is working correctly, run the following command:
sudo systemctl status httpd.service
(press the Q key to exit and return to the terminal prompt)
or open the link http://localhost/
2. Install PHP
sudo dnf install php php-cli php-common php-opcache php-fpm php-mysqlnd php-pecl-zip php-devel php-gd php-pecl-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json
Test PHP by creating a document in /var/www/html with the command:
sudo nano /var/www/html/info.php
and using the function phpinfo()
<?php
phpinfo();
?>
Restart your Apache server to ensure it works with the new PHP installed.
sudo systemctl restart httpd.service
Open the link http://localhost/info.php
You will get a page with the PHP version and system data.
3. Configure MariaDB
Start the database with:
sudo systemctl enable mariadb
sudo systemctl start mariadb
and check its operation:
sudo systemctl status mariadb
Run the security script before first use with
sudo mysql_secure_installation
and answer the following questions:
• Enter password for user root - Press the Enter key. As the command is running with sudo privileges, the password is not required.
• Switch to unit_socket authentication - N
• Change the root password? - N. It is safe to allow local sudo access with the default root password.
• Remove anonymous users? - Y
• Disallow root login remotely? - Y
• Remove test database and access to it? - Y
• Reload privilege tables now? - Y
Access the database with:
sudo mysql
Create a database, e.g. website.
CREATE DATABASE website;
MySQL should answer ‘Query OK’ after each line.
Create a user account for the web application to access. Provide a username and a secure password, both enclosed in single commas (’ ‘).
CREATE USER 'username' IDENTIFIED BY 'password';
Finally, grant all rights to the user.
GRANT ALL ON website.* TO 'username';
FLUSH PRIVILEGES;
Exit the SQL shell and return to the terminal prompt.
quit
4. Check LAMP stack installation
To check the components of the LAMP stack, embed a block of PHP code containing a database connection, within an HTML page. PHP code can be integrated into an HTML file using the tag <?php. The PHP code block can then connect to an SQL-based database using the command mysqli_connect. Provide the correct database credentials for the connection.
To test all components of the LAMP stack, follow these steps:
1. In Terminal, change to the directory var/www/html and create a new file, now the phptest.php
cd /var/www/html
sudo nano phptest.php
2. Add the following content to the file. Confirm that the servername variable is set to localhost. Replace USERNAME and PASSWORD with the credentials for the WEB database user account (not the root password).
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Welcome to the Site!</p>';
$servername = "localhost";
$username = "USERNAME";
$password = "PASSWORD";
// Create the MySQL connection
$conn = mysqli_connect($servername, $username, $password);
// If the conn variable is empty, the connection has failed. The output for the failure case includes the error message
if (!$conn) {
die('<p>Connection failed: </p>' . mysqli_connect_error());
}
echo '<p>Connected successfully</p>';
?>
</body>
</html>
3. Run the LAMP stack script test by opening the link http://localhost/phptest.php
4. If the test is successful, the browser displays the message:
Welcome to the site!
Connected successfully