How to install Apache, PHP 7.3 and MySQL on CentOS 7.6

<p>This tutorial shows how to 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.</p><p>This updated tutorial shows the installation of the latest PHP versions (7.0, 7.1, 7.2 and 7.3) on CentOS 7.6.</p><p>1 Preliminary Note</p><p>In this tutorial, I use the hostname server1.example.com with the IP p 192.168.0.100. These settings might differ for you, so you have to replace them where appropriate.</p><p>I will add the EPEL repo here to install latest phpMyAdmin as follows:</p><pre class="brush:bash;toolbar:false">rpm--import/etc/pki/rpm-gpg/RPM-GPG-KEY* yum-yinstallepel-release</pre><p>To edit files on the shell, I&#39;ll install the nano editor. If you prefer vi for file editing, then skip this step.</p><p>yum -y install nano</p><p>2 Installing MySQL / MariaDB</p><p>MariaDB is a MySQL fork of the original MySQL developer Monty Widenius. MariaDB is compatible with MySQL and I&#39;ve chosen to use MariaDB here instead of MySQL. Run this command to install MariaDB with yum:</p><pre class="brush:bash;toolbar:false">yum-yinstallmariadb-servermariadb</pre><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><pre class="brush:bash;toolbar:false">systemctlstartmariadb.service systemctlenablemariadb.service</pre><p>Set passwords for the MySQL root account:</p><p>mysql_secure_installation</p><pre class="brush:bash;toolbar:false">[root@server1~]#mysql_secure_installation</pre><p>NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB</p><p> SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!</p><pre class="brush:bash;toolbar:false">InordertologintoMariaDBtosecureit,we&#39;llneedthecurrent passwordfortherootuser.Ifyou&#39;vejustinstalledMariaDB,and youhaven&#39;tsettherootpasswordyet,thepasswordwillbeblank, soyoushouldjustpressenterhere. Entercurrentpasswordforroot(enterfornone):&lt;--ENTER OK,successfullyusedpassword,movingon... SettingtherootpasswordensuresthatnobodycanlogintotheMariaDB rootuserwithouttheproperauthorisation. Setrootpassword?[Y/n] Newpassword:&lt;--yourmariadbpassword Re-enternewpassword:&lt;--yourmariadbpassword Passwordupdatedsuccessfully! Reloadingprivilegetables.. ...Success! Bydefault,aMariaDBinstallationhasananonymoususer,allowinganyone tologintoMariaDBwithouthavingtohaveauseraccountcreatedfor them.Thisisintendedonlyfortesting,andtomaketheinstallation goabitsmoother.Youshouldremovethembeforemovingintoa productionenvironment. Removeanonymoususers?[Y/n]&lt;--ENTER ...Success! Normally,rootshouldonlybeallowedtoconnectfrom&#39;localhost&#39;.This ensuresthatsomeonecannotguessattherootpasswordfromthenetwork. Disallowrootloginremotely?[Y/n]&lt;--ENTER ...Success! Bydefault,MariaDBcomeswithadatabasenamed&#39;test&#39;thatanyonecan access.Thisisalsointendedonlyfortesting,andshouldberemoved beforemovingintoaproductionenvironment. Removetestdatabaseandaccesstoit?[Y/n]&lt;--ENTER -Droppingtestdatabase... ...Success! -Removingprivilegesontestdatabase... ...Success! Reloadingtheprivilegetableswillensurethatallchangesmadesofar willtakeeffectimmediately. Reloadprivilegetablesnow?[Y/n]&lt;--ENTER ...Success!</pre><p>Cleaning up...</p><p>All done! If you&#39;ve completed all of the above steps, your MariaDB</p><p>installation should now be secure.</p><p>Thanks for using MariaDB!</p><p>[root@server1 ~]#</p><p>3 Installing Apache</p><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><pre class="brush:bash;toolbar:false">yum-yinstallhttpd</pre><p>Here a screenshot of the installation process.</p><p>Installing Apache web server on CentOS</p><p>Now configure your system to start Apache at boot time...</p><pre class="brush:bash;toolbar:false">systemctlstarthttpd.service systemctlenablehttpd.service</pre><p>To be able to access the webserver from outside, we have to open the HTTP (80) and HTTPS (443) ports in the firewall. The default firewall on CentOS is firewalld which can be configured with the firewalld-cmd command.</p><pre class="brush:bash;toolbar:false">firewall-cmd--permanent--zone=public--add-service=http firewall-cmd--permanent--zone=public--add-service=https firewall-cmd--reload</pre><p>Now direct your browser to the IP address of your server, in my case http://192.168.0.100, and you should see the Apache placeholder page:</p><p>Apache Default page on CentOS</p><p>4 Installing PHP</p><p>The PHP version that ships with CentOS as default is quite old (PHP 5.4). Therefore I will show you in this chapter some options to install newer PHP versions like PHP 7.0 to 7.3 from Remi repository.</p><p>Add the Remi CentOS repository.</p><pre class="brush:bash;toolbar:false">rpm-Uvhhttp://rpms.remirepo.net/enterprise/remi-release-7.rpm Installyum-utilsasweneedtheyum-config-managerutility.</pre><p>yum -y install yum-utils</p><p>and run yum update</p><p>yum update</p><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, for PHP 7.4 use chapter 4.4 and for PHP 7.3 follow chapter 4.5 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><p>4.1 Install PHP 5.4</p><p>To install PHP 5.4, run this command:</p><pre class="brush:bash;toolbar:false">yum-yinstallphp</pre><p>4.2 Install PHP 7.0</p><p>We can install PHP 7.0 and the Apache PHP 7.0 module as follows:</p><pre class="brush:bash;toolbar:false">yum-config-manager--enableremi-php70 yum-yinstallphpphp-opcache</pre><p>4.3 Install PHP 7.1</p><p>If you want to use PHP 7.1 instead, use:</p><pre class="brush:bash;toolbar:false">yum-config-manager--enableremi-php71 yum-yinstallphpphp-opcache</pre><p>4.4 Install PHP 7.2</p><p>If you want to use PHP 7.2 instead, use:</p><pre class="brush:bash;toolbar:false">yum-config-manager--enableremi-php72 yum-yinstallphpphp-opcache</pre><p>4.5 Install PHP 7.3</p><p>If you want to use PHP 7.3 instead, use:</p><pre class="brush:bash;toolbar:false">yum-config-manager--enableremi-php73 yum-yinstallphpphp-opcache</pre><p>In this example and in the downloadable virtual machine, I&#39;ll use PHP 7.3.</p><p>We must restart Apache to apply the changes:</p><p>systemctl restart httpd.service</p><p>5 Testing PHP / Getting Details About Your PHP Installation</p><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><p>nano /var/www/html/info.php</p><pre class="brush:php;toolbar:false">&lt;?php phpinfo();</pre><p>Now we call that file in a browser (e.g. http://192.168.0.100/info.php):</p><p>PHP version information</p><p>As you see, PHP 7.3 is working, and it&#39;s working through the Apache 2.0 Handler, as shown in the Server API line. 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&#39;t have MySQL support in PHP yet.</p><p>6 Getting MySQL Support In PHP</p><p>To get MySQL support in PHP, we can install the php-mysqlnd package. It&#39;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><pre class="brush:bash;toolbar:false">yumsearchphp</pre><p>Pick the ones you need and install them like this:</p><pre class="brush:bash;toolbar:false">yum-yinstallphp-mysqlndphp-pdo</pre><p>In the next step I will install some common PHP modules that are required by CMS Systems like Wordpress, Joomla, and Drupal:</p><pre class="brush:bash;toolbar:false">yum-yinstallphp-gdphp-ldapphp-odbcphp-pearphp-xmlphp-xmlrpcphp-mbstringphp-soapcurlcurl-devel</pre><p>Now restart Apache web server:</p><pre class="brush:bash;toolbar:false">systemctlrestarthttpd.service</pre><p>Now reload http://192.168.0.100/info.php in your browser and scroll down to the modules section again. You should now find lots of new modules like curl etc there.:</p><p>PHP has now MySQL support enabled</p><p>If you don&#39;t need the PHP info output anymore, then delete that file for security reasons.</p><pre class="brush:bash;toolbar:false">rm/var/www/html/info.php</pre><p>7 phpMyAdmin installation</p><p>phpMyAdmin is a web interface through which you can manage your MySQL databases.</p><p>phpMyAdmin can now be installed as follows:</p><pre class="brush:bash;toolbar:false">yum-yinstallphpMyAdmin</pre><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; stanza and adding the &#39;Require all granted&#39; line):</p><p>nano /etc/httpd/conf.d/phpMyAdmin.conf</p><p>[...]</p><p>Alias /phpMyAdmin /usr/share/phpMyAdmin</p><p>Alias /phpmyadmin /usr/share/phpMyAdmin</p><pre class="brush:bash;toolbar:false">&lt;Directory/usr/share/phpMyAdmin/&gt; AddDefaultCharsetUTF-8 &lt;IfModulemod_authz_core.c&gt; #Apache2.4 #&lt;RequireAny&gt; #Requireip127.0.0.1 #Requireip::1 #&lt;/RequireAny&gt; Requireallgranted &lt;/IfModule&gt; &lt;IfModule!mod_authz_core.c&gt; #Apache2.2 OrderDeny,Allow DenyfromAll Allowfrom127.0.0.1 Allowfrom::1 &lt;/IfModule&gt; &lt;/Directory&gt; &lt;Directory/usr/share/phpMyAdmin/&gt; Optionsnone AllowOverrideLimit Requireallgranted &lt;/Directory&gt;</pre><p>[...]</p><p>Restart Apache to apply the configuration changes:</p><p>systemctl restart httpd.service</p><p>Afterwards, you can access phpMyAdmin under http://192.168.0.100/phpmyadmin/:</p><p>PHPMyAdmin Login</p><p>PHPMyAdmin on CentOS</p><p>8 Download as a virtual machine</p><p>This setup is available as virtual machine download in ova/ovf format (compatible with VMWare and Virtualbox) for howtoforge subscribers.</p><p>Login details for the VM</p><p>The Linux root password is: howtoforge.</p><p>Rhe MySQL root password is: howtoforge</p><p>Please change both passwords on the first login.</p><p>The IP address of the VM is 192.168.0.100</p><p>9 Links</p><p> Apache: http://httpd.apache.org/</p><p> PHP: http://www.php.net/</p><p> MySQL: http://www.mysql.com/</p><p> CentOS: http://www.centos.org/</p><p> phpMyAdmin: http://www.phpmyadmin.net/</p>
RangeTime:0.007962s
RangeMem:215.61 KB
返回顶部 留言