tutorial-cloud/blog/2019-05-29-blog-18.md
2024-10-25 10:05:08 +05:30

326 lines
19 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
slug: Building Your Website Foundation Step-by-Step Guide to Installing LAMP Stack on CentOS 8
title: Building Your Website Foundation Step-by-Step Guide to Installing LAMP Stack on CentOS 8
authors: [slorber, yangshun]
tags: [hola, docusaurus]
---
<!-- truncate -->
import CodeBlock from '@site/src/components/CodeBloack';
import test from '@site/static/img/test-page.png';
import php from '@site/static/img/image-95.jpeg.webp';
import todo from '@site/static/img/todo_list.png';
<div className="head">Introduction</div>
<div className="text">A "LAMP" stack is like a group of tools that work together to help your computer host websites and web apps made with PHP. Think of it as a team: "L" stands for Linux, the operating system; "A" is for Apache, the web server; "M" is for MariaDB, where all the important data hangs out; and "P" is for PHP, the tool that makes things happen on the website. Together, they create a combination to bring your websites to life!</div><br/>
<div className="text">In a LAMP stack, which helps your computer host websites, the part that manages data is usually a MySQL database server. However, for CentOS 8, the default toolbox didn't include MySQL. So, people started using MariaDB instead. Think of MariaDB as a friendly cousin of MySQL—it does the same job! The cool thing is, you can switch to MariaDB without needing to change anything in how your website works. It's like swapping out one tool for another without causing any trouble. Easy peasy!</div><br/>
<div className="text">In this guide, you're going to set up a special toolbox called a LAMP stack on a CentOS 8 server. We'll use MariaDB as the manager for all the important information your website needs. It's like creating a superhero team for your website! Let's get started!</div><br/>
<div className="head">What You Need Before Setting Up Your Website on CentOS 8</div>
<div className="text">Before we start, make sure you have a special computer running CentOS 8. You should be logged in as a regular user with some extra powers (sudo privileges), and there's a protective shield on your computer called a firewall. If you haven't set up your computer this way yet, no worries! You can check out our beginner's guide called "Initial Server Setup Guide for CentOS 8" to get everything ready. Once you're set up, we can dive into the fun stuff!</div>
<div className="text" style={{ fontSize:'28px' }}>Step 1: Setting Up the Apache Web Server</div>
<div className="text">To show web pages to our visitors, we're going to use Apache, a widely used open-source web server that can be set up to display PHP pages. We'll use dnf, a tool that helps us install software on CentOS 8.</div><br/>
<div className="text">To install Apache, follow these steps:</div><br/>
<div className="text">Open the terminal on your CentOS 8 system. (This is like a command center where you can type instructions to your computer.) Type the following command and press Enter:</div>
<CodeBlock code={`sudo dnf install httpd`} /><br/>
<div className="text">When asked, type 'y' and press Enter to confirm that you want to install Apache.</div><br/>
<div className="text">Once the installation is done, use the following command to turn on and start the server:</div>
<CodeBlock code={`sudo systemctl start httpd`} /><br/>
<div className="text">If you've set up the firewalld firewall as we instructed earlier, you'll need to allow connections to Apache. Use the following command to permanently allow HTTP connections, which use port 80 by default:</div>
<CodeBlock code={`sudo firewall-cmd --permanent --add-service=http`} /><br/>
<div className="text">To make sure the change worked, you can check by running:</div>
<CodeBlock code={`sudo firewall-cmd --permanent --list-all`} /><br/>
<div className="text">You'll get a response that looks something like this:</div>
<CodeBlock code={`Output
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: cockpit dhcpv6-client http ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
`} /><br/>
<div className="text">You'll have to reload the firewall settings so that the changes become active:</div>
<CodeBlock code={`sudo firewall-cmd --reload`} /><br/>
<div className="text">Now that you've added the new firewall rule, you can check if the server is working by opening your web browser and typing in your server's public IP address or domain name.</div><br/>
<div className="note"><strong>Note</strong>: If you're using Cloudtopiaa as your DNS hosting provider, you can find step-by-step instructions in our product documentation on how to set up a new domain name and connect it to your server.</div><br/>
<div className="text">If you haven't linked a domain name to your server yet and you're not sure of your server's public IP address, you can find it by running this command:</div>
<CodeBlock code={`ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'`} /><br/>
<div className="text">This will show you a few different IP addresses. You can try each of them one by one in your web browser.</div><br/>
<div className="text">Alternatively, you can check which IP address is reachable from other places on the internet:</div>
<CodeBlock code={`curl -4 icanhazip.com`} /><br/>
<div className="text">Copy and paste the IP address that you see into your web browser's address bar, and it will take you to Apache's default landing page.</div><br/>
<img src={test} alt="Test Page" /> <br/>
<div className="text">If you can view this page, it means your web server is now installed correctly.</div><br/>
<div className="head">Step 2 — Adding MariaDB to the server</div>
<div className="text">Now that your web server is set up, you need to install a database system to store and manage data for your website. We'll install MariaDB, which is similar to MySQL and is commonly used for web applications.</div><br/>
<div className="text">To install MariaDB, follow these steps,Run the following command:</div>
<CodeBlock code={`sudo dnf install mariadb-server`} /><br/>
<div className="text">Once the installation is complete, you can turn on and start the MariaDB server by:</div>
<CodeBlock code={`sudo systemctl start mariadb`} /><br/>
<div className="text">To enhance the security of your database server, it's a good idea to run a security script provided with MariaDB. This script helps by removing some insecure default settings and tightening access to your database system. To start the interactive script, simply run:</div>
<CodeBlock code={`sudo mysql_secure_installation`} /><br/>
<div className="text">This script will guide you through a series of questions where you can adjust your MariaDB setup. The first question will ask for the current database root password. This is different from the system root user. The database root user has complete control over the database system. Since you've just installed MariaDB and haven't set any password yet, simply press ENTER when prompted.</div><br/>
<div className="text">The next question asks if you want to create a password for the database root user. Since MariaDB uses a more secure authentication method for the root user by default, you don't need to set a password at this time. Simply type 'N' and then press ENTER.</div><br/>
<div className="text">After that, you can press 'Y' and then ENTER to accept the default options for all the following questions. This will remove anonymous users and the test database, disable remote root login, and apply these new rules immediately so the server will enforce the changes you made.</div><br/>
<div className="text">Once you're done, log in to the MariaDB console by typing:</div>
<CodeBlock code={`sudo mysql`} /><br/>
<div className="text">This command connects to the MariaDB server as the main database user called root. The use of "sudo" before the command helps to infer this. When you run this command, you'll likely see something similar to this output:</div>
<CodeBlock code={`Output
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
`} /><br/>
<div className="text">You may have noticed that you didn't have to enter a password to log in as the root user. This is because the default way MariaDB handles authentication for the administrative user is through unix_socket instead of using a password. This might seem like a security concern initially, but it actually makes the database server more secure. It ensures that only users with sudo privileges on the system can log in as the MariaDB root user, either directly from the command line or from applications running with similar privileges.In simpler terms, this means you won't be able to use the root user to connect to the database from a PHP application.</div><br/>
<div className="text">For better security, it's a good practice to have separate user accounts with limited privileges set up for each database, especially if you're hosting multiple databases on your server. To illustrate this setup, we'll create a database called "example_database" and a user named "example_user". Feel free to use different names if you prefer.</div><br/>
<div className="text">To create a new database, enter the following command in your MariaDB console:</div>
<CodeBlock code={`CREATE DATABASE example_database;`} /><br/>
<div className="text">Now you can make a new user and give them complete access to the database you just made. The following command sets the user's password as "password," but you should use a strong password of your own choice:</div>
<CodeBlock code={`GRANT ALL ON example_database.* TO 'example_user'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;`} /><br/>
<div className="text">This grants the user "example_user" full control over the "example_database" database, while ensuring they can't create or change other databases on your server.</div><br/>
<div className="text">After that, you need to flush the privileges to make sure they're saved and ready for use in the current session:</div>
<CodeBlock code={`FLUSH PRIVILEGES;`} /><br/>
<div className="text">After completing the previous steps, you can exit the MariaDB shell by typing:</div>
<CodeBlock code={`exit`} /><br/>
<div className="text">To check if the new user has the correct permissions, log in to the MariaDB console once more, but this time use the credentials of the custom user:</div>
<CodeBlock code={`mysql -u example_user -p`} /><br/>
<div className="text">In this command, notice the "-p" flag, which will ask you to enter the password you selected when creating the "example_user" user. Once you're logged in to the MariaDB console, check to see if you can access the "example_database" database:</div>
<CodeBlock code={`SHOW DATABASES;`} /><br/>
<div className="text">You'll see something like this displayed:</div>
<CodeBlock code={`Output
+--------------------+
| Database |
+--------------------+
| example_database |
| information_schema |
+--------------------+
2 rows in set (0.000 sec)
`} /><br/>
<div className="text">To leave the MariaDB shell, simply type:</div>
<CodeBlock code={`exit`} /><br/>
<div className="text">Now that your database system is set up, you're ready to proceed with installing PHP, which is the last part of setting up the LAMP stack.</div><br/>
<div className="head">Step 3 — The PHP installation</div>
<div className="text">Now that you have Apache set up for serving your website and MariaDB for managing your database, it's time to install PHP. PHP is what processes code to show dynamic content to your website visitors. Along with the main PHP package, we'll need php-mysqlnd, a module that lets PHP communicate with MySQL-based databases. Don't worry, the essential PHP packages will be automatically installed as dependencies.</div><br/>
<div className="text">To install PHP and php-mysqlnd packages using the dnf package manager, do the following:</div>
<CodeBlock code={`sudo dnf install php php-mysqlnd`} /><br/>
<div className="text">Once the installation is complete, you'll need to restart the Apache web server to activate the PHP module:</div>
<CodeBlock code={`sudo systemctl restart httpd`} /><br/>
<div className="text">Your web server is now ready to go! Next, we'll create a simple PHP testing script to ensure everything is working as it should.</div><br/>
<div className="head">Step 4 — Using Apache to test PHP</div>
<div className="text">When you install Apache on CentOS 8, it automatically sets up a folder called "html" at /var/www/html. You dont have to adjust any settings in Apache for PHP to function properly on your web server.</div><br/>
<div className="text">The only change we'll make is to adjust the permission settings on the folder where Apache stores its files. This will allow you to create and edit files in that folder without needing to use "sudo" before each command.</div><br/>
<div className="text">Use the following command to change the ownership of the default Apache document root to a user and group named "sammy". Make sure to replace "sammy" with your own username and group:</div>
<CodeBlock code={`sudo chown -R sammy.sammy /var/www/html/`} /><br/>
<div className="text">sudo chown -R sammy.sammy /var/www/html/</div><br/>
<div className="text">The default text editor in CentOS 8 is vi, which is powerful but may be challenging for beginners. If you prefer a simpler editor, you can install nano, which is more user-friendly, to help you edit files on your CentOS 8 server.</div>
<CodeBlock code={`sudo dnf install nano`} /><br/>
<div className="text">When asked, type 'y' to confirm the installation.<br/><br/>
Next, let's create a new PHP file named "info.php" in the /var/www/html directory:</div>
<CodeBlock code={`nano /var/www/html/info.php`} /><br/>
<div className="text">Below is a simple PHP code that will show details about the PHP environment running on your server:</div>
<CodeBlock code={`<?php
phpinfo();
`} /><br/>
<div className="text">When you're done, save and close the file. If you're using nano, you can do this by pressing CTRL+X, then Y, and finally hitting ENTER to confirm.</div><br/>
<div className="text">Now, let's check if our web server can correctly show content generated by a PHP script. Open your web browser and enter your server's hostname or IP address, followed by "/info.php":</div>
<CodeBlock code={`http://server_host_or_IP/info.php`} /><br/>
<div className="text">When you open the browser and visit your server's address followed by "/info.php", you'll see a page that looks something like this:</div><br/>
<img src={php} alt="Test Page" /> <br/>
<div className="text">After you've finished checking the PHP server information on that page, it's a good idea to delete the file you created. This is because it contains sensitive details about your PHP environment and CentOS server. You can use the "rm" command to remove the file:</div>
<CodeBlock code={`rm /var/www/html/info.php`} /><br/>
<div className="text">If you ever need it again in the future, you can always create this file again. Now, let's move on to testing the database connection from the PHP side.</div><br/>
<div className="head">Step 5 — Connecting to the database from PHP (optional)</div>
<div className="text">If you'd like to check whether PHP can connect to MariaDB and run database queries, you can create a test table with some sample data and then retrieve this data from a PHP script.</div><br/>
<div className="text">To begin, let's connect to the MariaDB console using the database user you created earlier in Step 2 of this guide:</div>
<CodeBlock code={`mysql -u example_user -p`} /><br/>
<div className="text">Let's create a table called "todo_list". In the MariaDB console, enter the following command:</div>
<CodeBlock code={`CREATE TABLE example_database.todo_list (
item_id INT AUTO_INCREMENT,
content VARCHAR(255),
PRIMARY KEY(item_id)
);
`} /><br/>
<div className="text">Now, let's add some data into the "todo_list" table. You can repeat the following command a few times, each time using different values:</div>
<div className="text">INSERT INTO example_database.todo_list (content) VALUES ("My first important item");</div><br/>
<div className="text">To make sure the data was saved correctly in your table, execute the following command:</div>
<CodeBlock code={`SELECT * FROM example_database.todo_list;`} /><br/>
<div className="text">You'll receive output similar to this:</div>
<CodeBlock code={`Output
+---------+--------------------------+
| item_id | content |
+---------+--------------------------+
| 1 | My first important item |
| 2 | My second important item |
| 3 | My third important item |
| 4 | and this one more thing |
+---------+--------------------------+
4 rows in set (0.000 sec)
`} /><br/>
<div className="text">Once you've confirmed that you have valid data in your test table, you can exit the MariaDB console:</div>
<CodeBlock code={`exit`} /><br/>
<div className="text">Now, let's make the PHP script that will connect to MariaDB and retrieve your data. Create a new PHP file in your chosen web directory using your preferred editor. We'll use nano for this:</div><br/>
<div className="text">nano /var/www/html/todo_list.php</div><br/>
<div className="text">Copy and paste the following code into your PHP script:</div>
<CodeBlock code={`<?php
$user = "example_user";
$password = "password";
$database = "example_database";
$table = "todo_list";
try {
$db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
echo "<h2>TODO</h2><ol>";
foreach($db->query("SELECT content FROM $table") as $row) {
echo "<li>" . $row['content'] . "</li>";
}
echo "</ol>";
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
`} /><br/>
<div className="text">Once you've finished editing, save and close the file.</div><br/>
<div className="text">Now, you can view this page in your web browser by entering your server's hostname or public IP address, followed by "/todo_list.php":</div>
<CodeBlock code={`http://server_host_or_IP/todo_list.php`} /><br/>
<div className="text">You'll see a page similar to this, displaying the content you added to your test table:</div><br/>
<img src={todo} alt="Todo Page" /> <br/>
<div className="text">This indicates that your PHP setup is successfully connecting and communicating with your MariaDB server.</div><br/>
<div className="con">Conclusion:</div>
<div className="text">In this guide, you've established a solid base for hosting PHP websites and applications for your visitors. You've configured Apache to handle PHP requests and created a MariaDB database to store your website's data.</div><br/>
<div className="text"><strong>Ready to launch your PHP-powered website? Let's get started!</strong></div>
<div className="text"><strong>If you have any questions or need further assistance, feel free to ask.</strong></div>