A “LAMP” stack is a group of open-source software that is typically installed together khổng lồ enable a vps to host dynamic websites và web apps. This term is an acronym which represents the Linux operating system, with the Apache web server. The site data is typically stored in a MySQL database và dynamic content is processed by PHP.
Bạn đang xem: How to install the lamp stack on centos 7
On most Linux systems, you can install MySQL by downloading the mysql-server package from your system’s mặc định package management repositories. However, on CentOS 7 the mysql-server package will actually install MariaDB, a community-developed fork of the MySQL relational database management system which works as a drop-in replacement for MySQL. Thus, this tutorial will outline how lớn install a LAMP stack that consists of Linux, Apache, MariaDB, & PHP on a CentOS 7 server.
Before you begin with this guide, you should have a separate, non-root user trương mục set up on your server. You can learn how to bởi this by following our initial server thiết đặt for CentOS 7 tutorial.
Step 1 — Installing the Apache website Server
Apache is a popular open-source web hệ thống that is used to lớn display web pages khổng lồ visitors. You can configure it to serve PHP pages.
Install Apache using CentOS’s package manager, yum. A package manager allows you lớn install most software from a repository maintained by CentOS.
Type this command in your terminal to install the httpd Apache package:
sudo yum install httpdWhen prompted, enter Y to confirm the Apache installation.Once the installation is complete, start your Apache server with this command:
sudo systemctl start httpdYou can demo if your server is running by entering your public IP address or your domain name in your web browser.
Note: If you are using darkedeneurope.com as DNS hosting provider, you can check our hàng hóa docs for detailed instructions on how lớn set up a new domain name name & point it lớn your server.
If you bởi not have a domain name name pointed at your hệ thống or vày not know your server’s public IP address, you can find it by running the following command:
ip addr show eth0 | grep inet | awk " print $2; " | sed "s//.*$//"This will print out a few different addresses. You can try each of them in your web browser.
An alternative method is to use an outside các buổi tiệc nhỏ to tell you how it sees your server. You can vì chưng this by asking a specific hệ thống what your IP address is with this command:
curl http://icanhazip.comWhichever method you choose, type in your IP address into your web browser to verify that your vps is running.
http://your_server_IP_addressYou will be presented with the default CentOS 7 Apache landing page:
You can enable Apache to lớn start on boot with:
sudo systemctl enable httpd.service
Step 2 — Installing MySQL (MariaDB)With your web vps up and running, you can install MariaDB. It will organize & provide access to databases where your site can store information.
To install the MariaDB software package, run:
sudo yum install mariadb-serverWhen the installation is complete, start MariaDB:
sudo systemctl start mariadbYou can enable MariaDB khổng lồ start on boot with this command:
sudo systemctl enable mariadb.serviceTo improve the security of your database server, it’s recommended that you run a security script that comes pre-installed with MariaDB. This script will remove some insecure mặc định settings & lock down access khổng lồ your database system.
Start the interactive script by running:
sudo mysql_secure_installationThis script will take you through a series of prompts where you can make some changes lớn your MariaDB setup. The first prompt will ask you to enter the current database root password. This is not lớn be confused with the system root user. The database root user is an administrative user with full privileges over the database system. Because you just installed MariaDB & haven’t made any configuration changes, this password will be blank. Press ENTER at the prompt.
The next prompt asks you whether you’d like to phối up a database root password. Type N và then press ENTER.
From there, you can press Y, và then ENTER, khổng lồ accept the defaults for all the subsequent questions. This will remove anonymous users and the demo database, disable remote root login, và load these new rules so that the vps immediately respects the changes you have made.
When you’re finished, log in khổng lồ the MariaDB console by entering:
sudo mysqlThis connects you khổng lồ the MariaDB vps as the administrative database user root:
To demonstrate such a setup, create a database named example_database and a user named example_user. You can replace these names with different values.
Run the following command from your MariaDB console to lớn create a new database:
">CREATE DATABASE example_database;You can create a new user và grant them full privileges on the custom database you’ve just created. The following command defines this user’s password as password, but you should replace this value with a secure password:
">GRANT ALL ON example_database.* lớn "example_user"
"localhost" IDENTIFIED BY "password" WITH GRANT OPTION;This command gives the example_user user full privileges over the example_database database, while preventing this user from creating or modifying other databases on your server.
Use the FLUSH statement khổng lồ reload and save the privileges you just granted lớn example_user:
">FLUSH PRIVILEGES;Exit the MariaDB shell:
">exitYou can test if the new user has the proper permissions by logging in to lớn the MariaDB console again, but using the example_user credentials you created above:
mysql -u example_user -pNote the -p flag in this command, which will prompt you for the password you chose when creating the example_user user. After logging in khổng lồ the MariaDB console, confirm that you have access to lớn the example_database database with this statement:
">SHOW DATABASES;Your example_database should be listed in the output:
+--------------------+| Database |+--------------------+| example_database || information_schema |+--------------------+2 rows in set (0.000 sec)To exit the MariaDB shell, type:
">exitYour database system is mix up và you can move on to lớn installing PHP.
Step 3 — Installing PHP
You have Apache installed khổng lồ serve your content and MariaDB khổng lồ store & manage your data. PHP will process code lớn display dynamic content to the user. In addition lớn the php package, you’ll need php-mysql, a PHP module that allows PHP to lớn communicate with MySQL-based databases. Core PHP packages will automatically be installed as dependencies.
Use this command to install the php & php-mysql packages with yum:
sudo yum install php php-mysqlRestart the Apache web hệ thống to enable the PHP module you installed:
sudo systemctl restart httpd.serviceYour hệ thống is now configured with all the components necessary for your LAMP stack application. The next step is to demo your configuration lớn ensure that everything is working harmoniously.
Step 4 — Testing PHP on your Apache web Server
The default Apache installation on CentOS 7 will create a document root located at /var/www/html. You don’t need khổng lồ make any changes to Apache’s default settings in order for PHP to work correctly within your web server.
You can, however, make an adjustment khổng lồ change the default permission settings on your Apache document root folder. This allows you to lớn create & modify files in that directory with your regular system user without the need khổng lồ prefix each command with sudo.
The following command will change the ownership of the mặc định Apache document root lớn a user & group called sammy, so be sure khổng lồ replace the highlighted username and group in this command to reflect your system’s username và group:
sudo chown -R sammy.sammy /var/www/html/You can create a PHP test file khổng lồ ensure the web vps works as expected. Use your preferred text editor lớn create this file. The following examples use the default vi text editor in CentOS 7.
Create a PHP file called info.php at the var/www/html directory:
vi /var/www/html/info.phpThis opens a blank PHP file at the /var/www/html directory. Press I to lớn enter into INSERT mode in the vi editor. This allows you to lớn type and make changes within the text editor. Type the following PHP code:
phpinfo(); ?>This PHP code displays information about the PHP environment running on your server. When you are finished with making your changes khổng lồ this file, press the ESC key lớn exit out of INSERT mode in vi. Type :x – a semicolon và the letter x in lowercase – to save & close the file.
You can thử nghiệm whether your web server correctly displays PHP nội dung by going lớn your server’s public IP address, followed by /info.php:
http://your_server_IP_address/info.phpA website page, similar khổng lồ the one below, will be displayed in your browser:
This page gives you information about your server from the perspective of PHP. It is useful for debugging & ensuring that your settings are being applied correctly. After checking the relevant information about your PHP server, it’s best lớn remove this file as it contains sensitive information about your PHP environment & your CentOS server.
You can use rm to remove this file:
rm /var/www/html/info.phpYou can always recreate this page if you need lớn access the information again later. Next, you can demo the database connection utilizing PHP.
Step 5 – Testing Database Connection with PHP (Optional)
You can chạy thử if PHP connects to MariaDB and executes database queries by creating a test table with some kiểm tra data. You can query for its contents from a PHP script.
First, connect to the MariaDB console with the database user you created in Step 2 of this guide:
mysql -u example_user -pFrom the MariaDB console, run the following statement to create a table named todo_list within your example_database:
">CREATE TABLE example_database.todo_list (">item_id INT AUTO_INCREMENT,">content VARCHAR(255),">PRIMARY KEY(item_id)">);The MariaDB console will notify you about changes to your table after each edit.
Query OK, 0 rows affected (0.00 sec)Insert a few rows of content in the kiểm tra table. You can repeat the next command a few times, using different values, to populate your test table:
">INSERT INTO example_database.todo_list (content) VALUES ("My first important item");To confirm that the data was successfully saved khổng lồ your table, run:
">SELECT * FROM example_database.todo_list;Below is an example of the output:
+---------+--------------------------+| item_id | nội dung |+---------+--------------------------+| 1 | My first important thành tựu || 2 | My second important thành quả || 3 | My third important tác phẩm || 4 | and this one more thing |+---------+--------------------------+4 rows in phối (0.000 sec)After confirming that you have valid data in your demo table, you can exit the MariaDB console:
">exitNow you can create the PHP script that will connect to MariaDB và query for your content. Create a new PHP file in your custom website root directory using your preferred editor. This example uses vi:
vi /var/www/html/todo_list.phpAdd the following content by pressing I in the vi text editor, remembering lớn replace the example_user và password with your own:
$user = "example_user";$password = "password";$database = "example_database";$table = "todo_list";try $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password); echo "
TODO"; foreach($db->query("SELECT nội dung FROM $table") as $row) echo "" . $row<"content"> . ""; echo ""; catch (PDOException $e) print "Error!: " . $e->getMessage() . ""; die();Save & close the tệp tin when you’re done editing by pressing ESC, followed by typing :x in vi.
You can now access this page in your web browser by visiting your server’s host name or public IP address, followed by /todo_list.php:
http://server_host_or_IP/todo_list.phpBelow is an example of the website page, revealing the content you’ve inserted in your demo table:
In this guide, you’ve built a flexible foundation for serving PHP websites and applications to your visitors, using Apache as a web server. You’ve phối up Apache lớn handle PHP requests, & set up a MariaDB database lớn store your website’s data.