相关的环境搭建有很多介绍。参考了网上别人的经验,可以较快的搭建起来。这里记录了我个人的环境搭建过程。
这次的环境搭建,都是通过yum进行安装的,而没有通过编译源码进行安装。

先来看目标版本,这是安装后的信息:

点击(此处)折叠或打开

  1. # uname -a
  2. Linux localhostww.localdomain 4.1.12-112.14.13.el7uek.x86_64 #2 SMP Thu Jan 18 11:38:29 PST 2018 x86_64 x86_64 x86_64 GNU/Linux

  3. # httpd -version
  4. Server version: Apache/2.4.6 ()
  5. Server built: Apr 11 2018 05:56:48

  6. # mysqld --version
  7. /usr/sbin/mysqld Ver 8.0.11 for Linux on x86_64 (MySQL Community Server - GPL)

  8. # php -v
  9. PHP 7.1.19 (cli) (built: Jun 25 2018 13:58:17) ( NTS )
  10. Copyright (c) 1997-2018 The PHP Group
  11. Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies

1. 安装Apahce (httpd)


1.1 安装


这个很简单:

点击(此处)折叠或打开

  1. # yum install httpd


1.2 小插曲

然而,遇到了个小问题:

点击(此处)折叠或打开

  1. Loaded plugins: langpacks, ulninfo
  2. Existing lock /var/run/yum.pid: another copy is running as pid 2657.
  3. Another app is currently holding the yum lock; waiting for it to exit...

这是个常见的问题,原因是yum在尝试更新。
我们来查一下进程以及父进程信息:

点击(此处)折叠或打开

  1. # ps -ef | grep yum
  2. root 2657 1571 0 07:23 ? 00:00:00 /usr/bin/python /usr/share/PackageKit/helpers/yum/yumBackend.py get-updates none

  3. # ps -ef | grep 1571
  4. root 1571 1 0 07:21 ? 00:00:01 /usr/libexec/packagekitd
  5. root 2657 1571 0 07:23 ? 00:00:00 /usr/bin/python /usr/share/PackageKit/helpers/yum/yumBackend.py get-updates none

把父进程1571杀掉,然后再查,此时已经没有相关进程了。

点击(此处)折叠或打开

  1. # kill -9 1571
  2. # ps -ef | grep yum | grep -v grep
  3. #


1.3 再次安装

再通过yum安装httpd,一切OK。

点击(此处)折叠或打开

  1. # yum install httpd

1.4 启动httpd, 测试


默认的httpd是80端口,先查看一下;很好,这个端口没有被监听。

点击(此处)折叠或打开

  1. # netstat -an | grep 80

启动 httpd

点击(此处)折叠或打开

  1. # systemctl start httpd.service

再看一下监听端口,有了:

点击(此处)折叠或打开

  1. # netstat -an | grep 80
  2. tcp6 0 0 :::80 :::*

也可以通过浏览器来查看apache默认的测试界面:

点击(此处)折叠或打开

  1. http://localhost:80

2. 安装PHP


2.1 检查并删除现有的PHP版本


参考连接

最终,执行如下命令查看是否已经完全删除:

点击(此处)折叠或打开

  1. # php -v
  2. bash: php: command not found...
  3. # rpm -qa|grep php                         <-- 没有返回就对了


2.2 下载rpm并安装

我是通过yum安装的,所以可以掠过此处.
参考官方连接

根据上述描述,需要下载php 7.1.9的rpm,可以从这里下载。

2.3 更新yum的repo,并且安装PHP

仍旧参考官方连接。
更新yum的repo:

点击(此处)折叠或打开

  1. # cd /etc/yum.repos.d
  2. # mv public-yum-ol7.repo public-yum-ol7.repo.bak
  3. # wget http://yum.oracle.com/public-yum-ol7.repo

在repo中设置要安装的PHP版本:

点击(此处)折叠或打开

  1. # yum install yum-utils
  2. # yum-config-manager --enable ol7_developer_php71

安装

点击(此处)折叠或打开

  1. # yum install php


2.4 测试安装

点击(此处)折叠或打开
  1. # which php
  2. /usr/bin/php
  3. # php -v
  4. PHP 7.1.19 (cli) (built: Jun 25 2018 13:58:17) ( NTS )
  5. Copyright (c) 1997-2018 The PHP Group
  6. Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies

2.5 安装其他相关组件

这些在后续开发中会用到,一起安装了吧

点击(此处)折叠或打开

  1. yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash libmcrypt

遇到一个不能成功安装的,暂时略过,不影响环境搭建

点击(此处)折叠或打开

  1. # yum install php-mcrypt
  2. Loaded plugins: langpacks, ulninfo
  3. Resolving Dependencies
  4. --> Running transaction check
  5. ---> Package php-mcrypt.x86_64 0:5.4.16-7.el7 will be installed
  6. --> Processing Dependency: php(zend-abi) = 20100525-64 for package: php-mcrypt-5.4.16-7.el7.x86_64
  7. --> Processing Dependency: php(api) = 20100412-64 for package: php-mcrypt-5.4.16-7.el7.x86_64
  8. --> Finished Dependency Resolution
  9. Error: Package: php-mcrypt-5.4.16-7.el7.x86_64 (epel)
  10. Requires: php(api) = 20100412-64
  11. Installed: php-common-7.1.19-1.0.1.el7.x86_64 (@ol7_developer_php71)
  12. php(api) = 20160303-64

检查一下我们的安装:

点击(此处)折叠或打开

  1. # yum list installed | grep php
  2. gd-last.x86_64 2.1.1-2.el7 @ol7_developer_php71
  3. php.x86_64 7.1.19-1.0.1.el7 @ol7_developer_php71
  4. php-bcmath.x86_64 7.1.19-1.0.1.el7 @ol7_developer_php71
  5. php-cli.x86_64 7.1.19-1.0.1.el7 @ol7_developer_php71
  6. php-common.x86_64 7.1.19-1.0.1.el7 @ol7_developer_php71
  7. php-gd.x86_64 7.1.19-1.0.1.el7 @ol7_developer_php71
  8. php-imap.x86_64 7.1.19-1.0.1.el7 @ol7_developer_php71
  9. php-ldap.x86_64 7.1.19-1.0.1.el7 @ol7_developer_php71
  10. php-mbstring.x86_64 7.1.19-1.0.1.el7 @ol7_developer_php71
  11. php-mysqlnd.x86_64 7.1.19-1.0.1.el7 @ol7_developer_php71
  12. php-odbc.x86_64 7.1.19-1.0.1.el7 @ol7_developer_php71
  13. php-pdo.x86_64 7.1.19-1.0.1.el7 @ol7_developer_php71
  14. php-pear.noarch 1:1.9.4-21.el7 @ol7_latest
  15. php-process.x86_64 7.1.19-1.0.1.el7 @ol7_developer_php71
  16. php-xml.x86_64 7.1.19-1.0.1.el7 @ol7_developer_php71
  17. php-xmlrpc.x86_64 7.1.19-1.0.1.el7 @ol7_developer_php71

2.6 测试PHP是否工作

在"/etc/www/html"下创建这样一个测试文件:
(不知什么原因,简单的插入php代码,会导致我后面的章节统统给删掉;在经历了2次这样的事情后,还是简单的在代码中加入几个##吧)。

  1. #touch /etc/www/html/index.php
  2. with content
  3. ##<##?php
  4. phpinfo();
  5. ##?##>

在浏览器里访问"http://localhost:80/index.php",结果不对
需要重新启动httpd,之后就OK了。

  1. # syystemctl start httpd.service

3. 安装mysql

参考链接

3.1 添加mysql的repo

这里下载mysql的repo,我下载的是"mysql80-community-release-el7-1.noarch.rpm"
然后添加到本地repo里:

  1. # yum localinstall mysql80-community-release-el7-1.noarch.rpm

  2. # yum repolist enabled | grep"mysql.*-community.*"

    mysql-connectors-community/x86_64 MySQLConnectorsCommunity                 51

    mysql-tools-community/x86_64     MySQL ToolsCommunity                      63

    mysql80-community/x86_64         MySQL 8.0 CommunityServer                 17

也可以直接查看文件/etc/yum.repos.d/mysql-community.repo

  1. [mysql80-community]
  2. name=MySQL 8.0 Community Server
  3. baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
  4. enabled=1     
  5. gpgcheck=1
  6. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
也可以通过下面的命令来需改需要安装的版本,我需要安装8.0,所以就没有进行这个操作。

  1. # sudo yum-config-manager --disable mysql57-community
  2. # sudo yum-config-manager --enable mysql80-community

3.2  安装mysql


  1. # sudo yum install mysql-community-server

  2. # yum list installed | grep mysql
  3. mysql-community-client.x86_64 8.0.11-1.el7 @mysql80-community
  4. mysql-community-common.x86_64 8.0.11-1.el7 @mysql80-community
  5. mysql-community-libs.x86_64 8.0.11-1.el7 @mysql80-community
  6. mysql-community-libs-compat.x86_64 5.7.16-1.el7 @Server-Mysql/7.3
  7. mysql-community-server.x86_64 8.0.11-1.el7 @mysql80-community
  8. mysql80-community-release.noarch el7-1 installed
  9. php-mysqlnd.x86_64 7.1.19-1.0.1.el7 @ol7_developer_php71

3.3 启动


  1. # systemctl start mysqld.service

3.4 获取并修改临时密码

密码存在log文件里,获取临时密码后,修改密码
  1. # grep 'temporary password' /var/log/mysqld.log
  2. 2018-06-28T07:40:46.220757Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: fh!dh_o)z8fD


# mysql -uroot -p'fh!dh_o)z8fD'

mysql> ALTER USER 'root'@'localhost'IDENTIFIED BY 'HHhh0011_2233';   



4. 总体测试 (Aphache/PHP/Mysql)

4.1 创建数据库


  1. mysql> create database root_db1;
  2. mysql> use root_db1;
  3. mysql> create table MyGuests(id int auto_increment, firstname varchar(20), lastname varchar(20), primary key (id));
  4. mysql> desc MyGuests;
  5. +-----------+-------------+------+-----+---------+----------------+
  6. | Field | Type | Null | Key | Default | Extra |
  7. +-----------+-------------+------+-----+---------+----------------+
  8. | id | int(11) | NO | PRI | NULL | auto_increment |
  9. | firstname | varchar(20) | YES | | NULL | |
  10. | lastname | varchar(20) | YES | | NULL | |
  11. +-----------+-------------+------+-----+---------+----------------+
  12. 3 rows in set (0.00 sec)

  13. mysql> insert into MyGuests (firstname, lastname) values( 'aa', 'bb');
  14. mysql> insert into MyGuests (firstname, lastname) values( 'xx', 'yy');
  15. mysql> select * from MyGuests;
  16. +----+-----------+----------+
  17. | id | firstname | lastname |
  18. +----+-----------+----------+
  19. | 1 | aa | bb |
  20. | 2 | xx | yy |
  21. +----+-----------+----------+
  22. 2 rows in set (0.01 sec)

4.2 创建php代码


  1. ##<span style="font-size:14px;"##>
  2. ##<##?php
  3. $servername = "127.0.0.1";
  4. $port=3306;
  5. $username = "root";
  6. $password = "HHhh0011_2233";
  7. $dbname = "root_db1";
  8. $socket="mysqli.default_socket";
  9. // Create connection
  10. $conn = new mysqli($servername, $username, $password, $dbname,$port,$socket);
  11. // Check connection
  12. if ($conn->connect_error) {
  13.     die("Connection failed 1: " . $conn->connect_error);
  14. }
  15. $sql = "SELECT id, firstname, lastname FROM MyGuests";
  16. $result = $conn->query($sql);

  17. if ($result->num_rows > 0) {
  18.     // output data of each row
  19.     while($row = $result->fetch_assoc()) {
  20.         echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
    "
    ;
  21.     }
  22. } else {
  23.     echo "0 results";
  24. }
  25. $conn->close();
  26. ##?##>

4.3 测试


直接测试PHP程序,报错。
  1. #php /var/www/testMysql01.php
  2. return err:
  3. PHP Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in /var/www/html/testMysql01.php on line 10
  4. PHP Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in /var/www/html/testMysql01.php on line 10
  5. Connection failed 1: The server requested authentication method unknown to the client
原因是 php 7.1的 mysqli 组件不支持 mysql 8的密码加密方式"caching_sha2_password",需要修改为 "mysql_native_password"。

  1. # mysql -uroot -pHHhh0011_2233
  2. mysql> select user,plugin from mysql.user;
  3. +------------------+-----------------------+
  4. | user | plugin |
  5. +------------------+-----------------------+
  6. | mysql.infoschema | mysql_native_password |
  7. | mysql.session | mysql_native_password |
  8. | mysql.sys | mysql_native_password |
  9. | root | caching_sha2_password|  
  10. +------------------+-----------------------+
  11. mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '0racle_Nis';
  12. mysql> select user,plugin from mysql.user;
  13. +------------------+-----------------------+
  14. | user | plugin |
  15. +------------------+-----------------------+
  16. | mysql.infoschema | mysql_native_password |
  17. | mysql.session | mysql_native_password |
  18. | mysql.sys | mysql_native_password |
  19. | root | mysql_native_password |
  20. +------------------+-----------------------+
  21. mysql> exit
再次测试,OK!

  1. # php testMysql01.php
  2. id: 1 - Name: aa bb
    id: 2 - Name: xx yy







10-05 17:34