CentOS7搭建lamp

来源:互联网 发布:史丹利快报的淘宝店 编辑:程序博客网 时间:2024/04/26 22:44

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 - Mediabaseurl=file:///media/cdromgpgcheck=1enabled=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

之后便可使用本地源yum安装软件。

2、安装Apache

yum install httpd #根据提示,输入Y安装即可成功安装systemctl start httpd.service #启动apachesystemctl stop httpd.service #停止apachesystemctl restart httpd.service #重启apachesystemctl enable httpd.service #设置apache开机启动

此时访问服务器的ip还不能成功,需要配置如下内容:
(1)关闭firewall:

systemctl stop firewalld.service #停止firewallsystemctl 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-prohibitedCOMMIT

保存文件后,

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 #启动MariaDBsystemctl stop mariadb.service #停止MariaDBsystemctl restart mariadb.service #重启MariaDBsystemctl 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 anyoneto log into MySQL without having to have a user account created forthem.  This is intended only for testing, and to make the installationgo a bit smoother.  You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] y #是否移除匿名登陆 ... Success!Normally, root should only be allowed to connect from 'localhost'.  Thisensures 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 canaccess.  This is also intended only for testing, and should be removedbefore 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 farwill 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,若能显示相关配置信息,说明安装成功。

0 0
原创粉丝点击