292 lines
16 KiB
Markdown
292 lines
16 KiB
Markdown
---
|
||
slug: Installation Guide for LAMP Stack on CentOS 7
|
||
title: Installation Guide for LAMP Stack on CentOS 7 Linux, Apache, MySQL, PHP
|
||
authors: [slorber, yangshun]
|
||
tags: [hola, docusaurus]
|
||
---
|
||
|
||
<!-- truncate -->
|
||
|
||
import CodeBlock from '@site/src/components/CodeBloack';
|
||
import apache from '@site/static/img/default_apache.png';
|
||
import php from '@site/static/img/default_php.png';
|
||
import todo from '@site/static/img/todo_list.png';
|
||
|
||
<div className="head">Introduction</div><br/>
|
||
<div className="text">A "LAMP" stack is a group of software that works together to enable your computer to host websites and web apps. The term "LAMP" represents Linux (the operating system), Apache (the web server), MariaDB (a place to store website data), and PHP (a language for making websites dynamic).</div><br/>
|
||
|
||
<div className="text">On most computers, when we ask for "MySQL," we get MariaDB instead, and that's okay because they're similar. This guide will help you set up the LAMP stack on your computer running CentOS 7. It's like putting together a team to make your websites run smoothly. Let's get started!</div>
|
||
|
||
<div className="head">Before You Start</div>
|
||
<div className="text">Before we dive into the exciting part, make sure you have a special account set up on your computer. We won't be using the root account for this - it's too powerful!</div><br/>
|
||
|
||
<div className="text">Don't worry if you haven't set up this special account yet. We have a guide that shows you how to do it. Just follow our step-by-step instructions for setting up your personal space on your CentOS 7 computer. Once you've got that sorted, we'll be all set for the next steps!</div>
|
||
|
||
<div className="head">Setting Up Apache - Your Website Host</div>
|
||
<div className="text">Apache is a friendly host for your website, making it visible to visitors and handling PHP pages.To get Apache, we'll use a special tool on CentOS called "yum." It helps bring things to your computer easily.</div><br/>
|
||
|
||
<div className="text">Now, imagine you're talking to your computer. Say, "Hey, computer, I want Apache!" and use this magical command in your terminal:</div>
|
||
|
||
<CodeBlock code={`sudo yum install httpd`} /><br/>
|
||
|
||
<div clssName="text">When asked, type Y to confirm the Apache installation. After the installation finishes, kick off your Apache server by using this command.</div>
|
||
|
||
<CodeBlock code={`sudo systemctl start httpd`} /><br/>
|
||
|
||
<div clssName="text">To check if your server is working, open your web browser and type in your public IP address or your website's name.</div>
|
||
|
||
<div clssName="note"><strong>Note</strong>: If you picked Cloudtopiaa to manage your website's address, just follow the simple instructions in our guides. They'll show you how to create a new website name and connect it to your server hassle-free.</div>
|
||
|
||
<CodeBlock code={`ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'`} /><br/>
|
||
|
||
<div className="text">After running the command, you'll see a few addresses. Test each one in your web browser to see your server. Another way is to ask another server to tell you your IP address using this command:</div>
|
||
|
||
<CodeBlock code={`curl http://icanhazip.com`} /><br/>
|
||
|
||
<div className="text">Whatever way you pick, just enter your IP address into your web browser to make sure your server is working.</div>
|
||
|
||
<CodeBlock code={`http://your_server_IP_address`} /><br/>
|
||
|
||
<div className="text">When you check, you should see a basic web page – that's the default landing page for CentOS 7 Apache.</div>
|
||
|
||
<img src={apache} alt="default_apache" />
|
||
|
||
<div className="text">To make sure Apache starts automatically when your computer starts up, use this command:</div>
|
||
|
||
<CodeBlock code={`sudo systemctl enable httpd.service`} /><br/>
|
||
|
||
<div className="head">Installing MariaDB for Your Website's Database Management</div>
|
||
|
||
<div className="text">Now that your website foundation is set up, let's install MariaDB. It helps manage and store information for your site in organized databases.<br/><br/>To get MariaDB on your system, just run this command:</div>
|
||
|
||
<CodeBlock code={`sudo yum install mariadb-server`} /><br/>
|
||
|
||
<div className="text">After the installation finishes, kick off MariaDB with this command:</div>
|
||
|
||
<CodeBlock code={`sudo systemctl start mariadb`} /><br/>
|
||
|
||
<div className="text">To make sure MariaDB starts automatically when your computer boots up, use this command:</div>
|
||
|
||
<CodeBlock code={`sudo systemctl enable mariadb.service`} /><br/>
|
||
|
||
<div className="text">To enhance the security of your database server, it's a good idea to use a special script that's already installed with MariaDB. This script will fix some security issues and make sure your database is more protected.</div><br/>
|
||
|
||
<div className="text">To start the script, run:</div>
|
||
|
||
<CodeBlock code={`sudo mysql_secure_installation`} /><br/>
|
||
|
||
<div className="text">This script will ask you a few questions to make your MariaDB setup more secure. The first question is about the 'database root password.' It's not the same as your computer's root password. The database root is like a manager with full control over the database. Since you just installed MariaDB and didn't set up a password yet, leave it blank and press ENTER when asked.</div><br/>
|
||
|
||
<div className="text">The next question will ask if you want to create a password for the database root. Type N and press ENTER.</div><br/>
|
||
|
||
<div className="text">After that, just type Y and press ENTER for the following questions. This will get rid of any unnecessary users and the test database, stop remote root login, and apply these changes right away.</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 action links you to the MariaDB server as the main database boss, also known as the 'root' user:</div>
|
||
|
||
|
||
<CodeBlock code={`Output
|
||
Welcome to the MariaDB monitor. Commands end with ; or \g.
|
||
Your MariaDB connection id is 12
|
||
Server version: 5.5.68-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. \
|
||
`} /><br/>
|
||
|
||
|
||
<div className="text">For better security, it's a good idea to have separate user accounts with limited access for each database, especially if you have more than one on your server.</div><br/>
|
||
|
||
<div className="text">To see how it works, let's make a database called 'example_database' and a user named 'example_user.' You can use different names if you want.</div><br/>
|
||
|
||
<div className="text">In your MariaDB console, type the following command to create a new database:</div>
|
||
|
||
<CodeBlock code={`CREATE DATABASE example_database;`} /><br/>
|
||
|
||
<div className="text">Let's make a new user and give them complete access to the special database you just made. The command below sets the password as 'password,' but make sure to use a strong and secure password of your choice:</div>
|
||
|
||
|
||
<CodeBlock code={`GRANT ALL ON example_database.* TO 'example_user'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;`} /><br/>
|
||
|
||
<div className="text">This command ensures that the 'example_user' can do everything with the 'example_database' but won't be able to mess with any other databases on your server.<br/> <br/> After that, use the FLUSH statement to make sure the changes to 'example_user' are saved and take effect:</div>
|
||
|
||
|
||
<CodeBlock code={`FLUSH PRIVILEGES;`} /><br/>
|
||
|
||
<div className="text">To leave the MariaDB shell, just type:</div>
|
||
|
||
<CodeBlock code={`exit`} /><br/>
|
||
|
||
<div className="text">To check if the new user has the right permissions, log back into the MariaDB console, but this time use the 'example_user' details you set up earlier:</div>
|
||
|
||
<CodeBlock code={`mysql -u example_user -p`} /><br/>
|
||
|
||
<div className="text">Remember the '-p' flag in this command? It'll ask for the password you set when making the 'example_user.' Once you're in the MariaDB console, check if you can access the 'example_database' using this statement:</div>
|
||
|
||
<CodeBlock code={`SHOW DATABASES;`} /><br/>
|
||
|
||
<div className="text">If everything went well, you should see 'example_database' in the output:</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, just type:</div>
|
||
|
||
<CodeBlock code={`exit`} /><br/>
|
||
|
||
<div className="text">Your database is all set, and now you can move on to installing PHP.</div>
|
||
|
||
<div className="head">Adding PHP for Interactive Website Content</div>
|
||
|
||
<div className="text">Now that you have Apache for showing your website and MariaDB for storing data, let's add PHP to make your site interactive. PHP processes code to show dynamic content. You'll also need 'php-mysql' to make PHP talk to MySQL databases. No worries about the core PHP packages; they'll be installed automatically.</div>
|
||
|
||
<div className="text">Just use this command to get both 'php' and 'php-mysql' packages with yum:</div>
|
||
|
||
<CodeBlock code={`sudo yum install php php-mysql`} /><br/>
|
||
|
||
<div className="text">After installing PHP, restart the Apache web server so that it recognizes the new PHP module you just added:</div>
|
||
|
||
<CodeBlock code={`sudo systemctl restart httpd.service`} /><br/>
|
||
|
||
<div className="text">Your server is now set up with everything needed for your LAMP stack application. The next thing to do is to check if everything is working well together.</div>
|
||
|
||
<div className="head">Checking PHP on Your Apache Web Server</div>
|
||
|
||
<div className="text">The initial setup of Apache on CentOS 7 puts the main folder for your website at /var/www/html. You don't have to tweak any Apache settings for PHP to work correctly.</div><br/>
|
||
|
||
<div className="text">But, if you want to make things easier, you can change the permission settings on that folder. This way, you can create and edit files in that directory without needing to use 'sudo' every time.</div><br/>
|
||
|
||
<div className="text">Run the following command to change the owner of the default Apache folder to a user and group named 'sammy.' Just replace 'sammy' with your own username and group:</div>
|
||
|
||
<CodeBlock code={`sudo chown -R sammy.sammy /var/www/html/`} /><br/>
|
||
|
||
<div className="text">To check if your web server is working correctly, let's create a PHP test file. You can use any text editor you like. In the examples below, we'll use the default 'vi' text editor in CentOS 7.</div><br/>
|
||
|
||
<div className="text">Make a new PHP file called 'info.php' in the 'var/www/html' directory:</div>
|
||
|
||
<CodeBlock code={`vi /var/www/html/info.php`} /><br/>
|
||
|
||
<div className="text">This will open an empty PHP file in the 'var/www/html' directory. Press 'I' to start typing and making changes. Now, enter the following PHP code:</div>
|
||
|
||
<CodeBlock code={`<?php phpinfo(); ?>`} /><br/>
|
||
|
||
<div className="text">This PHP code shows details about the PHP setup on your server. After editing, press 'ESC' to exit insert mode in vi. Then type ':x' to save and close the file</div><br/>
|
||
|
||
<div className="text">To check if your web server can display PHP content correctly, go to your server's public IP address followed by '/info.php' in a web browser</div>
|
||
|
||
<CodeBlock code={`http://your_server_IP_address/info.php`} /><br/>
|
||
|
||
<div className="text">When you open your web browser and go to your server's public IP address followed by '/info.php', you'll see a page that looks like the one below:</div><br/>
|
||
|
||
<img src={php} alt="default_php" /><br/>
|
||
|
||
<div className="text">This page shows information about your server from the PHP perspective. It's helpful for solving problems and making sure your settings are correct. When you're finished, it's a good idea to delete this file because it contains sensitive information about your PHP and CentOS setup.<br/> <br/>
|
||
To get rid of the file, you can use 'rm':</div>
|
||
|
||
<CodeBlock code={`rm /var/www/html/info.php`} /><br/>
|
||
|
||
<div className="text">You can save this page so you can come back to it later if you need the information again. Next, you can check if the database is working properly using PHP.</div><br/>
|
||
|
||
<div className="head">Checking Database Connection with PHP (Optional)</div>
|
||
<div className="text">To see if PHP is correctly connecting to MariaDB and performing database operations, you can create a test table with some example data. Afterward, you can ask PHP to retrieve and display the contents of that table.</div><br/>
|
||
<div className="text">Begin by accessing the MariaDB console using the username and password you set up earlier:</div>
|
||
|
||
|
||
<CodeBlock code={`mysql -u example_user -p`} /><br/>
|
||
|
||
<div className="text">In the MariaDB console, enter the following command to create a table called "todo_list" in your "example_database":</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">The MariaDB console will let you know about any changes made to your table after each update:</div>
|
||
<CodeBlock code={`Query OK, 0 rows affected (0.00 sec)`} /><br/>
|
||
|
||
<div className="text">Add some information into the test table. You can use the following command multiple times, changing the values each time, to fill up your test table:</div>
|
||
|
||
<CodeBlock code={`INSERT INTO example_database.todo_list (content) VALUES ("My first important item");`} /><br/>
|
||
|
||
<div className="text">To make sure that the information was saved correctly in your table, execute the following command:</div>
|
||
|
||
<CodeBlock code={`SELECT * FROM example_database.todo_list;`} /><br/>
|
||
|
||
<div className="text">Here's what an example of the result might look like:</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 checked that your test table has the right information, you can leave the MariaDB console:</div>
|
||
|
||
<CodeBlock code={`exit`} /><br/>
|
||
|
||
<div className="text">Now, let's make a PHP script to connect to MariaDB and get your data. Open a new PHP file in your web folder using your favorite text editor. If you like, you can use vi as shown in this example:</div>
|
||
|
||
<CodeBlock code={`vi /var/www/html/todo_list.php`} /><br/>
|
||
|
||
<div className="text">To add the following content, follow these steps in the vi text editor:</div>
|
||
|
||
<div className="text"><ol><li>Press the "I" key to enter insert mode.</li><li>Replace "example_user" and "password" with your actual database username and password.</li></ol></div>
|
||
|
||
<div className="text">After making these changes, the content will be added to the file.</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">To save and exit the file in vi:Press the "Esc" key to exit insert mode. Type ":x" (without quotes) and press Enter.</div>
|
||
<div className="text">Once you've saved and closed the file, you can view this page in your web browser by going to 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">Here's what the web page might look like, showing the information you added to your test table:</div><br/>
|
||
|
||
|
||
<img src={todo} alt="todo_list" /><br/>
|
||
|
||
|
||
<div className="con">Conclusion:</div>
|
||
|
||
<div className="text">In this guide, you've created a strong base for hosting PHP websites and apps for your visitors, using Apache as your web server. You've configured Apache to work with PHP and set up a MariaDB database to store your website's information.</div> <br/>
|
||
|
||
|
||
<div className="text"><strong>Ready to start building your own website? Let's get started!</strong></div> |