{"id":26608,"date":"2024-06-18T14:42:46","date_gmt":"2024-06-18T07:42:46","guid":{"rendered":"https:\/\/tom.ji42.com\/?p=26608"},"modified":"2024-06-18T14:42:46","modified_gmt":"2024-06-18T07:42:46","slug":"how-to-install-linux-apache-mysql-php-lamp-stack-on-centos-7","status":"publish","type":"post","link":"https:\/\/tom.tomwork.net\/?p=26608","title":{"rendered":"How To Install Linux, Apache, MySQL, PHP (LAMP) stack On CentOS 7"},"content":{"rendered":"<h3 id=\"introduction\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-install-linux-apache-mysql-php-lamp-stack-on-centos-7#introduction\">Introduction<\/a><\/h3>\n<p>A \u201cLAMP\u201d stack is a group of open-source software that is typically installed together to enable a server to host dynamic websites and web apps. This term is an acronym which represents the\u00a0<strong>L<\/strong>inux operating system, with the\u00a0<strong>A<\/strong>pache web server. The site data is typically stored in a\u00a0<strong>M<\/strong>ySQL database and dynamic content is processed by\u00a0<strong>P<\/strong>HP.<\/p>\n<p>On most Linux systems, you can install MySQL by downloading the\u00a0<code>mysql-server<\/code>\u00a0package from your system\u2019s default package management repositories. However, on CentOS 7 the\u00a0<code>mysql-server<\/code>\u00a0package 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 to install a LAMP stack that consists of\u00a0<strong>L<\/strong>inux,\u00a0<strong>A<\/strong>pache,\u00a0<strong>M<\/strong>ariaDB, and\u00a0<strong>P<\/strong>HP on a CentOS 7 server.<!--more--><\/p>\n<h2 id=\"prerequisites\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-install-linux-apache-mysql-php-lamp-stack-on-centos-7#prerequisites\">Prerequisites<\/a><\/h2>\n<p>Before you begin with this guide, you should have a separate, non-root user account set up on your server. You can learn how to do this by following our\u00a0<a href=\"https:\/\/www.digitalocean.com\/community\/articles\/initial-server-setup-with-centos-7\">initial server setup for CentOS 7<\/a>\u00a0tutorial.<\/p>\n<h2 id=\"step-1-installing-the-apache-web-server\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-install-linux-apache-mysql-php-lamp-stack-on-centos-7#step-1-installing-the-apache-web-server\">Step 1 \u2014 Installing the Apache Web Server<\/a><\/h2>\n<p>Apache is a popular open-source web server that is used to display web pages to visitors. You can configure it to serve PHP pages.<\/p>\n<p>Install Apache using CentOS\u2019s package manager,\u00a0<code>yum<\/code>. A package manager allows you to install most software from a repository maintained by CentOS.<\/p>\n<p>Type this command in your terminal to install the\u00a0<code>httpd<\/code>\u00a0Apache package:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\"><span class=\"token function\">sudo<\/span> yum <span class=\"token function\">install<\/span> httpd<\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>When prompted, enter\u00a0<code>Y<\/code>\u00a0to confirm the Apache installation. Once the installation is complete, start your Apache server with this command:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\"><span class=\"token function\">sudo<\/span> systemctl start httpd<\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>You can test if your server is running by entering your public IP address or your domain name in your web browser.<\/p>\n<div class=\"callout note\">\n<p><strong>Note<\/strong>: If you are using DigitalOcean as DNS hosting provider, you can check our\u00a0<a href=\"https:\/\/www.digitalocean.com\/docs\/networking\/dns\/how-to\/manage-records\/#a-records\">product docs<\/a>\u00a0for detailed instructions on how to set up a new domain name and point it to your server.<\/p>\n<\/div>\n<p>If you do not have a domain name pointed at your server or do not know your server\u2019s public IP address, you can find it by running the following command:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\"><span class=\"token function\">ip<\/span> addr show eth0 <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> inet <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#8216;{ print $2; }&#8217;<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">sed<\/span> <span class=\"token string\">&#8216;s\/\\\/.*$\/\/&#8217;<\/span><\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>This will print out a few different addresses. You can try each of them in your web browser.<\/p>\n<p>An alternative method is to use an outside party to tell you how it sees your server. You can do this by asking a specific server what your IP address is with this command:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\"><span class=\"token function\">curl<\/span> http:\/\/icanhazip.com<\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>Whichever method you choose, type in your IP address into your web browser to verify that your server is running.<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<pre class=\"\"><code>http:\/\/<mark>your_server_IP_address<\/mark>\r\n<\/code><\/pre>\n<p>You will be presented with the default CentOS 7 Apache landing page:<img decoding=\"async\" src=\"https:\/\/assets.digitalocean.com\/articles\/lamp_c7\/default_apache.png\" alt=\"CentOS 7 Apache\u2019s default web server status landing page after initial installation.\" \/><\/p>\n<p>You can enable Apache to start on boot with:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\"><span class=\"token function\">sudo<\/span> systemctl <span class=\"token builtin class-name\">enable<\/span> httpd.service<\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<h2 id=\"step-2-installing-mysql-mariadb\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-install-linux-apache-mysql-php-lamp-stack-on-centos-7#step-2-installing-mysql-mariadb\">Step 2 \u2014 Installing MySQL (MariaDB)<\/a><\/h2>\n<p>With your web server up and running, you can install MariaDB. It will organize and provide access to databases where your site can store information.<\/p>\n<p>To install the MariaDB software package, run:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\"><span class=\"token function\">sudo<\/span> yum <span class=\"token function\">install<\/span> mariadb-server<\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>When the installation is complete, start MariaDB:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\"><span class=\"token function\">sudo<\/span> systemctl start mariadb<\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>You can enable MariaDB to start on boot with this command:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\"><span class=\"token function\">sudo<\/span> systemctl <span class=\"token builtin class-name\">enable<\/span> mariadb.service<\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>To improve the security of your database server, it\u2019s recommended that you run a security script that comes pre-installed with MariaDB. This script will remove some insecure default settings and lock down access to your database system.<\/p>\n<p>Start the interactive script by running:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\"><span class=\"token function\">sudo<\/span> mysql_secure_installation<\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>This script will take you through a series of prompts where you can make some changes to your MariaDB setup. The first prompt will ask you to enter the current\u00a0<strong>database root<\/strong>\u00a0password. This is not to be confused with the\u00a0<strong>system root<\/strong>\u00a0user. The\u00a0<strong>database root<\/strong>\u00a0user is an administrative user with full privileges over the database system. Because you just installed MariaDB and haven\u2019t made any configuration changes, this password will be blank. Press\u00a0<code>ENTER<\/code>\u00a0at the prompt.<\/p>\n<p>The next prompt asks you whether you\u2019d like to set up a\u00a0<strong>database root<\/strong>\u00a0password. Type\u00a0<code>N<\/code>\u00a0and then press\u00a0<code>ENTER<\/code>.<\/p>\n<p>From there, you can press\u00a0<code>Y<\/code>, and then\u00a0<code>ENTER<\/code>, to accept the defaults for all the subsequent questions. This will remove anonymous users and the test database, disable remote\u00a0<strong>root<\/strong>\u00a0login, and load these new rules so that the server immediately respects the changes you have made.<\/p>\n<p>When you\u2019re finished, log in to the MariaDB console by entering:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\"><span class=\"token function\">sudo<\/span> mysql<\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>This connects you to the MariaDB server as the administrative database user\u00a0<strong>root<\/strong>:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<pre><code><\/code><\/pre>\n<div class=\"secondary-code-label\" title=\"Output\">Output<\/div>\n<pre><code>Welcome to the MariaDB monitor.  Commands end with ; or \\g.\r\nYour MariaDB connection id is 12\r\nServer version: 5.5.68-MariaDB MariaDB Server\r\n\r\nCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.\r\n\r\nType 'help;' or '\\h' for help. Type '\\c' to clear the current input statement. \r\n<\/code><\/pre>\n<p>For increased security, it\u2019s best to have dedicated user accounts with less expansive privileges set up for every database. This is especially important if you plan on having multiple databases hosted on your server.<\/p>\n<p>To demonstrate such a setup, create a database named\u00a0<code>example_database<\/code>\u00a0and a user named\u00a0<strong>example_user<\/strong>. You can replace these names with different values.<\/p>\n<p>Run the following command from your MariaDB console to create a new database:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<ol>\n<li style=\"box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;\" data-prefix=\"MariaDB [(none)]&gt;\">CREATE DATABASE <mark>example_database<\/mark><span class=\"token punctuation\">;<\/span><\/li>\n<\/ol>\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>You can create a new user and grant them full privileges on the custom database you\u2019ve just created. The following command defines this user\u2019s password as\u00a0<code><mark>password<\/mark><\/code>, but you should replace this value with a secure password:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<ol>\n<li style=\"box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;\" data-prefix=\"MariaDB [(none)]&gt;\">GRANT ALL ON <mark>example_database<\/mark>.* TO <span class=\"token string\">&#8216;<mark>example_user<\/mark>&#8216;<\/span>@<span class=\"token string\">&#8216;localhost&#8217;<\/span> IDENTIFIED BY <span class=\"token string\">&#8216;<mark>password<\/mark>&#8216;<\/span> WITH GRANT OPTION<span class=\"token punctuation\">;<\/span><\/li>\n<\/ol>\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>This command gives the\u00a0<strong>example_user<\/strong>\u00a0user full privileges over the\u00a0<code>example_database<\/code>\u00a0database, while preventing this user from creating or modifying other databases on your server.<\/p>\n<p>Use the\u00a0<code>FLUSH<\/code>\u00a0statement to reload and save the privileges you just granted to\u00a0<strong>example_user<\/strong>:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<ol>\n<li style=\"box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;\" data-prefix=\"MariaDB [(none)]&gt;\">FLUSH PRIVILEGES<span class=\"token punctuation\">;<\/span><\/li>\n<\/ol>\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>Exit the MariaDB shell:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<ol>\n<li style=\"box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;\" data-prefix=\"MariaDB [(none)]&gt;\"><span class=\"token builtin class-name\">exit<\/span><\/li>\n<\/ol>\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>You can test if the new user has the proper permissions by logging in to the MariaDB console again, but using the\u00a0<strong>example_user<\/strong>\u00a0credentials you created above:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\">mysql <span class=\"token parameter variable\">-u<\/span> <mark>example_user<\/mark> <span class=\"token parameter variable\">-p<\/span><\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>Note the\u00a0<code>-p<\/code>\u00a0flag in this command, which will prompt you for the password you chose when creating the\u00a0<strong>example_user<\/strong>\u00a0user. After logging in to the MariaDB console, confirm that you have access to the\u00a0<code>example_database<\/code>\u00a0database with this statement:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<ol>\n<li style=\"box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;\" data-prefix=\"MariaDB [(none)]&gt;\">SHOW DATABASES<span class=\"token punctuation\">;<\/span><\/li>\n<\/ol>\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>Your\u00a0<code>example_database<\/code>\u00a0should be listed in the output:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<pre><code><\/code><\/pre>\n<div class=\"secondary-code-label\" title=\"Output\">Output<\/div>\n<pre><code>+--------------------+\r\n| Database           |\r\n+--------------------+\r\n| <mark>example_database<\/mark>   |\r\n| information_schema |\r\n+--------------------+\r\n2 rows in set (0.000 sec)\r\n<\/code><\/pre>\n<p>To exit the MariaDB shell, type:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<ol>\n<li style=\"box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;\" data-prefix=\"MariaDB [(none)]&gt;\"><span class=\"token builtin class-name\">exit<\/span><\/li>\n<\/ol>\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>Your database system is set up and you can move on to installing PHP.<\/p>\n<h2 id=\"step-3-installing-php\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-install-linux-apache-mysql-php-lamp-stack-on-centos-7#step-3-installing-php\">Step 3 \u2014 Installing PHP<\/a><\/h2>\n<p>You have Apache installed to serve your content and MariaDB to store and manage your data. PHP will process code to display dynamic content to the user. In addition to the\u00a0<code>php<\/code>\u00a0package, you\u2019ll need\u00a0<code>php-mysql<\/code>, a PHP module that allows PHP to communicate with MySQL-based databases. Core PHP packages will automatically be installed as dependencies.<\/p>\n<p>Use this command to install the\u00a0<code>php<\/code>\u00a0and\u00a0<code>php-mysql<\/code>\u00a0packages with\u00a0<code>yum<\/code>:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\"><span class=\"token function\">sudo<\/span> yum <span class=\"token function\">install<\/span> php php-mysql<\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>Restart the Apache web server to enable the PHP module you installed:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\"><span class=\"token function\">sudo<\/span> systemctl restart httpd.service<\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>Your server is now configured with all the components necessary for your LAMP stack application. The next step is to test your configuration to ensure that everything is working harmoniously.<\/p>\n<h2 id=\"step-4-testing-php-on-your-apache-web-server\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-install-linux-apache-mysql-php-lamp-stack-on-centos-7#step-4-testing-php-on-your-apache-web-server\">Step 4 \u2014 Testing PHP on your Apache Web Server<\/a><\/h2>\n<p>The default Apache installation on CentOS 7 will create a document root located at\u00a0<code>\/var\/www\/html<\/code>. You don\u2019t need to make any changes to Apache\u2019s default settings in order for PHP to work correctly within your web server.<\/p>\n<p>You can, however, make an adjustment to change the default permission settings on your Apache document root folder. This allows you to create and modify files in that directory with your regular system user without the need to prefix each command with\u00a0<code>sudo<\/code>.<\/p>\n<p>The following command will change the ownership of the default Apache document root to a user and group called\u00a0<code><mark>sammy<\/mark><\/code>, so be sure to replace the highlighted username and group in this command to reflect your system\u2019s username and group:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\"><span class=\"token function\">sudo<\/span> <span class=\"token function\">chown<\/span> <span class=\"token parameter variable\">-R<\/span> <mark>sammy<\/mark>.<mark>sammy<\/mark> \/var\/www\/html\/<\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>You can create a PHP test file to ensure the web server works as expected. Use your preferred text editor to create this file. The following examples use the default\u00a0<code>vi<\/code>\u00a0text editor in CentOS 7.<\/p>\n<p>Create a PHP file called\u00a0<code>info.php<\/code>\u00a0at the\u00a0<code>var\/www\/html<\/code>\u00a0directory:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\"><span class=\"token function\">vi<\/span> \/var\/www\/html\/info.php<\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>This opens a blank PHP file at the\u00a0<code>\/var\/www\/html<\/code>\u00a0directory. Press\u00a0<code>I<\/code>\u00a0to enter into\u00a0<code>INSERT<\/code>\u00a0mode in the\u00a0<code>vi<\/code>\u00a0editor. This allows you to type and make changes within the text editor. Type the following PHP code:<\/p>\n<div class=\"code-label\" title=\"\/var\/www\/html\/info.php\">\/var\/www\/html\/info.php<\/div>\n<div class=\"code-toolbar\">\n<pre class=\"language-php\"><code><span class=\"token php language-php\"><span class=\"token delimiter important\">&lt;?php<\/span> <span class=\"token function\">phpinfo<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token delimiter important\">?&gt;<\/span><\/span>\r\n<\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>This PHP code displays information about the PHP environment running on your server. When you are finished with making your changes to this file, press the\u00a0<code>ESC<\/code>\u00a0key to exit out of\u00a0<code>INSERT<\/code>\u00a0mode in\u00a0<code>vi<\/code>. Type\u00a0<code>:x<\/code>\u00a0\u2013 a semicolon and the letter x in lowercase \u2013 to save and close the file.<\/p>\n<p>You can test whether your web server correctly displays PHP content by going to your server\u2019s public IP address, followed by\u00a0<code>\/info.php<\/code>:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<pre><code>http:\/\/<mark>your_server_IP_address<\/mark>\/info.php\r\n<\/code><\/pre>\n<p>A web page, similar to the one below, will be displayed in your browser:<img decoding=\"async\" src=\"https:\/\/assets.digitalocean.com\/articles\/lamp_c7\/default_php.png\" alt=\"CentOS 7 default PHP information web page containing information about the current PHP configuration\" \/><\/p>\n<p>This page gives you information about your server from the perspective of PHP. It is useful for debugging and ensuring that your settings are being applied correctly. After checking the relevant information about your PHP server, it\u2019s best to remove this file as it contains sensitive information about your PHP environment and your CentOS server.<\/p>\n<p>You can use\u00a0<code>rm<\/code>\u00a0to remove this file:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\"><span class=\"token function\">rm<\/span> \/var\/www\/html\/<mark>info.php<\/mark><\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>You can always recreate this page if you need to access the information again later. Next, you can test the database connection utilizing PHP.<\/p>\n<h2 id=\"step-5-testing-database-connection-with-php-optional\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-install-linux-apache-mysql-php-lamp-stack-on-centos-7#step-5-testing-database-connection-with-php-optional\">Step 5 \u2013 Testing Database Connection with PHP (Optional)<\/a><\/h2>\n<p>You can test if PHP connects to MariaDB and executes database queries by creating a test table with some test data. You can query for its contents from a PHP script.<\/p>\n<p>First, connect to the MariaDB console with the database user you created in\u00a0<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-install-linux-apache-mysql-php-lamp-stack-on-centos-7#step-2-%E2%80%94-installing-mariadb\">Step 2<\/a>\u00a0of this guide:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\">mysql <span class=\"token parameter variable\">-u<\/span> <mark>example_user<\/mark> <span class=\"token parameter variable\">-p<\/span><\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>From the MariaDB console, run the following statement to create a table named\u00a0<code>todo_list<\/code>\u00a0within your\u00a0<code>example_database<\/code>:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<ol>\n<li style=\"box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;\" data-prefix=\"MariaDB [(none)]&gt;\">CREATE TABLE <mark>example_database<\/mark>.<mark>todo_list<\/mark> <span class=\"token punctuation\">(<\/span><\/li>\n<li style=\"box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;\" data-prefix=\"MariaDB [(none)]&gt;\">item_id INT AUTO_INCREMENT,<\/li>\n<li style=\"box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;\" data-prefix=\"MariaDB [(none)]&gt;\">content VARCHAR<span class=\"token punctuation\">(<\/span><span class=\"token number\">255<\/span><span class=\"token punctuation\">)<\/span>,<\/li>\n<li style=\"box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;\" data-prefix=\"MariaDB [(none)]&gt;\">PRIMARY KEY<span class=\"token punctuation\">(<\/span>item_id<span class=\"token punctuation\">)<\/span><\/li>\n<li style=\"box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;\" data-prefix=\"MariaDB [(none)]&gt;\"><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/li>\n<\/ol>\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>The MariaDB console will notify you about changes to your table after each edit.<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<pre><code>Query OK, 0 rows affected (0.00 sec)\r\n<\/code><\/pre>\n<p>Insert a few rows of content in the test table. You can repeat the next command a few times, using different values, to populate your test table:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<ol>\n<li style=\"box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;\" data-prefix=\"MariaDB [(none)]&gt;\">INSERT INTO <mark>example_database<\/mark>.<mark>todo_list<\/mark> <span class=\"token punctuation\">(<\/span>content<span class=\"token punctuation\">)<\/span> VALUES <span class=\"token punctuation\">(<\/span><span class=\"token string\">&#8220;<mark>My first important item<\/mark>&#8220;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/li>\n<\/ol>\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>To confirm that the data was successfully saved to your table, run:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<ol>\n<li style=\"box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;\" data-prefix=\"MariaDB [(none)]&gt;\">SELECT * FROM <mark>example_database<\/mark>.<mark>todo_list<\/mark><span class=\"token punctuation\">;<\/span><\/li>\n<\/ol>\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>Below is an example of the output:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<pre><code><\/code><\/pre>\n<div class=\"secondary-code-label\" title=\"Output\">Output<\/div>\n<pre><code>+---------+--------------------------+\r\n| item_id | content                  |\r\n+---------+--------------------------+\r\n|       1 | My first important item  |\r\n|       2 | My second important item |\r\n|       3 | My third important item  |\r\n|       4 | and this one more thing  |\r\n+---------+--------------------------+\r\n4 rows in set (0.000 sec)\r\n<\/code><\/pre>\n<p>After confirming that you have valid data in your test table, you can exit the MariaDB console:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<ol>\n<li style=\"box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre;\" data-prefix=\"MariaDB [(none)]&gt;\"><span class=\"token builtin class-name\">exit<\/span><\/li>\n<\/ol>\n<pre class=\"prefixed custom_prefix language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>Now you can create the PHP script that will connect to MariaDB and query for your content. Create a new PHP file in your custom web root directory using your preferred editor. This example uses\u00a0<code>vi<\/code>:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<div class=\"code-toolbar\">\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\"><span class=\"token function\">vi<\/span> \/var\/www\/html\/<mark>todo_list.php<\/mark><\/li>\n<\/ol>\n<pre class=\"prefixed command language-bash\"><code><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>Add the following content by pressing\u00a0<code>I<\/code>\u00a0in the\u00a0<code>vi<\/code>\u00a0text editor, remembering to replace the\u00a0<code><mark>example_user<\/mark><\/code>\u00a0and\u00a0<code><mark>password<\/mark><\/code>\u00a0with your own:<\/p>\n<div class=\"code-label\" title=\"\/var\/www\/html\/todo_list.php\">\/var\/www\/html\/todo_list.php<\/div>\n<div class=\"code-toolbar\">\n<pre class=\"language-php\"><code><span class=\"token php language-php\"><span class=\"token delimiter important\">&lt;?php<\/span>\r\n<span class=\"token variable\">$user<\/span> <span class=\"token operator\">=<\/span> <span class=\"token string double-quoted-string\">\"<mark>example_user<\/mark>\"<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token variable\">$password<\/span> <span class=\"token operator\">=<\/span> <span class=\"token string double-quoted-string\">\"<mark>password<\/mark>\"<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token variable\">$database<\/span> <span class=\"token operator\">=<\/span> <span class=\"token string double-quoted-string\">\"<mark>example_database<\/mark>\"<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token variable\">$table<\/span> <span class=\"token operator\">=<\/span> <span class=\"token string double-quoted-string\">\"<mark>todo_list<\/mark>\"<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n<span class=\"token keyword\">try<\/span> <span class=\"token punctuation\">{<\/span>\r\n  <span class=\"token variable\">$db<\/span> <span class=\"token operator\">=<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">PDO<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string double-quoted-string\">\"mysql:host=localhost;dbname=<span class=\"token interpolation\"><span class=\"token variable\">$database<\/span><\/span>\"<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token variable\">$user<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token variable\">$password<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n  <span class=\"token keyword\">echo<\/span> <span class=\"token string double-quoted-string\">\"&lt;h2&gt;TODO&lt;\/h2&gt;&lt;ol&gt;\"<\/span><span class=\"token punctuation\">;<\/span> \r\n  <span class=\"token keyword\">foreach<\/span><span class=\"token punctuation\">(<\/span><span class=\"token variable\">$db<\/span><span class=\"token operator\">-&gt;<\/span><span class=\"token function\">query<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string double-quoted-string\">\"SELECT content FROM <span class=\"token interpolation\"><span class=\"token variable\">$table<\/span><\/span>\"<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> <span class=\"token variable\">$row<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n    <span class=\"token keyword\">echo<\/span> <span class=\"token string double-quoted-string\">\"&lt;li&gt;\"<\/span> <span class=\"token operator\">.<\/span> <span class=\"token variable\">$row<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string single-quoted-string\">'content'<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">.<\/span> <span class=\"token string double-quoted-string\">\"&lt;\/li&gt;\"<\/span><span class=\"token punctuation\">;<\/span>\r\n  <span class=\"token punctuation\">}<\/span>\r\n  <span class=\"token keyword\">echo<\/span> <span class=\"token string double-quoted-string\">\"&lt;\/ol&gt;\"<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">catch<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">PDOException<\/span> <span class=\"token variable\">$e<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n    <span class=\"token keyword\">print<\/span> <span class=\"token string double-quoted-string\">\"Error!: \"<\/span> <span class=\"token operator\">.<\/span> <span class=\"token variable\">$e<\/span><span class=\"token operator\">-&gt;<\/span><span class=\"token function\">getMessage<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">.<\/span> <span class=\"token string double-quoted-string\">\"&lt;br\/&gt;\"<\/span><span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token keyword\">die<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<\/span><\/code><\/pre>\n<div class=\"toolbar\">\n<div class=\"toolbar-item\"><button>Copy<\/button><\/div>\n<\/div>\n<\/div>\n<p>Save and close the file when you\u2019re done editing by pressing\u00a0<code>ESC<\/code>, followed by typing\u00a0<code>:x<\/code>\u00a0in\u00a0<code>vi<\/code>.<\/p>\n<p>You can now access this page in your web browser by visiting your server\u2019s host name or public IP address, followed by\u00a0<code>\/todo_list.php<\/code>:<\/p>\n<div class=\"code-label\" title=\"\"><\/div>\n<pre><code>http:\/\/<mark>server_host_or_IP<\/mark>\/todo_list.php\r\n<\/code><\/pre>\n<p>Below is an example of the web page, revealing the content you\u2019ve inserted in your test table:<img decoding=\"async\" src=\"https:\/\/assets.digitalocean.com\/articles\/lemp_debian10\/todo_list.png\" alt=\"Example of the PHP web page outputting the data that was inserted into the test table in MariaDB\" \/><\/p>\n<h2 id=\"conclusion\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-install-linux-apache-mysql-php-lamp-stack-on-centos-7#conclusion\">Conclusion<\/a><\/h2>\n<p>In this guide, you\u2019ve built a flexible foundation for serving PHP websites and applications to your visitors, using Apache as a web server. You\u2019ve set up Apache to handle PHP requests, and set up a MariaDB database to store your website\u2019s data.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction A \u201cLAMP\u201d stack is a group of open-source software that is typically installed together to enable a server to host dynamic websites and web apps. This term is an acronym which represents the\u00a0Linux operating system, with the\u00a0Apache web server. The site data is typically stored in a\u00a0MySQL database and dynamic content is processed by\u00a0PHP. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[13],"tags":[],"class_list":["post-26608","post","type-post","status-publish","format-standard","hentry","category-13"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6cOVM-6Va","_links":{"self":[{"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/posts\/26608","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26608"}],"version-history":[{"count":1,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/posts\/26608\/revisions"}],"predecessor-version":[{"id":26609,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/posts\/26608\/revisions\/26609"}],"wp:attachment":[{"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}