• LeekinDeveloper@Gmail.com

Centos install and configure LAMP


Linux Centos6.5搭建LAMP

这是一个装饰图

这一久因为帮一个朋友搭建免流服务,基于centos系统来进行PHP运行环境的搭建,这种黑客技术没什么技术含量,就是搭建运行环境,再把准备好的脚本传上去就可以了,今天做一个总结,还有个人的一些感悟

首先是LAMP(Linux+Apache+Mysql+PHP)

我们都是站在巨人的肩膀上,这些工具都经历了很多年的版本迭代,已经很成熟,我们我要做的就是学会使用工具来让我们的工作更高效

  1. 前期准备

    配置防火墙,开启80端口,3306端口

    1
    2
    3
    $ vim /etc/sysconfig/iptables #使用VIM打开iptables这个文件
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT #允许80端口通过防火墙
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT #允许3306端口通过防火墙

note:将这两行代码粘贴到开启22端口那行代码的下面,而不是最后

1
/etc/init.d/iptables restart #重启防火墙使配置生效

关闭SELINUX

1
2
3
4
5
6
$ vim /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq!#保存退出
shutdown -r now#重启系统
  1. 安装lamp

    安装MySQL

    note:linux不像windows,下载一个wamp一键配置好环境,linux需要分别安装每个软件,并进行相应的配置。在开始之前,确保你的linux已经有C++的编译器GCC,和包管理器:yum
    1
    2
    3
    4
    yum install mysql mysql-server #询问是否要安装,输入Y即可自动安装,直到安装完成
    /etc/init.d/mysqld start #启动MySQL
    chkconfig mysqld on #设为开机启动
    cp /usr/share/mysql/my-medium.cnf /etc/my.cnf #拷贝配置文件(note:如果/etc目录下面默认有一个my.cnf,直接覆盖即可)

为MySQL设置root密码

1
2
$ mysqladmin -u root password "newpass"  #newpass是你的密码
$ /etc/init.d/mysqld restart #重启mysql服务,如果你觉得写这么长的路径很麻烦,可以写一个sh脚本

安装PHP5

1
yum install php #根据提示输入Y直到安装完成

安装PHP组件,使 PHP5 支持 MySQL

1
2
3
yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt #这里选择以上安装包进行安装,根据提示输入Y回车
/etc/init.d/mysqld restart #重启MySql
/etc/init.d/httpd restart #重启Apche

安装Apache

1
2
yum install httpd #根据提示,输入Y安装即可成功安装
/etc/init.d/httpd start #启动Apache

note:如果出现错误提示:httpd:httpd: Could not reliably determine the server’s fully qualif domain name, using ::1 for ServerName

解决方案:
1
$ vim /etc/httpd/conf/httpd.conf  #编辑

找到 #ServerName www.example.com:80
修改为 ServerName www.leekin.com:80 #这里设置为你自己的域名,如果没有域名,可以设置为localhost ,保存退出

1
2
$ chkconfig httpd on #设为开机启动
$ /etc/init.d/httpd restart #重启Apache

  1. 配置

    配置Apache

1
vi /etc/httpd/conf/httpd.conf #编辑文件
  1. 在44行: ServerTokens OS修改为:ServerTokens Prod (在出现错误页的时候不显示服务器操作系统的名称)
  2. 在76行: KeepAlive Off 修改为:KeepAlive On (允许程序性联机)
  3. 在83行: MaxKeepAliveRequests 100 修改为:MaxKeepAliveRequests 1000 (增加同时连接数)
  4. 在331行:Options Indexes FollowSymLinks  修改为:Options Includes ExecCGI FollowSymLinks(允许服务器执行CGI及SSI,禁止列出目录)
  5. 在338行 :AllowOverride None修改为:AllowOverride All (允许.htaccess)
  6. 在402行:DirectoryIndex index.html index.html.var 修改为:DirectoryIndex index.html index.htm Default.html Default.htm index.php Default.php index.html.var (设置默认首页文件,增加index.php)
  7. 在536行:ServerSignature On 修改为:ServerSignature Off (在错误页中不显示Apache的版本)
  8. 在554行:Options Indexes MultiViews FollowSymLinks 行 修改为 Options MultiViews FollowSymLinks(不在浏览器上显示树状目录结构)
  9. 在759行:AddDefaultCharset UTF-8 修改为:AddDefaultCharset GB2312 (添加GB2312为默认编码)这步不是必须的,根据自己的需要改
  10. 在796行:AddHandler cgi-script .cgi 修改为:AddHandler cgi-script .cgi .pl (允许扩展名为.pl的CGI脚本运行)
    保存退出

配置PHP

1
vim /etc/php.ini #编辑
  1. short_open_tag = ON #在229行支持php短标签
  2. open_basedir = .:/tmp/ #在380行 设置表示允许访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录,可以防止php木马跨站,如果改了之后安装程序有问题,可以注销此行,或者直接写上程序的目录/data/www.osyunwei.com/:/tmp/
  3. #在386行 列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。
  4. expose_php = Off #在432行 禁止显示php版本的信息
  5. magic_quotes_gpc = On #在745行 打开magic_quotes_gpc来防止SQL注入
1
2
/etc/init.d/mysqld restart #重启MySql
/etc/init.d/httpd restart #重启Apche
  1. 测试一下

    到这一步,如果不出什么意外的话,我们的安装配置工作就结束了,我们需要写一个简单的PHP脚本来测试一下

    1
    $ vim /var/www/html/index.php #编辑输入下面内容
1
2
3
<?php
phpinfo();
?>

浏览器输入你的ip地址,应该会显示你的php信息,如果不出什么意外的话,如果什么都没显示,应该检查你的安装与配置过程,查看报错,或者是浏览器报错提示你没有html/index.php这个文件的访问权限,这时候你需要用chmod命令给予它777的访问权限。

测试MySQL

1
$ vim /var/www/html/testMysql.php # 输入以下内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$db_host='localhost ';//主机名
$db_database='testMysql';//数据库名
$db_username='root';//用户名
$db_password='password';//你刚刚设置的密码
$connection=mysql_connect($db_host,$db_username,$db_password);//连接到数据库
mysql_query("set names 'utf8'");//编码转化
if(!$connection){
die("could not connect to the database:</br>".mysql_error());//诊断连接错误
}
else
{
echo "connect mysql success!";
}
mysql_close($connection);//关闭连接
?>

当然,在测试之前,必须先建一个数据库

1
2
$ mysql -u root -p # 输入密码进入数据库
mysql>create database testMysql;

执行这个php文件,查看是否能够链接到数据库!