linux上搭建Php+Mysql+Apache
Posted Suebingo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux上搭建Php+Mysql+Apache相关的知识,希望对你有一定的参考价值。
一、Linux安装phpstudy(mysql版本过低,php版本过低,该方法不可取)
1.输入wget -c http://lamp.phpstudy.NET/phpstudy.bin 回车
2.输入chmod +x phpstudy.bin 回车 ,这一步用于设置权限,要在root权限下操作
3. 输入./phpstudy.bin 回车,执行安装。
4. 输入php -v 和mysql -v提示
[root@ecs-9d91-0003 suebingo]# php
-bash: php: command not found
[root@ecs-9d91-0003 suebingo]# mysql
-bash: mysql: command not found
5. find / -name mysql 和 find / -name php
6. ln -s /phpstudy/mysql/bin/mysql /usr/bin 将mysql链接到bin目录,解决:-bash: mysql: command not found
7. ln -s /phpstudy/server/php/bin/php /usr/bin 将php链接到bin目录下
8. /phpstudy/server/php/bin/php /tmp/Suebingo/index.php运行php程序文件(phpstudy安装方式运行)
9. 非phpstudy安装方式运行:/usr/bin/php /tmp/suebingo/nfc_attendance_system_server/index.php
10. mysql -v 提示
[root@ecs-9d91-0003 suebingo]# mysql -v
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
11. 解决9步 MYSQL5.3版本解决密码忘记问题
(1)执行phpstudy stop
(2)打开vim /etc/my.cnf,找到 [mysqld]这个模块:在最后面添加一段代码skip-grant-tables ##忽略mysql权限问题,直接登录
(3)执行phpstudy start
(4)后执行mysql -u root -p,后不输入密码直接回车
(5)执行mysql> use mysql; ##使用mysql数据库,提示Database changed
(6)执行mysql> update user set password=password("te**123456") where user="root";##更新密码
(7)执行mysql> flush privileges;##刷新权限
数据报错:
ERROR 1293 (HY000) at line 12: Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
原因:两个数据库的版本不同
二、Linux安装mysql8.0 (建议使用该方法)
安装wget下载工具:yum -y install wget
(1)wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
(2)yum localinstall mysql80-community-release-el7-1.noarch.rpm
(3)yum repolist enabled | grep "mysql.*-community.*"
(4)yum install mysql-community-server
(5)启动mysql服务
启动:systemctl start mysqld或者service mysqld start
关闭:service mysqld stop
查看启动状态:systemctl status mysqld或者service mysqld status
设置开机启动:systemctl enable mysqld 和systemctl daemon-reload
Mysql 8.0.19版本解决密码忘记问题:linux中
(1)查看初始安装密码sudo grep 'temporary password' /var/log/mysqld.log
初始密码为:2020-03-02T10:27:09.640012Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 1xE4+e<#8BLO
(2)mysql -u root -p 以初始密码登录
(3)ALTER USER 'root'@'localhost' IDENTIFIED BY '20**te*****@'; 修改root密码为20**te*****@
Linux下彻底卸载mysql详解
⑴ 使用以下命令查看当前安装mysql情况,查找以前是否装有mysql
rpm -qa|grep -i mysql
显示之前安装了:
MySQL-client-5.5.25a-1.rhel5
MySQL-server-5.5.25a-1.rhel5
⑵ 停止mysql服务、删除之前安装的mysql
删除命令:rpm -e –nodeps 包名
rpm -ev MySQL-client-5.5.25a-1.rhel5
rpm -ev MySQL-server-5.5.25a-1.rhel5
如果提示依赖包错误,则使用以下命令尝试:
rpm -ev MySQL-client-5.5.25a-1.rhel5 --nodeps
如果提示错误:error: %preun(xxxxxx) scriptlet failed, exit status 1
则用以下命令尝试:
rpm -e --noscripts MySQL-client-5.5.25a-1.rhel5
⑶ 查找之前老版本mysql的目录、并且删除老版本mysql的文件和库
find / -name mysql
查找结果如下:
find / -name mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/lib64/mysql
删除对应的mysql目录
rm -rf /var/lib/mysql
rm -rf /var/lib/mysql
rm -rf /usr/lib64/mysql
具体的步骤如图:查找目录并删除
注意:卸载后/etc/my.cnf不会删除,需要进行手工删除
rm -rf /etc/my.cnf
⑷ 再次查找机器是否安装mysql
rpm -qa|grep -i mysql
⑸ 总结
无结果,说明已经卸载彻底,接下来直接安装mysql即可。
三、在centos7上安装php7.3(不建议)
(1)首先安装 EPEL 源:yum install epel-release
(2)安装 REMI 源:yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
(3)安装 Yum 源管理工具:yum install yum-utils
(4)安装 PHP7.3:
yum install -y php73-php-fpm php73-php-cli php73-php-bcmath php73-php-gd php73-php-json php73-php-mbstring php73-php-mcrypt php73-php-mysqlnd php73-php-opcache php73-php-pdo php73-php-pecl-crypto php73-php-pecl-mcrypt php73-php-pecl-geoip php73-php-recode php73-php-snmp php73-php-soap php73-php-xmll
(5)安装完成后最好重启一下计算机,不然有可能无法启动php
设置开机启动、运行服务:
systemctl enable php73-php-fpm
systemctl start php73-php-fpm
(6)查找安装包:rpm -qa | grep 'php'
找到包:php73-php-fpm-7.3.15-1.el7.remi.x86_64
(7)查php73-php-fpm-7.3.15-1.el7.remi.x86_64安装位置:rpm -ql php73-php-fpm-7.3.6-1.el7.remi.x86_64
找到/opt/remi/php73/root/usr/sbin/php-fpm缩短为/opt/remi/php73/
(8)查找php.ini位置:find /etc/opt/remi/php73 -name php.ini
显示为:/etc/opt/remi/php73/php.ini
(9)编辑/etc/opt/remi/php73/php.ini替换换 ;cgi.fix_pathinfo=1 为 cgi.fix_pathinfo=0
快捷命令:sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/opt/remi/php73/php.ini
(10)重启php73-php-fpm
命令:systemctl restart php73-php-fpm
(11)更多操作:
systemctl restart php73-php-fpm #重启
systemctl start php73-php-fpm #启动
systemctl stop php73-php-fpm #关闭
systemctl status php73-php-fpm #检查状态
(12)查看 PHP 验证一下是否安装成功:php73 -v
(13)更新 PHP 运行下面的命令系统就会更新所有可以更新的软件包括 PHP 更新命令:yum update
(14)php73 index.php 使用该方式运行php文件,而非php index.php
四、Linux安装Apache
1.yum install httpd
如下:
#ServerName www.example.com:80 改为 ServerName localhost:80
#Listen 改为 Listen:8080(linux中开放的端口号80XX)
3. service httpd start 启动
service httpd stop 停止
service httpd status 查看状态
五、linux安装php(版本过低不建议使用该方法)
1、安装php;
yum install php
2、重启httpd;
service httpd restart
3、apache默认页面路径/var/www/html下新建一个test.php页面;
4、带端口号在所有已联网的电脑的浏览器输入:http://139.159.178.10:8080/test.php
[root@ecs-9d91-0005 ~]# vi /etc/httpd/conf/httpd.conf
[root@ecs-9d91-0005 ~]# firewall-cmd --permanent --add-port=9090/tcp
FirewallD is not running
[root@ecs-9d91-0005 ~]# getenforce
Disabled
[root@ecs-9d91-0005 ~]# systemctl restart httpd
[root@ecs-9d91-0005 ~]# firewall-cmd --permanent --add-port=9090/tcp
success
[root@ecs-9d91-0005 ~]# firewall-cmd --permanent --add-port=9527/tcp
success
[root@ecs-9d91-0005 ~]# firewall-cmd --reload
success
[root@ecs-9d91-0005 ~]# firewall-cmd --permanent --add-port=3306/tcp
success
[root@ecs-9d91-0005 ~]#
[root@ecs-9d91-0005 ~]#
[root@ecs-9d91-0005 ~]# firewall-cmd --permanent --add-port=3306/udp
success
[root@ecs-9d91-0005 ~]# firewall-cmd --permanent --add-port=9527/udp
success
[root@ecs-9d91-0005 ~]#
[root@ecs-9d91-0005 ~]# firewall-cmd --permanent --add-port=9090/udp
success
[root@ecs-9d91-0005 ~]#
[root@ecs-9d91-0005 ~]#
[root@ecs-9d91-0005 ~]# firewall-cmd --reload
success
[root@ecs-9d91-0005 ~]#
[root@ecs-9d91-0005 ~]#
[root@ecs-9d91-0005 ~]#
[root@ecs-9d91-0005 ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: dhcpv6-client ssh
ports: 9090/tcp 9527/tcp 3306/tcp 3306/udp 9527/udp 9090/udp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[root@ecs-9d91-0005 ~]# systemctl stop firewalld
[root@ecs-9d91-0005 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
创建虚拟主机
[root@ecs-9d91-0005 ~]# cd /etc/httpd/conf
conf/ conf.d/ conf.modules.d/
[root@ecs-9d91-0005 ~]# cd /etc/httpd/conf.d/
[root@ecs-9d91-0005 conf.d]# ll
total 20
-rw-r--r-- 1 root root 2926 Aug 8 2019 autoindex.conf
-rw-r--r-- 1 root root 625 Jan 27 00:00 php.conf
-rw-r--r-- 1 root root 366 Aug 8 2019 README
-rw-r--r-- 1 root root 1252 Aug 6 2019 userdir.conf
-rw-r--r-- 1 root root 824 Aug 6 2019 welcome.conf
[root@ecs-9d91-0005 conf.d]# cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf server.conf
[root@ecs-9d91-0005 conf.d]# ll
total 24
-rw-r--r-- 1 root root 2926 Aug 8 2019 autoindex.conf
-rw-r--r-- 1 root root 625 Jan 27 00:00 php.conf
-rw-r--r-- 1 root root 366 Aug 8 2019 README
-rw-r--r-- 1 root root 1511 Mar 22 21:28 server.conf
-rw-r--r-- 1 root root 1252 Aug 6 2019 userdir.conf
-rw-r--r-- 1 root root 824 Aug 6 2019 welcome.conf
[root@ecs-9d91-0005 conf.d]# vim server.conf
[root@ecs-9d91-0005 conf.d]# systemctl restart httpd
server.conf文件信息:
<VirtualHost 139.158.178.10:7080>
DocumentRoot "/var/www/html"
</VirtualHost>
5、关联php和mysql
(1)搜索模块
[root@tele-2 ~]# yum search php
(2)安装相关模块
[root@tele-2 ~]# yum install php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc
6、安装完毕为5.4.16版本
六、linux安装php(建议使用该方法,版本为7.2.27)
1、首先查看是否有老版本
yum list installed | grep php
2、如果安装的有【remove后按1中实际显示】
yum remove php.x86_64 php-cli.x86_64 php-common.x86_64 php-gd.x86_64 php-ldap.x86_64 php-mbstring.x86_64 php-mcrypt.x86_64 php-mysql.x86_64 php-pdo.x86_64
3、由于linux的yum源不存在php7.x,所以我们要更改yum源:
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
4、查看yum源中有没有php7.x
yum search php7
5、yum 安装php72w和各种拓展,选自己需要的即可:
yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml
6、查看php版本
php -v(版本为7.2.27)
以上是关于linux上搭建Php+Mysql+Apache的主要内容,如果未能解决你的问题,请参考以下文章
LNMP环境搭建(linux+Nginx + Mysql + PHP)
Linux学习总结(三十九)lnmp环境搭建1-mysql,php 安装