Linux - CentOS7+Apache+MySQL+PHP(LAMP)环境搭建

安装Apache

一、安装

yum -y install httpd

二、开启apache服务

systemctl start httpd.service

三、设置apache服务开机启动

systemctl enable httpd.service

四、验证apache服务是否安装成功

CentOS7查看ip地址的方式为:

ip addr

在本机浏览器中输入虚拟机的ip地址,如果看到apache默认的页面–有Testing 123… 字样,便是成功安装了apache服务了。

访问应该是失败的,原因如下:
查了资料,说法是,CentOS7用的是Firewall-cmd,CentOS7之前用的是iptables防火墙;要想让外网能访问到apache主目录,就需要做以下的操作:

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

然后再访问外网ip,就可以了。

安装PHP

一、安装(默认版本)

yum -y install php

二、重启apache服务

systemctl restart httpd.service

三、测试php环境

定位目录:

vi /var/www/html/info.php

注意:如果你安装了可视化界面(如xftp),你可以直接定位 /var/www/html/ 新建一个php文件。

定位目录后,在 info.php 中插入以下代码:

<?php
 phpinfo(); 
?>

然后,在自己电脑浏览器输入 10.17.1.223/info.php ,运行后会出现php的一些信息即为成功!

注意:IP地址请自行更改

安装MySQL

一、安装

yum install mariadb mariadb-server mariadb-libs mariadb-devel

二、开启MySQL服务

systemctl start mariadb.service

三、设置开机启动MySQL服务

systemctl enable mariadb.service

四、数据库安全设置

mysql_secure_installation

请按照如下加粗样式进行确认设置,请按照自己的需求填写 Y or N:

[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] y
New password:
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] y
… 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] n
… skipping.

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] y

  • 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] y
… 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!
————————————————————————————————————

五、登陆数据库测试

mysql -uroot -p

[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.52-MariaDB MariaDB Server

Copyright © 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
±-------------------+
3 rows in set (0.02 sec)

MariaDB [(none)]>

PHP与MySQL进行关联

yum -y install php-mysql

安装常用的PHP模块

例如,GD库,curl,mbstring,…

yum -y install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel

环境测试

首先重启一下Apache服务器,命令:systemctl restart httpd.service

在浏览器中输入:

10.17.1.223/info.php

服务器IP + /info.php,请自行修改。

  1. 如果页面出现了PHP相关模块信息,紫色区块,则表示LAMP环境搭建成功。
  2. 如果已有php程序,只需要把整个php程序上传到 /var/www/html 目录下即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王佳斌

请作者喝杯咖啡 :)

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值