LAMP环境搭建

来源:互联网 发布:虚拟化软件 编辑:程序博客网 时间:2024/06/07 14:40

一、更改防火墙

CentOS7默认使用的防火墙是firewall,我们将它更改为iptables:

1、关闭firewall:

sudo systemctl stop firewalld.service #停止firewall

sudo systemctl disable firewalld.service #禁止firewall开机启动

2、安装iptables:

sudo yum install iptables-services #安装iptables

编辑iptables配置文件:/etc/sysconfig/iptables ,开放80(http)及3306(mysql)端口:

-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

然后:

sudo systemctl start iptables.service #启动iptables

sudo systemctl enable iptables.service #开机启动iptables

二、关闭SELINUX

编辑/etc/selinux/config:

#SELINUX=enforcing #注释掉

#SELINUXTYPE=targeted #注释掉

SELINUX=disabled #增加

然后使配置立即生效:

sudo setenforce 0

三、安装Apache

sudo yum install httpd #根据提示,输入Y进行安装

然后启动Apache:

sudo systemctl start httpd.service

其他命令:

sudo systemctl stop httpd.service #停止Apache

sudo systemctl restart httpd.service #重启Apache

sudo systemctl enable httpd.service #开机启动Apache

然后打开浏览器输入:http://localhost进行测试。

四、安装MariaDB

CentOS7中,已经使用MariaDB代替了MySQL。

1、安装MariaDB:

sudo yum install mariadb mariadb-server #根据提示,输入Y进行安装

启动MariaDB:

sudo systemctl start mariadb.service

其他命令与httpd类似。

然后拷贝配置文件:

sudo cp /usr/share/mysql/mysql-huge.cnf /etc/my.cnf

2、为root账号设置密码:

sudo mysql_secure_installation

然后输入Y,再输入两次密码,回车。接下来根据提示进行操作,直到出现:Thanks for using MySQL!

至此密码设置完成,重启MariaDB:

sudo systemctl restart mariadb.service

五、安装PHP

1、安装PHP:

sudo yum install php #根据提示,输入Y进行安装

2、安装PHP组件

sudo yum install php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash

为了支持MariaDB,必须安装php-mysql组件,其他组件是否安装视自己需求而定。

然后重启MariaDB和Apache:

sudo systemctl restart mariadb.service

sudo systemctl restart httpd.service

至此安装工作就应经完成了,最后来进行配置。

六、配置

1、Apache配置:

sudo vi /etc/httpd/conf/httpd.conf #编辑文件

ServerSignature On  #添加,在错误页中显示Apache的版本,Off为不显示

Options Indexes FollowSymLinks  #修改为:Options Includes ExecCGI FollowSymLinks(允许服务器执行CGI及SSI,禁止列出目录)

#AddHandler cgi-script .cgi #修改为:AddHandler cgi-script .cgi .pl (允许扩展名为.pl的CGI脚本运行)

AllowOverride None  #修改为:AllowOverride All (允许.htaccess)

#Options Indexes FollowSymLinks   #修改为 Options FollowSymLinks(不在浏览器上显示树状目录结构)

DirectoryIndex index.html   #修改为:DirectoryIndex index.html index.htm Default.html Default.htmindex.php(设置默认首页文件,增加index.php)

MaxKeepAliveRequests 500  #添加MaxKeepAliveRequests 500 (增加同时连接数)

:wq! #保存退出

sudo systemctl restart httpd.service #重启apache

sudo rm -f /etc/httpd/conf.d/welcome.conf /var/www/error/noindex.html #删除默认测试页

2、PHP配置:

sudo vi /etc/php.ini #编辑

date.timezone = PRC #把前面的分号去掉,改为date.timezone = PRC

expose_php = Off #禁止显示php版本的信息

open_basedir = .:/tmp/  #设置表示允许访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录,可以防止php木马跨

:wq! #保存退出

sudo systemctl restart mariadb.service #重启MariaDB

sudo systemctl restart httpd.service #重启apache

3、最终测试:

cd /var/www/html

sudo vi index.php

<?php

phpinfo();

?>

:wq! #保存退出

然后在浏览器中输入:http://localhost(若在Apache配置中已将index.php加入到DirectoryIndex )或http://localhost/index.php进行测试,正常情况会出现PHP Version画面,在里面可以查看Apache及MySQL等组件情况。

七、其他

1、配置别名(alias)

sudo yum install、sudo systemctl start等冗长命令输入,实在是不爽!我们可以在~/.bashrc中配置别名,然后只要输入install或start就行了:

alias install='sudo yum install'

alias start='sudo systemctl start'

其他stop、restart等里面类似。

2、yum源

CentOS默认yum源是国外的,下载速度估计有点慢,源中的资源旧(国内的163 yum也不是很新),所以我们可以将其yum源设置为网易yum源。

1)备份yum源:

cd etc/yum.repos.d/

sudo mv CentOS-Base.repo CentOS-Base.repo.bak

2)下载CentOS7 yum源:

sudo wget http://mirrors.163.com/.help/CentOS7-Base-163.repo

3)运行以下命令生成缓存:

sudo yum clean all

sudo yum makecache

至此已更改完毕。

0 0