{"id":3504,"date":"2018-05-02T15:24:16","date_gmt":"2018-05-02T11:24:16","guid":{"rendered":"https:\/\/www.howtoforge.com\/tutorial\/install-apache-with-php-and-mysql-lamp-on-debian-stretch\/"},"modified":"2018-05-02T15:24:16","modified_gmt":"2018-05-02T11:24:16","slug":"debian-9-lamp-server-tutorial-with-apache-php-7-and-mariadb","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/debian-9-lamp-server-tutorial-with-apache-php-7-and-mariadb\/","title":{"rendered":"Debian 9 LAMP Server Tutorial with Apache, PHP 7 and MariaDB"},"content":{"rendered":"<p>LAMP is short for <b>L<\/b>inux, <b>A<\/b>pache, <b>M<\/b>ySQL, <b>P<\/b>HP. This tutorial shows how you can install an Apache web server on a Debian\u00a0Stretch (9)\u00a0server with PHP 7 (mod_php) and MariaDB\u00a0support. MariaDB is a fork of the well known MySQL database server, it provides a MySQL compatible feature set and is a bit faster according to benchmarks that I found on the internet. MariaDB will work with all applications that require MySQL like Wordpress, Joomla etc.<\/p>\n<p><span>A LAMP setup is a perfect basis for CMS systems like Joomla, Wordpress or Drupal.<\/span><\/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 address <span class=\"system\">192.168.1.100<\/span>. These settings might differ for you, so you have to replace them where appropriate.<\/p>\n<h2 id=\"-installing-mariadb-as-mysql-replacement\">2 Installing MariaDB as MySQL replacement<\/h2>\n<p>First, we install MariaDB\u00a0like this:<\/p>\n<p class=\"command\"><span>apt-get -y install mariadb-server mariadb-client<\/span><\/p>\n<p>Next, we will secure MariaDB with the <strong>mysql_secure_installation<\/strong> command. Run the below command and follow the wizard.<\/p>\n<p class=\"command\">mysql_secure_installation<\/p>\n<p>The recommended input is shown in red.<\/p>\n<p class=\"system\">mysql_secure_installation<\/p>\n<p class=\"system\">NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB<br \/>SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!<\/p>\n<p class=\"system\">In order to log into MariaDB to secure it, we&#8217;ll need the current<br \/>password for the root user. 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 class=\"system\">Enter current password for root (enter for none): <span class=\"highlight\">&lt;&#8211; Hit return<\/span><br \/>OK, successfully used password, moving on&#8230;<\/p>\n<p class=\"system\">Setting the root password ensures that nobody can log into the MariaDB<br \/>root user without the proper authorisation.<\/p>\n<p class=\"system\">Set root password? [Y\/n] <span class=\"highlight\">&lt;&#8211; y<\/span><br \/>New password: <span class=\"highlight\">&lt;&#8211; Enter the new password for the MariaDB root user<\/span><br \/>Re-enter new password: <span class=\"highlight\">&lt;&#8211; Enter the password again<\/span><br \/>Password updated successfully!<br \/>Reloading privilege tables..<br \/>&#8230; Success!<\/p>\n<p class=\"system\">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. This is intended only for testing, and to make the installation<br \/>go a bit smoother. You should remove them before moving into a<br \/>production environment.<\/p>\n<p class=\"system\">Remove anonymous users? [Y\/n] <span class=\"highlight\">&lt;&#8211; y<\/span><br \/>&#8230; Success!<\/p>\n<p class=\"system\">Normally, root should only be allowed to connect from &#8216;localhost&#8217;. This<br \/>ensures that someone cannot guess at the root password from the network.<\/p>\n<p class=\"system\">Disallow root login remotely? [Y\/n] <span class=\"highlight\">&lt;&#8211; y<\/span><br \/>&#8230; Success!<\/p>\n<p class=\"system\">By default, MariaDB comes with a database named &#8216;test&#8217; that anyone can<br \/>access. This is also intended only for testing, and should be removed<br \/>before moving into a production environment.<\/p>\n<p class=\"system\">Remove test database and access to it? [Y\/n] <span class=\"highlight\">&lt;&#8211; y<\/span><br \/>&#8211; Dropping test database&#8230;<br \/>&#8230; Success!<br \/>&#8211; Removing privileges on test database&#8230;<br \/>&#8230; Success!<\/p>\n<p class=\"system\">Reloading the privilege tables will ensure that all changes made so far<br \/>will take effect immediately.<\/p>\n<p class=\"system\">Reload privilege tables now? [Y\/n] <span class=\"highlight\">&lt;&#8211; y<\/span><br \/>&#8230; Success!<\/p>\n<p class=\"system\">Cleaning up&#8230;<\/p>\n<p class=\"system\">All done! If you&#8217;ve completed all of the above steps, your MariaDB<br \/>installation should now be secure.<\/p>\n<p class=\"system\">Thanks for using MariaDB!<\/p>\n<p>The MariaDB setup is secured now.<\/p>\n<h2 id=\"-installing-apache-web-server\">3 Installing Apache web server<\/h2>\n<p>Apache is available as a Debian package, therefore we can install it like this:<\/p>\n<p class=\"command\">apt-get -y install apache2<\/p>\n<p>Now direct your browser to <span class=\"system\">http:\/\/192.168.1.100<\/span>, and you should see the Apache2 placeholder page (<span class=\"system\">It works!<\/span>):<\/p>\n<p><a class=\"fancybox\" id=\"img-debian-apache-default-page\" href=\"https:\/\/www.howtoforge.com\/images\/install-apache-with-php-and-mysql-on-debian-stretch\/big\/debian-apache-default-page.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/debian-9-lamp-server-tutorial-with-apache-php-7-and-mariadb.png\" alt=\"Debian 9 Apache default page\" width=\"550\" height=\"350\" title=\"\"><\/a><\/p>\n<p>Apache&#8217;s default document root is <span class=\"system\">\/var\/www<\/span>\u00a0on Debian, and the configuration file is <span class=\"system\">\/etc\/apache2\/apache2.conf<\/span>. Additional configurations are stored in subdirectories of the <span class=\"system\">\/etc\/apache2<\/span> directory such as <span class=\"system\">\/etc\/apache2\/mods-enabled<\/span> (for Apache modules), <span class=\"system\">\/etc\/apache2\/sites-enabled<\/span> (for virtual hosts), and <span class=\"system\">\/etc\/apache2\/conf-enabled<\/span>.<\/p>\n<h2 id=\"-installing-php-\">4 Installing PHP 7.1<\/h2>\n<p>We can install PHP and the Apache PHP module as follows:<\/p>\n<p class=\"command\">apt-get -y install php7.0 libapache2-mod-php7.0<\/p>\n<p>We must restart Apache afterward:<\/p>\n<p class=\"command\">service apache2 restart<\/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 web site is <span class=\"system\">\/var\/www\/html<\/span>. We will now create a small PHP file (<span class=\"system\">info.php<\/span>) in that directory and call it in a browser. 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<br\/>phpinfo();<\/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-debian-9-php-info\" href=\"https:\/\/www.howtoforge.com\/images\/install-apache-with-php-and-mysql-on-debian-stretch\/big\/debian-9-php-info.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/debian-9-lamp-server-tutorial-with-apache-php-7-and-mariadb-1.png\" alt=\"PHP installed on Debian 9 in Apache\" width=\"550\" height=\"350\" title=\"\"><\/a><\/p>\n<p>As you see, PHP 7.0 is working, and it&#8217;s working through the <span class=\"system\">Apache 2.0 Handler<\/span>, as shown in the <span class=\"system\">Server API<\/span> line. If you scroll further down, you will see all modules that are already enabled in PHP5. MySQL \/ MariaDB is not listed there which means we don&#8217;t have MySQL support in PHP5 yet.<\/p>\n<h2 id=\"-getting-mysql-and-mariadb-support-in-php\">6 Getting MySQL and MariaDB Support in PHP<\/h2>\n<p>To get MySQL support in PHP, we\u00a0will install the <span class=\"system\">php7.0-mysql<\/span> package. 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 PHP 7 modules like this:<\/p>\n<p class=\"command\">apt-cache search php7.0<\/p>\n<p>Pick the ones you need and install them like this:<\/p>\n<p class=\"command\">apt-get -y install php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick php7.0-imap php7.0-mcrypt php-memcache php7.0-pspell php7.0-recode php7.0-sqlite3\u00a0php7.0-tidy php7.0-xmlrpc php7.0-xsl<\/p>\n<p>Now restart Apache:<\/p>\n<p class=\"command\">service apache2 restart<\/p>\n<h2 id=\"-php-cache-to-improve-the-php-speed\">7 PHP Cache to improve the PHP speed<\/h2>\n<p>To speed up PHP, an Opcache should be installed. Check if the PHP Opcache module has been installed and enabled correctly.Run this command:<\/p>\n<p class=\"command\">php &#8211;version<\/p>\n<p>The output shall contain the line I marked in red.<\/p>\n<p class=\"system\">PHP 7.0.27-0+deb9u1 (cli) (built: Jan 5 2018 13:51:52) ( NTS )<br \/>Copyright (c) 1997-2017 The PHP Group<br \/>Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies<br \/><span class=\"highlight\">with Zend OPcache<\/span> v7.0.27-0+deb9u1, Copyright (c) 1999-2017, by Zend Technologies<\/p>\n<p>If you do not see the Opcache module in the result, install it with this command:<\/p>\n<p class=\"command\">apt-get -y install\u00a0php7.0-opcache<\/p>\n<p>There is one more cache which might be useful, it&#8217;s name is APCu. APCu\u00a0is a free PHP opcode cacher for caching and optimizing PHP intermediate code.<\/p>\n<p><span>APCu<\/span>\u00a0can be installed as follows:<\/p>\n<p class=\"command\">apt-get -y install\u00a0<span>php-apcu<\/span><\/p>\n<p>Now restart Apache:<\/p>\n<p class=\"command\">service apache2 restart<\/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 there, including the MySQL module which is used as MariaDB driver:<\/p>\n<p><a class=\"fancybox\" id=\"img-php-with-mysql-support\" href=\"https:\/\/www.howtoforge.com\/images\/install-apache-with-php-and-mysql-on-debian-stretch\/big\/php-with-mysql-support.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/debian-9-lamp-server-tutorial-with-apache-php-7-and-mariadb-2.png\" alt=\"MySQL support enabled in PHP 7\" width=\"550\" height=\"350\" title=\"\"><\/a><\/p>\n<h2 id=\"-phpmyadmin\">8 phpMyAdmin<\/h2>\n<p>phpMyAdmin is a web interface through which you can manage your MySQL and MariaDB databases. It&#8217;s a good idea to install it:<\/p>\n<p class=\"command\">apt-get -y install phpmyadmin<\/p>\n<p>You will see the following questions:<\/p>\n<p><a class=\"fancybox\" id=\"img-phpmyadmin-apache-debian\" href=\"https:\/\/www.howtoforge.com\/images\/install-apache-with-php-and-mysql-on-debian-stretch\/big\/phpmyadmin-apache-debian.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/debian-9-lamp-server-tutorial-with-apache-php-7-and-mariadb-3.png\" alt=\"Configure phpMyAdmin for Apache\" width=\"550\" height=\"196\" title=\"\"><\/a><\/p>\n<p class=\"system\"><span><span>Web server to reconfigure automatically: <\/span><span class=\"highlight\">&lt;&#8211; apache2<\/span><\/span><\/p>\n<p><span><a class=\"fancybox\" id=\"img-dbconfig-common\" href=\"https:\/\/www.howtoforge.com\/images\/install-apache-with-php-and-mysql-on-debian-stretch\/big\/dbconfig-common.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/debian-9-lamp-server-tutorial-with-apache-php-7-and-mariadb-4.png\" alt=\"Use dbconfig-common to configure phpMyAdmin\" width=\"550\" height=\"110\" title=\"\"><\/a><\/span><\/p>\n<p class=\"system\"><span>Configure database for phpmyadmin with dbconfig-common?<\/span><span class=\"highlight\">&lt;&#8211; Yes<\/span><\/p>\n<p class=\"system\"><a class=\"fancybox\" id=\"img-phpmyadmin-application-password\" href=\"https:\/\/www.howtoforge.com\/images\/install-apache-with-php-and-mysql-on-debian-stretch\/big\/phpmyadmin-application-password.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/debian-9-lamp-server-tutorial-with-apache-php-7-and-mariadb-5.png\" alt=\"Automatically create an application password\" width=\"550\" height=\"120\" title=\"\"><\/a><\/p>\n<p class=\"system\"><span>MySQL application password for phpmyadmin: <\/span><span class=\"highlight\">&lt;&#8211; Press enter, apt will create a random password automatically.<\/span><\/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-phpmyadmin-debian\" href=\"https:\/\/www.howtoforge.com\/images\/install-apache-with-php-and-mysql-on-debian-stretch\/big\/phpmyadmin-debian.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/debian-9-lamp-server-tutorial-with-apache-php-7-and-mariadb-6.png\" alt=\"PHPMyAdmin on Debian 9\" width=\"550\" height=\"350\" title=\"\"><\/a><\/p>\n<h2 id=\"-enable-mysql-root-login-for-phpmyadmin\">9 Enable MySQL root Login for phpMyAdmin<\/h2>\n<p>While you can log in as root user into MariaDB on the shell, the root login will not work in phpMyAdmin.\u00a0To allow the root user to use phpMyAdmin as well, run the following command on the shell:<\/p>\n<p class=\"command\">echo &#8220;UPDATE mysql.user SET plugin = &#8216;mysql_native_password&#8217; WHERE user = &#8216;root&#8217; AND plugin = &#8216;unix_socket&#8217;;FLUSH PRIVILEGES;&#8221; | mysql -u root -p<\/p>\n<h2 id=\"-links\">10 Links<\/h2>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Finstall-apache-with-php-and-mysql-lamp-on-debian-stretch%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/debian-9-lamp-server-tutorial-with-apache-php-7-and-mariadb-7.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Finstall-apache-with-php-and-mysql-lamp-on-debian-stretch%2F&amp;text=Debian+9+LAMP+Server+Tutorial+with+Apache%2C+PHP+7+and+MariaDB&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/debian-9-lamp-server-tutorial-with-apache-php-7-and-mariadb-8.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/debian-9-lamp-server-tutorial-with-apache-php-7-and-mariadb-9.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Finstall-apache-with-php-and-mysql-lamp-on-debian-stretch%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/debian-9-lamp-server-tutorial-with-apache-php-7-and-mariadb-10.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>LAMP is short for Linux, Apache, MySQL, PHP. This tutorial shows how you can install an Apache web server on a Debian\u00a0Stretch (9)\u00a0server with PHP 7 (mod_php) and MariaDB\u00a0support. MariaDB is a fork of the well known MySQL database server, it provides a MySQL compatible feature set and is a bit faster according to benchmarks &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-3504","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/3504","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=3504"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/3504\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=3504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=3504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=3504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}