The LAMP stack is a collection of open-source software that is commonly used to enable a server to host dynamic websites and web applications. LAMP stands for Linux, Apache, MySQL, and PHP. In this guide, we will walk you through the step-by-step process of installing and configuring the LAMP stack on Rocky Linux 8, a free and open-source operating system.
1. Introduction
Before we begin, let’s briefly explain what each component of the LAMP stack does:
- Linux: Rocky Linux 8 is based on CentOS and provides a stable and secure platform for hosting web applications.
- Apache: Apache is a widely used web server that serves web pages to clients over the internet.
- MySQL: MariaDB, a popular branch of MySQL, is a powerful relational database management system that stores and manages website data.
- PHP: PHP is a server-side scripting language that processes dynamic content and interacts with databases.
Now that we have a basic understanding of the LAMP stack components, let’s dive into the installation process.
2. Installing Apache Web Server
The first step is to install the Apache web server on your Rocky Linux 8 system. Open the terminal and run the following command:
sudo yum install httpd
This command will install the Apache web server along with its required dependencies. Once the installation is complete, enable Apache to start automatically upon system boot by running the following command:
sudo systemctl enable httpd
To start the Apache web server, use the following command:
sudo systemctl start httpd
You can verify the status of Apache by running the following command:
sudo systemctl status httpd
If Apache is running properly, you should see an output similar to the following:
● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2021-05-06 13:39:18 EDT; 6s ago Docs: man:httpd.service(8) Main PID: 9517 (httpd) Status: "Started, listening on: port 80" Tasks: 213 (limit: 4956) Memory: 25.0M CGroup: /system.slice/httpd.service ├─9517 /usr/sbin/httpd -DFOREGROUND ├─9518 /usr/sbin/httpd -DFOREGROUND ├─9519 /usr/sbin/httpd -DFOREGROUND
3. Configuring Apache
After installing Apache, you need to configure it to serve your web pages correctly. By default, Apache serves web pages from the /var/www/html
directory. You can create a simple HTML page to test if Apache is working fine.
Create a file named index.html
in the /var/www/html
directory with the following command:
sudo echo "Hello there, Apache web server is now running" > /var/www/html/index.html
Restart Apache to apply the changes:
sudo systemctl restart httpd
Now, you can access your web server by entering your server’s IP address or domain name in your web browser. If everything is set up correctly, you should see the message “Hello there, Apache web server is now running” on the page.
4. Installing PHP
The next step is to install PHP on your Rocky Linux 8 system. PHP is a popular server-side scripting language used to process dynamic content and interact with databases.
To install PHP along with some common modules, open the terminal and run the following command:
sudo dnf install php php-zip php-intl php-mysqlnd php-dom php-simplexml php-xml php-xmlreader php-curl php-exif php-ftp php-gd php-iconv php-json php-mbstring php-posix php-sockets php-tokenizer
This command will install PHP and its necessary dependencies. Once the installation is complete, you can verify the PHP version by running the following command:
php -v
If PHP is installed correctly, you should see an output similar to the following:
PHP 7.4.19 (cli) (built: May 4 2021 11:06:37) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
5. Configuring PHP
After installing PHP, you need to make some configuration changes to ensure it works seamlessly with Apache. Open the PHP configuration file using a text editor of your choice:
sudo vi /etc/php.ini
Inside the file, find the date.timezone
directive and set your desired timezone. For example:
date.timezone = America/New_York
Save and exit the file.
Next, you need to configure Apache to use PHP to process .php
files. Open the Apache configuration file using the following command:
sudo vi /etc/httpd/conf/httpd.conf
Find the following lines in the file:
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
Add index.php
to the DirectoryIndex
directive, like this:
<IfModule dir_module> DirectoryIndex index.php index.html </IfModule>
Save and exit the file.
Restart Apache to apply the changes:
sudo systemctl restart httpd
6. Installing MariaDB Server
The next component of the LAMP stack is the MariaDB server, a popular and powerful relational database management system. To install MariaDB on Rocky Linux 8, run the following command:
sudo yum install mariadb-server
This command will install the MariaDB server along with its dependencies. Once the installation is complete, enable MariaDB to start automatically upon system boot:
sudo systemctl enable mariadb
Start the MariaDB server using the following command:
sudo systemctl start mariadb
To check the status of the MariaDB server, run the following command:
sudo systemctl status mariadb
If MariaDB is running properly, you should see an output similar to the following:
● mariadb.service - MariaDB 10.3 database server Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2021-05-06 13:59:14 EDT; 2s ago Docs: man:mysqld(8) https://mariadb.com/kb/en/library/systemd/ Main PID: 13095 (mysqld) Status: "Taking your SQL requests now..." Tasks: 30 (limit: 4956)
7. Configuring MariaDB
After installing MariaDB, you need to perform some initial configuration steps. Run the following command to secure your MariaDB installation:
sudo mysql_secure_installation
This command will prompt you to set a root password, remove anonymous users, disallow remote root login, and remove the test database. Follow the prompts and answer the questions accordingly.
Once you have secured MariaDB, you can connect to the MySQL shell to verify the installation. Run the following command:
sudo mysql -u root -p
Enter the root password when prompted. You should now be logged in to the MySQL shell.
To view the existing databases on your MariaDB server, run the following command:
SHOW DATABASES;
You should see a list of databases similar to the following:
+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+
Congratulations! You have successfully installed and configured the LAMP stack on Rocky Linux 8.
10. Conclusion
In this guide, we have walked you through the step-by-step process of installing and configuring the LAMP stack on Rocky Linux 8. By following these instructions, you now have a robust and reliable infrastructure for hosting dynamic websites and web applications.
Remember to regularly update and maintain your LAMP stack components to ensure optimal performance and security. If you encounter any issues during the installation process, refer to the official documentation or seek assistance from the Rocky Linux community.
At Shape.host, we offer reliable and scalable cloud hosting solutions, including Cloud VPS services, to help businesses succeed online. Our team of experts is dedicated to providing top-notch support and ensuring the smooth operation of your web applications. Contact us today to learn more about our services and how we can assist you in achieving your hosting goals.