{"id":2911,"date":"2018-02-20T16:58:32","date_gmt":"2018-02-20T13:58:32","guid":{"rendered":"https:\/\/www.howtoforge.com\/tutorial\/centos-lamp-server-apache-mysql-php\/"},"modified":"2018-02-20T16:58:32","modified_gmt":"2018-02-20T13:58:32","slug":"how-to-install-apache-php-7-2-and-mysql-on-centos-7-4-lamp","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/how-to-install-apache-php-7-2-and-mysql-on-centos-7-4-lamp\/","title":{"rendered":"How to install Apache, PHP 7.2 and MySQL on CentOS 7.4 (LAMP)"},"content":{"rendered":"<p>This tutorial shows how\u00a0to install an Apache web server on CentOS 7 server with PHP (mod_php) and MySQL database. The acronym LAMP is short for <strong>L<\/strong>inux, <strong>A<\/strong>pache, <strong>M<\/strong>ySQL, <strong>P<\/strong>HP.<\/p>\n<p>This updated tutorial shows the installation of the latest PHP versions (7.0, 7.1 and 7.2) on CentOS 7.4.<\/p>\n<h2 id=\"-preliminary-note\">1 Preliminary Note<\/h2>\n<p>In this tutorial, I use the hostname <span class=\"system\">server1.example.com<\/span> with the IP p <span class=\"system\">192.168.1.100<\/span>. These settings might differ for you, so you have to replace them where appropriate.<\/p>\n<p>I will add the EPEL repo here to install latest phpMyAdmin as follows:<\/p>\n<p class=\"command\"><span>rpm &#8211;import \/etc\/pki\/rpm-gpg\/RPM-GPG-KEY*<br \/>yum -y install epel-release<\/span><\/p>\n<p>To edit files on the shell, I&#8217;ll install the nano editor. If you prefer vi for file editing, then skip this step.<\/p>\n<p class=\"command\">yum -y install nano<\/p>\n<h2 id=\"-installing-mysql-mariadb\">2 Installing MySQL \/ MariaDB<\/h2>\n<p>MariaDB is a MySQL fork of the original MySQL developer Monty Widenius. MariaDB is compatible with MySQL and I&#8217;ve chosen to use MariaDB here instead of MySQL. Run this command to install MariaDB with yum:<\/p>\n<p class=\"command\">yum -y install mariadb-server mariadb<\/p>\n<p>Then we create the system startup links for MySQL (so that MySQL starts automatically whenever the system boots) and start the MySQL server:<\/p>\n<p class=\"command\">systemctl start mariadb.service<br \/>systemctl enable mariadb.service<\/p>\n<p>Set passwords for the MySQL root account:<\/p>\n<p class=\"command\">mysql_secure_installation<\/p>\n<p class=\"system\">[<a href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"ef9d80809baf9c8a9d998a9dde\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a> ~]# mysql_secure_installation<\/p>\n<p>NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0 SERVERS IN PRODUCTION USE!\u00a0 PLEASE READ EACH STEP CAREFULLY!<\/p>\n<p>In order to log into MariaDB to secure it, we&#8217;ll need the current<br \/>password for the root user.\u00a0 If you&#8217;ve just installed MariaDB, and<br \/>you haven&#8217;t set the root password yet, the password will be blank,<br \/>so you should just press enter here.<\/p>\n<p>Enter current password for root (enter for none): <strong>&lt;&#8211;ENTER<\/strong><br \/>OK, successfully used password, moving on&#8230;<\/p>\n<p>Setting the root password ensures that nobody can log into the MariaDB<br \/>root user without the proper authorisation.<\/p>\n<p>Set root password? [Y\/n] <br \/>New password: <strong>&lt;&#8211;yourmariadbpassword<\/strong><br \/>Re-enter new password: <strong>&lt;&#8211;yourmariadbpassword<\/strong><br \/>Password updated successfully!<br \/>Reloading privilege tables..<br \/>\u00a0&#8230; Success!<\/p>\n<p>By default, a MariaDB installation has an anonymous user, allowing anyone<br \/>to log into MariaDB without having to have a user account created for<br \/>them.\u00a0 This is intended only for testing, and to make the installation<br \/>go a bit smoother.\u00a0 You should remove them before moving into a<br \/>production environment.<\/p>\n<p>Remove anonymous users? [Y\/n] <strong>&lt;&#8211;ENTER<\/strong><br \/>\u00a0&#8230; Success!<\/p>\n<p>Normally, root should only be allowed to connect from &#8216;localhost&#8217;.\u00a0 This<br \/>ensures that someone cannot guess at the root password from the network.<\/p>\n<p>Disallow root login remotely? [Y\/n] <strong>&lt;&#8211;ENTER<\/strong><br \/>\u00a0&#8230; Success!<\/p>\n<p>By default, MariaDB comes with a database named &#8216;test&#8217; that anyone can<br \/>access.\u00a0 This is also intended only for testing, and should be removed<br \/>before moving into a production environment.<\/p>\n<p>Remove test database and access to it? [Y\/n] <strong>&lt;&#8211;ENTER<\/strong><br \/>\u00a0&#8211; Dropping test database&#8230;<br \/>\u00a0&#8230; Success!<br \/>\u00a0&#8211; Removing privileges on test database&#8230;<br \/>\u00a0&#8230; Success!<\/p>\n<p>Reloading the privilege tables will ensure that all changes made so far<br \/>will take effect immediately.<\/p>\n<p>Reload privilege tables now? [Y\/n] <strong>&lt;&#8211;ENTER<\/strong><br \/>\u00a0&#8230; Success!<\/p>\n<p>Cleaning up&#8230;<\/p>\n<p>All done!\u00a0 If you&#8217;ve completed all of the above steps, your MariaDB<br \/>installation should now be secure.<\/p>\n<p>Thanks for using MariaDB!<br \/>[<a href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"f4869b9b80b4879186829186c5\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a> ~]#<\/p>\n<h2 id=\"-installing-apache\">3 Installing Apache<\/h2>\n<p>CentOS 7 ships with Apache 2.4. Apache is directly available as a CentOS 7 package, therefore we can install it like this:<\/p>\n<p class=\"command\">yum -y install httpd<\/p>\n<p>Here a screenshot of the installation process.<\/p>\n<p class=\"system\"><a class=\"fancybox\" id=\"img-centos-apache-installation\" href=\"https:\/\/www.howtoforge.com\/images\/apache-php-mysql-lamp-centos-7-4\/big\/centos-apache-installation.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/how-to-install-apache-php-7-2-and-mysql-on-centos-7-4-lamp.png\" alt=\"Install Apache http server on CentOS\" width=\"550\" height=\"254\" title=\"\"><\/a><\/p>\n<p>Now configure your system to start Apache at boot time&#8230;<\/p>\n<p class=\"command\">systemctl start httpd.service<\/p>\n<p class=\"command\">systemctl enable httpd.service<\/p>\n<p>To be able to access the web server from outside, we have to open the HTTP (80) and HTTPS (443) ports in the firewall.\u00a0The default firewall on CentOS is firewalld which can be configured with the firewalld-cmd command.<\/p>\n<p class=\"command\">firewall-cmd &#8211;permanent &#8211;zone=public &#8211;add-service=http <br \/>firewall-cmd &#8211;permanent &#8211;zone=public &#8211;add-service=https<br \/>firewall-cmd &#8211;reload<\/p>\n<p>Now direct your browser to the IP address of your server, in my case\u00a0<span class=\"system\">http:\/\/192.168.1.100<\/span>, and you should see the Apache placeholder page:<\/p>\n<p><a class=\"fancybox\" id=\"img-centos-apache-default-page\" href=\"https:\/\/www.howtoforge.com\/images\/apache-php-mysql-lamp-centos-7-3\/big\/centos-apache-default-page.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/how-to-install-apache-php-7-2-and-mysql-on-centos-7-4-lamp-1.png\" alt=\"Apache web server started on CentOS 7\" width=\"550\" height=\"358\" title=\"\"><\/a><\/p>\n<h2 id=\"-installing-php\">4 Installing PHP<\/h2>\n<p>The PHP version that ships with CentOS as default is quite old (PHP 5.4). Therefore I will show you in this\u00a0chapter some options to install newer PHP versions like PHP 7.0 or 7.1 from Remi repository.<\/p>\n<p>Add the Remi CentOS repository.<\/p>\n<p class=\"command\">rpm -Uvh\u00a0http:\/\/rpms.remirepo.net\/enterprise\/remi-release-7.rpm<\/p>\n<p>Install yum-utils as we need the yum-config-manager utility.<\/p>\n<p class=\"command\">yum -y install yum-utils<\/p>\n<p>and run yum update<\/p>\n<p class=\"command\">yum update<\/p>\n<p>Now you have to chose which PHP version you want to use on the server. If you like to use PHP 5.4, then proceed to chapter 4.1. To install PHP 7.0, follow the commands in chapter 4.2, for PHP 7.1 chapter 4.3 and for PHP 7.1, use chapter 4.4 instead. Follow just one of the 4.x chapters and not all of them as you can only use one PHP version at a time with Apache mod_php.<\/p>\n<h3 id=\"-install-php-\">4.1 Install PHP 5.4<\/h3>\n<p>To install PHP 5.4, run this command:<\/p>\n<p class=\"command\"><span>yum -y install\u00a0<\/span>php<\/p>\n<h3 id=\"-install-php--2\">4.2 Install PHP 7.0<\/h3>\n<p>We can install PHP 7.0 and the Apache PHP 7.0 module as follows:<\/p>\n<p class=\"command\">yum-config-manager &#8211;enable remi-php70<\/p>\n<p class=\"command\">yum -y install\u00a0php php-opcache<\/p>\n<h3 id=\"-install-php--3\">4.3 Install PHP 7.1<\/h3>\n<p>If you want to use PHP 7.1 instead, use:<\/p>\n<p class=\"command\"><span>yum-config-manager &#8211;enable remi-php71<\/span><\/p>\n<p class=\"command\"><span>yum -y install\u00a0php php-opcache<\/span><\/p>\n<h3 id=\"-install-php--4\">4.4 Install PHP 7.2<\/h3>\n<p>If you want to use PHP 7.2 instead, use:<\/p>\n<p class=\"command\"><span>yum-config-manager &#8211;enable<span>\u00a0<\/span>remi-php72<\/span><\/p>\n<p class=\"command\"><span>yum -y install\u00a0php<span>\u00a0<\/span>php-opcache<\/span><\/p>\n<p>In this example and in the downloadable virtual machine, I&#8217;ll use PHP 7.2.<\/p>\n<p>We must restart Apache to apply the changes:<\/p>\n<p class=\"command\">\u00a0systemctl restart httpd.service<\/p>\n<h2 id=\"-testing-php-getting-details-about-your-php-installation\">5 Testing PHP \/ Getting Details About Your PHP Installation<\/h2>\n<p>The document root of the default website is \/var\/www\/html. We will create a small PHP file (info.php) in that directory and call it in a browser to test the PHP installation. The file will display lots of useful details about our PHP installation, such as the installed PHP version.<\/p>\n<p class=\"command\">nano \/var\/www\/html\/info.php<\/p>\n<pre>&lt;?php&#13;\nphpinfo();<\/pre>\n<p>Now we call that file in a browser (e.g. <span class=\"system\">http:\/\/192.168.1.100\/info.php<\/span>):<\/p>\n<p><a class=\"fancybox\" id=\"img-centos-with-php-7.2_\" href=\"https:\/\/www.howtoforge.com\/images\/apache-php-mysql-lamp-centos-7-4\/big\/centos-with-php-7.2_.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/how-to-install-apache-php-7-2-and-mysql-on-centos-7-4-lamp-2.png\" alt=\"PHP 7.2 installed on CentOS 7\" width=\"550\" height=\"349\" title=\"\"><\/a><\/p>\n<p>As you see, PHP 7.2 is working, and it&#8217;s working through the <span class=\"system\">Apache 2.0<\/span> Handler, as shown in the <span class=\"system\">Server API line<\/span>. If you scroll further down, you will see all modules that are already enabled in PHP. MySQL is not listed there which means we don&#8217;t have MySQL support in PHP yet.<\/p>\n<h2 id=\"-getting-mysql-support-in-php\">6 Getting MySQL Support In PHP<\/h2>\n<p>To get MySQL support in PHP, we can install the <span class=\"system\">php-mysqlnd<\/span>\u00a0package. It&#8217;s a good idea to install some other PHP modules as well as you might need them for your applications. You can search for available PHP5 modules like this:<\/p>\n<p class=\"command\">yum search php<\/p>\n<p>Pick the ones you need and install them like this:<\/p>\n<p class=\"command\">yum -y install\u00a0php-mysqlnd\u00a0php-pdo<\/p>\n<p>In the next step I will install some common PHP modules that are required by CMS Systems like WordPress, Joomla, and Drupal:<\/p>\n<p class=\"command\">yum -y install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring\u00a0php-soap curl curl-devel<\/p>\n<p>Now restart Apache web server:<\/p>\n<p class=\"command\">\u00a0systemctl restart httpd.service<\/p>\n<p>Now reload <span class=\"system\">http:\/\/192.168.1.100\/info.php<\/span> in your browser and scroll down to the modules section again. You should now find lots of new modules like curl etc there.:<\/p>\n<p><a class=\"fancybox\" id=\"img-centos-php-7-2-mysql\" href=\"https:\/\/www.howtoforge.com\/images\/apache-php-mysql-lamp-centos-7-4\/big\/centos-php-7-2-mysql.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/how-to-install-apache-php-7-2-and-mysql-on-centos-7-4-lamp-3.png\" alt=\"MySQL support added to PHP 7.2 on CentOS\" width=\"550\" height=\"349\" title=\"\"><\/a><\/p>\n<p>If you don&#8217;t need the php info output anymore, then delete that file for security reasons.<\/p>\n<p class=\"command\">rm\u00a0<span>\/var\/www\/<\/span>html<span>\/info.php<\/span><\/p>\n<h2 id=\"-phpmyadmin-installation\">7 phpMyAdmin installation<\/h2>\n<p>phpMyAdmin is a web interface through which you can manage your MySQL databases.<br \/>phpMyAdmin can now be installed as follows:<\/p>\n<p class=\"command\">yum -y install phpMyAdmin<\/p>\n<p>Now we configure phpMyAdmin. We change the Apache configuration so that phpMyAdmin allows connections not just from localhost (by commenting out the &lt;RequireAny&gt;\u00a0stanza and adding the &#8216;Require all granted&#8217; line):<\/p>\n<p class=\"command\">nano \/etc\/httpd\/conf.d\/phpMyAdmin.conf<\/p>\n<pre readability=\"9\">[...]<br\/>Alias \/phpMyAdmin \/usr\/share\/phpMyAdmin&#13;\nAlias \/phpmyadmin \/usr\/share\/phpMyAdmin&#13;\n&#13;\n&lt;Directory \/usr\/share\/phpMyAdmin\/&gt;<br\/>AddDefaultCharset UTF-8<p>&lt;IfModule mod_authz_core.c&gt;<br\/># Apache 2.4<br\/><span class=\"highlight\"># &lt;RequireAny&gt;<\/span><br\/><span class=\"highlight\"># Require ip 127.0.0.1<\/span><br\/><span class=\"highlight\"># Require ip ::1<\/span><br\/><span class=\"highlight\"># &lt;\/RequireAny&gt;<\/span><br\/><span class=\"highlight\"> Require all granted<\/span><br\/>&lt;\/IfModule&gt;<br\/>&lt;IfModule !mod_authz_core.c&gt;<br\/># Apache 2.2<br\/>Order Deny,Allow<br\/>Deny from All<br\/>Allow from 127.0.0.1<br\/>Allow from ::1<br\/>&lt;\/IfModule&gt;<br\/>&lt;\/Directory&gt;<br\/>&#13;\n&#13;\n&#13;\n&lt;Directory \/usr\/share\/phpMyAdmin\/&gt;&#13; Options none&#13; AllowOverride Limit&#13; Require all granted&#13;\n&lt;\/Directory&gt;<\/p><p>[...] <\/p><\/pre>\n<p>Next, we change the authentication in phpMyAdmin from <span class=\"system\">cookie<\/span> to <span class=\"system\">http<\/span>:<\/p>\n<p class=\"command\">nano \/etc\/phpMyAdmin\/config.inc.php<\/p>\n<pre>[...]&#13;\n$cfg['Servers'][$i]['auth_type'] = 'http'; \/\/ Authentication method (config, http or cookie based)?&#13;\n[...]<\/pre>\n<p>Restart Apache:<\/p>\n<p class=\"command\">systemctl restart\u00a0 httpd.service<\/p>\n<p>Afterwards, you can access phpMyAdmin under <span class=\"system\">http:\/\/192.168.1.100\/phpmyadmin\/<\/span>:<\/p>\n<p><a class=\"fancybox\" id=\"img-centos-7-phpmyadmin\" href=\"https:\/\/www.howtoforge.com\/images\/apache-php-mysql-lamp-centos-7-4\/big\/centos-7-phpmyadmin.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/how-to-install-apache-php-7-2-and-mysql-on-centos-7-4-lamp-4.png\" alt=\"PHPMyAdmin installed on CentOS 7.4\" width=\"550\" height=\"349\" title=\"\"><\/a><\/p>\n<h2 id=\"download-centos-server-as-virtual-machine\">8 Download as virtual machine<\/h2>\n<p>This setup is available as virtual machine download in ova\/ovf format (compatible with VMWare and Virtualbox) for howtoforge subscribers.<\/p>\n<p><strong>Login details for the VM<\/strong><\/p>\n<ul>\n<li>The Linux root password is: howtoforge.<\/li>\n<li>Rhe MySQL root password is: howtoforge<\/li>\n<\/ul>\n<p>Please change both passwords on the\u00a0first login.<\/p>\n<ul>\n<li>The IP address of the VM is 192.168.1.100<\/li>\n<\/ul>\n<h2 id=\"-links\">9 Links<\/h2>\n<p>\u00a0\u00a0\u00a0 Apache: <a href=\"http:\/\/httpd.apache.org\/\" target=\"_blank\" rel=\"noopener\">http:\/\/httpd.apache.org\/<\/a><br \/>\u00a0\u00a0\u00a0 PHP: <a href=\"http:\/\/www.php.net\/\" target=\"_blank\" rel=\"noopener\">http:\/\/www.php.net\/<\/a><br \/>\u00a0\u00a0\u00a0 MySQL: <a href=\"http:\/\/www.mysql.com\/\" target=\"_blank\" rel=\"noopener\">http:\/\/www.mysql.com\/<\/a><br \/>\u00a0\u00a0\u00a0 CentOS: <a href=\"http:\/\/www.centos.org\/\" target=\"_blank\" rel=\"noopener\">http:\/\/www.centos.org\/<\/a><br \/>\u00a0\u00a0\u00a0 phpMyAdmin: <a href=\"http:\/\/www.phpmyadmin.net\/\" target=\"_blank\" rel=\"noopener\">http:\/\/www.phpmyadmin.net\/<br \/><\/a><\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how\u00a0to install an Apache web server on CentOS 7 server with PHP (mod_php) and MySQL database. The acronym LAMP is short for Linux, Apache, MySQL, PHP. This updated tutorial shows the installation of the latest PHP versions (7.0, 7.1 and 7.2) on CentOS 7.4. 1 Preliminary Note In this tutorial, I use &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-2911","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=2911"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2911\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=2911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=2911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=2911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}