Linux Centos6.5搭建LAMP
这一久因为帮一个朋友搭建免流服务,基于centos系统来进行PHP运行环境的搭建,这种黑客技术没什么技术含量,就是搭建运行环境,再把准备好的脚本传上去就可以了,今天做一个总结,还有个人的一些感悟
首先是LAMP(Linux+Apache+Mysql+PHP) 我们都是站在巨人的肩膀上,这些工具都经历了很多年的版本迭代,已经很成熟,我们我要做的就是学会使用工具来让我们的工作更高效
前期准备配置防火墙,开启80端口,3306端口 1 2 3 $ vim /etc/sysconfig/iptables -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
note:将这两行代码粘贴到开启22端口那行代码的下面,而不是最后 1 /etc/init.d/iptables restart
关闭SELINUX 1 2 3 4 5 6 $ vim /etc/selinux/config SELINUX=disabled :wq! shutdown -r now
安装lamp安装MySQL note:linux不像windows,下载一个wamp一键配置好环境,linux需要分别安装每个软件,并进行相应的配置。在开始之前,确保你的linux已经有C++的编译器GCC,和包管理器:yum 1 2 3 4 yum install mysql mysql-server /etc/init.d/mysqld start chkconfig mysqld on cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
为MySQL设置root密码 1 2 $ mysqladmin -u root password "newpass" $ /etc/init.d/mysqld restart
安装PHP5
安装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 /etc/init.d/mysqld restart /etc/init.d/httpd restart
安装Apache 1 2 yum install httpd /etc/init.d/httpd start
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 vi /etc/httpd/conf/httpd.conf
在44行: ServerTokens OS修改为:ServerTokens Prod (在出现错误页的时候不显示服务器操作系统的名称)
在76行: KeepAlive Off 修改为:KeepAlive On (允许程序性联机)
在83行: MaxKeepAliveRequests 100 修改为:MaxKeepAliveRequests 1000 (增加同时连接数)
在331行:Options Indexes FollowSymLinks 修改为:Options Includes ExecCGI FollowSymLinks(允许服务器执行CGI及SSI,禁止列出目录)
在338行 :AllowOverride None修改为:AllowOverride All (允许.htaccess)
在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)
在536行:ServerSignature On 修改为:ServerSignature Off (在错误页中不显示Apache的版本)
在554行:Options Indexes MultiViews FollowSymLinks 行 修改为 Options MultiViews FollowSymLinks(不在浏览器上显示树状目录结构)
在759行:AddDefaultCharset UTF-8 修改为:AddDefaultCharset GB2312 (添加GB2312为默认编码)这步不是必须的,根据自己的需要改
在796行:AddHandler cgi-script .cgi 修改为:AddHandler cgi-script .cgi .pl (允许扩展名为.pl的CGI脚本运行)保存退出
配置PHP
short_open_tag = ON #在229行支持php短标签
open_basedir = .:/tmp/ #在380行 设置表示允许访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录,可以防止php木马跨站,如果改了之后安装程序有问题,可以注销此行,或者直接写上程序的目录/data/www.osyunwei.com/:/tmp/
#在386行 列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。
expose_php = Off #在432行 禁止显示php版本的信息
magic_quotes_gpc = On #在745行 打开magic_quotes_gpc来防止SQL注入
1 2 /etc/init.d/mysqld restart /etc/init.d/httpd restart
测试一下到这一步,如果不出什么意外的话,我们的安装配置工作就结束了,我们需要写一个简单的PHP脚本来测试一下 1 $ vim /var/www/html/index.php
浏览器输入你的ip地址,应该会显示你的php信息,如果不出什么意外的话,如果什么都没显示,应该检查你的安装与配置过程,查看报错,或者是浏览器报错提示你没有html/index.php这个文件的访问权限,这时候你需要用chmod命令给予它777的访问权限。
测试MySQL 1 $ vim /var/www/html/test Mysql.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 test Mysql;
执行这个php文件,查看是否能够链接到数据库!