如何在CentOS/RHEL上安装PHP7的LAMP环境?

》一文中默认安装的是PHP5版本,此文适用于CentOS7上搭建PHP7的LAMP环境,如果你的项目不支持PHP7请移步前面链接。

升级php 5.X至php7

本文适用于CentOS/RHEL 6、7操作系统安装PHP7的LAMP环境。

我们需要先更新的yum源(软件仓库):

yum -y update

然后一直等待直到Complete!字样出现的时候就是完成了。

安装Apache

yum -y install httpd

启动httpd

systemctl start httpd

服务开机自启动

systemctl enable httpd

你可以使用systemctl status httpd来查看apache服务运行状态:

[root@localhost ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2017-01-13 06:47:26 UTC; 1min 14s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 6174 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─6174 /usr/sbin/httpd -DFOREGROUND
           ├─6175 /usr/sbin/httpd -DFOREGROUND
           ├─6176 /usr/sbin/httpd -DFOREGROUND
           ├─6177 /usr/sbin/httpd -DFOREGROUND
           ├─6178 /usr/sbin/httpd -DFOREGROUND
           └─6179 /usr/sbin/httpd -DFOREGROUND

Jan 13 06:47:25 localhost.localdomain systemd[1]: Starting The Apache HTTP Se...
Jan 13 06:47:26 localhost.localdomain httpd[6174]: AH00558: httpd: Could not ...
Jan 13 06:47:26 localhost.localdomain systemd[1]: Started The Apache HTTP Ser...
Hint: Some lines were ellipsized, use -l to show in full.

设置防火墙规则给http/https放行

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

安装MariaDB数据库

yum -y install mariadb-server mariadb

安装完成

Installed:
  mariadb-server.x86_64 1:5.5.52-1.el7

Dependency Installed:
  mariadb.x86_64 1:5.5.52-1.el7
  mariadb-libs.x86_64 1:5.5.52-1.el7
  perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7
  perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7
  perl-DBD-MySQL.x86_64 0:4.023-5.el7
  perl-DBI.x86_64 0:1.627-4.el7
  perl-Data-Dumper.x86_64 0:2.145-3.el7
  perl-IO-Compress.noarch 0:2.061-2.el7
  perl-Net-Daemon.noarch 0:0.48-5.el7
  perl-PlRPC.noarch 0:0.2020-14.el7

Complete!

启动MariaDB服务

systemctl start mariadb

初始化MariaDB

mysql_secure_installation

设置过程

[root@localhost ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current password for the root user.  If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank,so you should just press enter here.

Enter current password for root (enter for none):   ##回车
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation.

Set root password? [Y/n]    ##回车
New password:    #root的密码
Re-enter new password:    #重复密码
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them.  This is intended only for testing, and to make the installation go a bit smoother.  You should remove them before moving into a production environment.

Remove anonymous users? [Y/n] ##回车
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] ##回车
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can access.  This is also intended only for testing, and should be removed before moving into a production environment.

Remove test database and access to it? [Y/n] ##回车
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far will take effect immediately.

Reload privilege tables now? [Y/n] ##回车
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB installation should now be secure.

Thanks for using MariaDB!

别忘了让MariaDB开机自启动

systemctl enable mariadb.service

重点!安装PHP7

安装epel-release

yum -y install epel-release

记下来安装epel的源:

如果你的系统是CentOS/RHEL 7,运行

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
rpm -Uvh http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

如果你的系统是CentOS/RHEL 6,运行

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
rpm -Uvh http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm

获取到rpm源之后我们进行php7的安装

yum -y install php70w

安装完成后安装php7的模块,查看哪些可用模块:

yum search php70w
[root@localhost ~]# yum search php70w
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * epel: mirrors.neusoft.edu.cn
 * extras: mirrors.yun-idc.com
 * remi-safe: mirror.innosol.asia
 * updates: mirrors.cn99.com
 * webtatic: uk.repo.webtatic.com
============================= N/S matched: php70w ==============================
php70w.x86_64 : PHP scripting language for creating dynamic web sites
php70w-bcmath.x86_64 : A module for PHP applications for using the bcmath
                     : library
php70w-cli.x86_64 : Command-line interface for PHP
php70w-common.x86_64 : Common files for PHP
php70w-dba.x86_64 : A database abstraction layer module for PHP applications
php70w-devel.x86_64 : Files needed for building PHP extensions
php70w-embedded.x86_64 : PHP library for embedding in applications
php70w-enchant.x86_64 : Enchant spelling extension for PHP applications
php70w-fpm.x86_64 : PHP FastCGI Process Manager
php70w-gd.x86_64 : A module for PHP applications for using the gd graphics
                 : library
php70w-imap.x86_64 : A module for PHP applications that use IMAP
php70w-interbase.x86_64 : A module for PHP applications that use
                        : Interbase/Firebird databases
php70w-intl.x86_64 : Internationalization extension for PHP applications
php70w-ldap.x86_64 : A module for PHP applications that use LDAP
php70w-mbstring.x86_64 : A module for PHP applications which need multi-byte
                       : string handling
php70w-mcrypt.x86_64 : Standard PHP module provides mcrypt library support
php70w-mysql.x86_64 : A module for PHP applications that use MySQL databases
php70w-mysqlnd.x86_64 : A module for PHP applications that use MySQL databases
php70w-odbc.x86_64 : A module for PHP applications that use ODBC databases
php70w-opcache.x86_64 : An opcode cache Zend extension
php70w-pdo.x86_64 : A database access abstraction module for PHP applications
php70w-pdo_dblib.x86_64 : MSSQL database module for PHP
php70w-pear.noarch : PHP Extension and Application Repository framework
php70w-pecl-apcu.x86_64 : APCu - APC User Cache
php70w-pecl-apcu-devel.x86_64 : APCu developer files (header)
php70w-pecl-imagick.x86_64 : Provides a wrapper to the ImageMagick library
php70w-pecl-imagick-devel.x86_64 : Imagick developer files (header)
php70w-pecl-mongodb.x86_64 : PECL package MongoDB driver
php70w-pecl-redis.x86_64 : Extension for communicating with the Redis key-value
                         : store
php70w-pecl-xdebug.x86_64 : PECL package for debugging PHP scripts
php70w-pgsql.x86_64 : A PostgreSQL database module for PHP
php70w-phpdbg.x86_64 : Interactive PHP debugger
php70w-process.x86_64 : Modules for PHP script using system process interfaces
php70w-pspell.x86_64 : A module for PHP applications for using pspell interfaces
php70w-recode.x86_64 : A module for PHP applications for using the recode
                     : library
php70w-snmp.x86_64 : A module for PHP applications that query SNMP-managed
                   : devices
php70w-soap.x86_64 : A module for PHP applications that use the SOAP protocol
php70w-tidy.x86_64 : Standard PHP module provides tidy library support
php70w-xml.x86_64 : A module for PHP applications which use XML
php70w-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC
                     : protocol

  Name and summary matches only, use "search all" for everything.

安装我们常用的模块:

yum -y install php70w-mysql php70w-xml php70w-soap php70w-xmlrpc php70w-mbstring php70w-json php70w-gd php70w-mcrypt php70w-opcache

重启一下apache httpd服务

systemctl restart httpd

通过php -v查看php版本

[root@localhost ~]# php -v
PHP 7.0.14 (cli) (built: Dec 10 2016 11:35:27) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

测试运行环境

vi /var/www/html/info.php

输入:

<?php
phpinfo();
?>

然后输入http://你的IP地址/info.php访问:

配置虚拟主机建立多站点请访问:

Post Comment