linux(centos) 搭建 PHP 环境相关配置:
1、Mysql 5.6
2、PHP 5.4
3、Apache 2.2
1. 安装 mysql,启动 mysql
2. 配置 yum 源
3. rpm 升级 PHP 版本
4. yum 安装 PHP及扩展
5. 启动 Apache 服务
6. 部署代码
7. 修改 Apache 配置文件(可选)
8. 重启 Apache 服务,关闭防火墙
9. 客户端打开服务器网址
验证 mysql 是否安装
rpm -qa | grep -i mysql
[root@localhost ~]# rpm -qa | grep -i mysql
# 安装过,则有多个 mysql 文件
# 未安装则无文件
已安装过 mysql ,需重新安装 mysql,可参考:Linux 重新安装 Mysql
未安装过 mysql
(1)在 /opt 目录下创建 mysql 目录,并上传 mysql 安装包
(2)进入 /opt/mysql 目录,安装 mysql
1、cd /opt/mysql/
2、ls -al
3、 rpm -ivh MySQL-client-5.6.48-1.el6.i686.rpm
rpm -ivh MySQL-shared-5.6.48-1.el6.i686.rpm
rpm -ivh MySQL-shared-compat-5.6.48-1.el6.i686.rpm
rpm -ivh MySQL-server-5.6.48-1.el6.i686.rpm
[root@localhost ~]# cd /opt/mysql/
[root@localhost mysql]# ls -al
total 89216
drwxr-xr-x. 2 root root 4096 Mar 3 13:38 .
drwxr-xr-x. 4 root root 4096 Mar 3 13:38 ..
-rw-r--r--. 1 root root 20893788 Mar 11 2020 MySQL-client-5.6.48-1.el6.i686.rpm
-rw-r--r--. 1 root root 64038720 Mar 11 2020 MySQL-server-5.6.48-1.el6.i686.rpm
-rw-r--r--. 1 root root 2235116 Mar 11 2020 MySQL-shared-5.6.48-1.el6.i686.rpm
-rw-r--r--. 1 root root 4173024 Mar 11 2020 MySQL-shared-compat-5.6.48-1.el6.i686.rpm
[root@localhost mysql]# rpm -ivh MySQL-client-5.6.48-1.el6.i686.rpm
warning: MySQL-client-5.6.48-1.el6.i686.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:MySQL-client ########################################### [100%]
[root@localhost mysql]# rpm -ivh MySQL-shared-5.6.48-1.el6.i686.rpm
warning: MySQL-shared-5.6.48-1.el6.i686.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:MySQL-shared ########################################### [100%]
[root@localhost mysql]# rpm -ivh MySQL-shared-compat-5.6.48-1.el6.i686.rpm
warning: MySQL-shared-compat-5.6.48-1.el6.i686.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:MySQL-shared-compat ########################################### [100%]
[root@localhost mysql]# rpm -ivh MySQL-server-5.6.48-1.el6.i686.rpm
warning: MySQL-server-5.6.48-1.el6.i686.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:MySQL-server ########################################### [100%]
...... 初始化数据库 ......
(3)验证 mysql 是否被安装成功,查看 mysql 服务状态
service mysql status
# 查看 mysql 服务状态
# mysql 安装成功,mysql 服务非启动状态
[root@localhost mysql]# service mysql status
MySQL is not running[FAILED]
# mysql 安装失败,mysql 服务无法识别
[root@localhost mysql]# service mysql status
mysql: unrecognized service
(4)启动 mysql 服务,查看 root 用户的密码:ROvVVC_2cf9jIXUX
1、service mysql start
2、cd /root
3、ls -al .mysql_secret
4、cat /root/.mysql_secret
[root@localhost ~]# service mysql start
Starting MySQL.[ OK ]
[root@localhost mysql]# cd /root
[root@localhost ~]# ls -al .mysql_secret
-rw-------. 1 root root 104 Apr 18 18:45 .mysql_secret
[root@localhost ~]# cat /root/.mysql_secret
# The random password set for the root user
at Sun Apr 18 18:45:25 2021 (local time): ROvVVC_2cf9jIXUX
(5)使用该密码,进入 mysql 修改密码,刷新授权表
1、mysql -u root -p
2、set password for root@localhost=password('123456');
3、flush privileges;
[root@localhost ~]# mysql -u root -p
Enter password: ROvVVC_2cf9jIXUX
Welcome to the MySQL monitor.
mysql> set password for root@localhost=password('123456');
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> Ctrl-C -- exit!
Aborted
(6)退出 mysql(Ctrl + C),再以新密码进入 mysql,验证密码是否修改成功
[root@localhost ~]# mysql -u root -p
Enter password: 123456
Welcome to the MySQL monitor.
(7)mysql 服务命令
service mysql status
# 启动 mysql 服务
# status:状态
# start:启动
# stop:关闭
# restart:重启
具体可参考:Linux 配置 yum 源
(1) 输入 yum list 查看安装包时,出现以下错误
[root@localhost ~]# yum list
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
YumRepo Error: All mirror URLs are not using ftp, http[s] or file.
Eg. Invalid release/repo/arch combination/
removing mirrorlist with no valid mirrors: /var/cache/yum/i386/6/base/mirrorlist.txt
Error: Cannot find a valid baseurl for repo: base
(2) 解决方法:修改系统时间和镜像文件,再获取安装包
1、date
2、date -s "本地时间"
3、echo "https://vault.centos.org/6.7/os/i386/" > /var/cache/yum/i386/6/base/mirrorlist.txt
4、echo "https://vault.centos.org/6.7/extras/i386/" > /var/cache/yum/i386/6/extras/mirrorlist.txt
5、echo "https://vault.centos.org/6.7/updates/i386/" > /var/cache/yum/i386/6/updates/mirrorlist.txt
6、yum list
[root@localhost ~]# date
Wed Mar 3 13:38:40 CST 2021
[root@localhost ~]# date -s "2021/4/17 16:04:00"
Sat Apr 17 16:04:00 CST 2021
[root@localhost ~]# echo "https://vault.centos.org/6.7/os/i386/" > /var/cache/yum/i386/6/base/mirrorlist.txt
[root@localhost ~]# echo "https://vault.centos.org/6.7/extras/i386/" > /var/cache/yum/i386/6/extras/mirrorlist.txt
[root@localhost ~]# echo "https://vault.centos.org/6.7/updates/i386/" > /var/cache/yum/i386/6/updates/mirrorlist.txt
[root@localhost ~]# yum list
rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
# rpm 升级 PHP 版本
[root@localhost mysql]# rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
Retrieving http://mirror.webtatic.com/yum/el6/latest.rpm
warning: /var/tmp/rpm-tmp.SnZumn: Header V4 DSA/SHA1 Signature, key ID cf4c4ff9: NOKEY
Preparing... ########################################### [100%]
1:webtatic-release ########################################### [100%]
(1)查看 php56 版本及扩展
yum list | grep -i php56w*
[root@localhost mysql]# yum list | grep -i php56w*
(2)yum 安装 php
yum install -y php56w.i386
# -y:默认全部选择 yes
[root@localhost mysql]# yum install -y php56w.i386
1、检查依赖
2、列出将要安装的软件包
3、下载软件包
4、安装软件包
5、校验软件包是否安装=
(3)yum 安装 php 扩展
1、yum install -y php56w-gd.i386
2、yum install -y php56w-mysql.i386
3、yum install -y php56w-mbstring.i386
[root@localhost ~]# yum install -y php56w-gd.i386
[root@localhost ~]# yum install -y php56w-mysql.i386
[root@localhost ~]# yum install -y php56w-mbstring.i386
(4)yum 安装时可能 pid 冲突,会导致以下问题:
Another app is currently holding the yum lock; waiting for it to exit...
The other application is: PackageKit
Memory : 30 M RSS ( 66 MB VSZ)
Started: Sun Apr 18 20:41:12 2021 - 01:16 ago
State : Sleeping, pid: 6995
解决方法:查找并杀死 yum 进程,重新安装
ps -ef | grep -i yum
# 第二列为 pid
kill -9 pid
# 杀死进程
[root@localhost ~]# ps -ef | grep -i yum
root 3050 2749 10 11:57 ? 00:00:05 /usr/bin/python /usr/share/PackageKit/helpers/yum/yumBackend.py get-updates none
root 3049 2854 0 11:58 pts/0 00:00:00 grep -i yum
# yum-pid:第一行的第二列
[root@localhost ~]# kill -9 3050
(5)查看 php 版本
php -v
[root@localhost ~]# php -v
PHP 5.6.40 (cli) (built: Jan 12 2019 09:38:57)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
(1)验证 Apache 是否安装(一般默认已安装)
yum list | grep -i httpd
[root@localhost ~]# yum list | grep -i httpd
httpd.i686 2.2.15-45.el6.centos @anaconda-CentOS-201508042139.i386/6.7
httpd-tools.i686 2.2.15-45.el6.centos @anaconda-CentOS-201508042139.i386/6.7
httpd.i686 2.2.15-47.el6.centos.4 updates
httpd-devel.i686 2.2.15-47.el6.centos.4 updates
httpd-manual.noarch 2.2.15-47.el6.centos.4 updates
httpd-tools.i686 2.2.15-47.el6.centos.4 updates
libmicrohttpd.i686 0.9.33-4.el6 base
libmicrohttpd-devel.i686 0.9.33-4.el6 base
libmicrohttpd-doc.noarch 0.9.33-4.el6 base
(2)启动 Apache 服务
1、service httpd status
2、service http start
[root@localhost ~]# service httpd status
httpd is stopped
[root@localhost ~]# service httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
(1)在 /var/www/html 目录下,上传代码包:
(2)在 /var/www/html 目录下,解压代码包,所有文件解压在 html 目录下
1、cd /var/www/html
2、ls -al
3、unzip tpshop.zip
4、ls -al
[root@localhost html]# cd /var/www/html
[root@localhost html]# ls -al
total 918956
drwxr-xr-x. 2 root root 4096 Apr 17 12:23 .
drwxr-xr-x. 6 root root 4096 Mar 1 18:23 ..
-rw-r--r--. 1 root root 940997992 Apr 19 2021 tpshop.zip
[root@localhost html]# unzip tpshop.zip
[root@localhost html]# ls -al
total 920644
drwxr-xr-x. 13 root root 4096 Apr 17 12:26 .
drwxr-xr-x. 6 root root 4096 Mar 1 18:23 ..
drwxr-xr-x. 3 root root 4096 Sep 17 2019 addons
drwxr-xr-x. 9 root root 4096 Sep 27 2019 application
drwxr-xr-x. 2 root root 4096 Sep 17 2019 backup
drwxr-xr-x. 2 root root 4096 Sep 17 2019 extend
-rw-r--r--. 1 root root 4286 Sep 17 2019 favicon.ico
-rw-r--r--. 1 root root 1653632 Sep 17 2019 hgzb.ttf
-rw-r--r--. 1 root root 701 Sep 17 2019 .htaccess
-rw-r--r--. 1 root root 2902 Sep 19 2019 index.php
drwxr-xr-x. 6 root root 4096 Sep 27 2019 install
-rw-r--r--. 1 root root 2819 Sep 17 2019 nginx.conf2
drwxr-xr-x. 7 root root 4096 Sep 19 2019 plugins
drwxr-xr-x. 11 root root 4096 Sep 19 2019 public
-rw-r--r--. 1 root root 178 Sep 17 2019 robots.txt
drwxr-xr-x. 5 root root 4096 Apr 11 15:12 runtime
drwxr-xr-x. 5 root root 4096 Sep 19 2019 template
drwxr-xr-x. 5 root root 4096 Sep 27 2019 thinkphp
-rw-r--r--. 1 root root 940997992 Apr 19 2021 tpshop.zip
drwxr-xr-x. 21 root root 4096 Sep 20 2019 vendor
-rw-r--r--. 1 root root 899 Apr 11 15:08 安装前必看.txt
(3)如果解压出来,所有文件在 html 目录下的一个子文件夹中(如: tpshop),但域名后不能圈套目录,有以下两种解决方法
1、mv ./tpshop/* ./
# ./:当前目录
# 将 tpshop 目录下的所有目录文件移动到当前目录
2、cd tpshop
3、ls -al
# 查看隐藏文件,如:.htaccess
2、mv ./tpshop/.htaccess ./
# 将隐藏文件移动到当前目录
1、find / -name httpd.conf
# /:根目录
# 从根目录查找 Apache 配置文件 httpd.conf
2、vi /etc/httpd/conf/httpd.conf
# 修改配置文件
httpd.conf 中的操作:
1、/Listen
# /:命令行模式下搜索
# n:命令行模式下搜索下一个,next
# 进入配置文件,在命令行模式下,搜索 Listen 位置
2、Listen 8001
# 添加新的监听端口号
3、<VirtualHost *:8001>
DocumentRoot /var/www/html/tpshop
ServerName localhost
</VirtualHost>
# 在末尾,配置虚拟主机
[root@localhost html]# find / -name httpd.conf
/etc/httpd/conf/httpd.conf
[root@localhost html]# vi /etc/httpd/conf/httpd.conf
Listen 8001
<VirtualHost *:8001>
DocumentRoot /var/www/html/tpshop
ServerName localhost
</VirtualHost>
(4)给 html 目录下的所有目录文件递归授权
chmod -R 777 ./*
[root@localhost html]# chmod -R 777 ./*
1、find / -name httpd.conf
# /:根目录
# 从根目录查找 Apache 配置文件 httpd.conf
2、vi /etc/httpd/conf/httpd.conf
# 修改配置文件
httpd.conf 中的操作:
1、/AllowOverride None
# /:命令行模式下搜索
# n:命令行模式下搜索下一个,next
# 进入配置文件,在命令行模式下,搜索 AllowOverride None 位置
2、将 None 改成 All
# 修改前两个 AllowOverride None
1、service httpd restart
# 重启 Apache 服务
2、service iptables stop
# 关闭防火墙
[root@localhost html]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
[root@localhost html]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]