LAMP(Linux+Apache+Mysql/Mariadb+php)这个服务器架构被广泛使用,最近打算学习php,所以也来动手搭建搭建。
Linux版本:Centos 7.1.1503
Linux安装软件可以用rpm方式,也可以用yum方式,rpm方式比较麻烦一点,要安装一堆依赖包,嫌麻烦的我当然优先选择yum方式,因为yum能够帮你解决依赖包的问题,一个yum install xxx命令就能安装了。yum方式可以使用网络源,也可以使用本地源,在/etc/yum.repos.d/目录下有多个xxx.repo文件,其中CentOS-Base.repo是网络源的配置文件。一开始(默认)我使用的是网络源,yum源网址虽然可以ping通,但是执行yum install xxx的时候却没法连接,改成国内的yum源(如163,sofu)的也连接不上,折腾一段时间原因还是没找到,只好改成本地源,最终成功搭建lamp。
搭建过程:
1、制作本地yum源
上传CentOS7 ISO文件到/media/目录下,我这里的镜像是CentOS-7-x86_64-DVD-1503-01.iso,新建目录/media/cdrom,并将ISO文件挂载到/media/cdrom;
mount /media/CentOS-7-x86_64-DVD-1503-01.iso /media/cdrom/
进入/etc/yum.repos.d/路径下,将里面的xxx.repo改为xxx.repo.bak,以让他们失效,然后新建一个文件 CentOS-Media.repo,将以下内容复制到文件中保存即可。
# CentOS-Media.repo
[c6-media]
name=CentOS-$releasever - Media
baseurl=file:///media/cdrom
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
之后便可使用本地源yum安装软件。
2、安装Apache
yum install httpd #根据提示,输入Y安装即可成功安装
systemctl start httpd.service #启动apache
systemctl stop httpd.service #停止apache
systemctl restart httpd.service #重启apache
systemctl enable httpd.service #设置apache开机启动
此时访问服务器的ip还不能成功,需要配置如下内容:
(1)关闭firewall:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
(2)安装iptables防火墙
yum install iptables-services #安装
vi /etc/sysconfig/iptables #编辑防火墙配置文件
在iptables文件中添加两行,让防火墙放行80(http),3306(mysql)端口
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
保存文件后,
systemctl restart iptables.service #最后重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动
(3)关闭SELINUX
vi /etc/selinux/config
修改config,
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
setenforce 0 #使配置立即生效
配置好后,可以使用命令curl 127.0.0.1
本地查看apache是否成功,若打印出html内容,说明安装成功;或者可以在浏览器输入服务器IP,若显示如下页面,也说明安装成功。
Apache安装好了,问题是我到时写的网页放在哪里呢,打开/etc/httpd/conf/httpd.conf,找到文件中有一行:DocumentRoot “/var/www/html”,这里就是网页存放的路径,如在浏览器输入xx.xx.xx.xx/index.html,实际上是访问/var/www/html/index.html。
2、安装Mysql/Mariadb
CentOS默认是安装Mariadb的,哪怕yum install的是mysql,安装的包还是mariadb,其实两者是一脉相承的,差别不大,推荐使用mariadb,安装方法:
yum install mariadb mariadb-server #询问是否要安装,输入Y即可自动安装,直到安装完成
systemctl start mariadb.service #启动MariaDB
systemctl stop mariadb.service #停止MariaDB
systemctl restart mariadb.service #重启MariaDB
systemctl enable mariadb.service #设置开机启动
此时直接输入命令mysql可以进入数据库,为了安全,为root设置密码,
mysql_secure_installation
执行如上命令,MySQL 会要求你提供现在 root 用户的密码,由于刚刚装好,所以密码是空的,直接回车,然后出现如下对话:
Set root password? [Y/n] y #是否设置root密码
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL 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] y #阻止是否root用户从远程登陆,如果选择y ,那只能以localhost方式登陆
... Success!
By default, MySQL 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 #是否移除 test 数据库
- 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...
设置好后进入数据库:mysql -uroot -p(-u:user,-p:password),输入密码即可进入。
3、安装php
yum install php php-mysql #php-mysql将php与mysql联系起来,按y完成安装
service httpd restart #重启apache
测试php是否安装成功
#切换到默认网页存放目录
cd /var/www/html
#创建一个 php 脚本文件
touch phpinfo.php
#在phpinfo.php敲入
echo '<?php phpinfo(); ?>' > phpinfo.php
在浏览器输入xx.xx.xx.xx/phpinfo.php,若能显示相关配置信息,说明安装成功。