Home > php教程 > php手册 > 在CentOS6.4中安装配置LAMP环境的详细步骤

在CentOS6.4中安装配置LAMP环境的详细步骤

WBOY
Release: 2016-06-13 09:40:29
Original
987 people have browsed it

  本文详细介绍了CentOS6.4系统中安装LAMP服务并对其进行配置的过程,即安装Apache+PHP+Mysql,参照了网上大神的设置,其他Linux发行系统可以参考~

  在本文中部分命令操作需要root权限,输入‘’命令后输入密码即可切换root身份。

设置文件允许80端口和3306端口。因为80端口是http协议所使用的端口,如果防火墙禁止80端口的话,网站配置好了也无法从外网访问。3306端口是MySql数据库的默认端口。使用VIM打开iptables文件并添加规则:

 

<span 1</span> <span 打开iptables文件进行修改
</span><span 2</span> vim /etc/sysconfig/<span iptables
</span><span 3</span> 
<span 4</span> <span 加入下列两行
</span><span 5</span> -A INPUT -p tcp -m state --state NEW -m tcp --dport <span 80</span> -<span j ACCEPT
</span><span 6</span> -A INPUT -p tcp -m state --state NEW -m tcp --dport <span 3306</span> -j ACCEPT
Copy after login

  添加好后文件应该如下图,如果以前修改过该文件责可能和笔者的图中略有不同:

  

2. 关闭SELINUX

中提供的()系统。对于目前可用的 安全模块来说,SELinux 是功能最全面基本上都是用负载均衡设备做流量分发,对外仅开放了仅有的几个端口。所以综合安全性和复杂性来说,SELinux的性价比并不高。修改文件关闭SELinux,设置后如图:

<span 1</span> vim /etc/selinux/<span config
</span><span 2</span> 
<span 3</span> <span 注释掉如下两行,在行首添加#注释。
</span><span 4</span> SELINUX=<span enforcing
</span><span 5</span> SELINUXTYPE=<span targeted
</span><span 6</span> 
<span 7</span> <span 在后面增加:
</span><span 8</span> SELINUX=disabled
Copy after login

’重启系统。

<span  1</span> <span 通过yum软件包管理器安装MySql,管理器会自动安装依赖项,遇到询问直接输入y确认,直到显示&ldquo;Complete!&rdquo;完成。
</span><span  2</span> <span yum</span> <span install</span> mysql mysql-<span server
</span><span  3</span> 
<span  4</span> <span 启动MySql
</span><span  5</span> /etc/init.d/<span mysqld start
</span><span  6</span> 
<span  7</span> <span 设置MySql服务为开机启动
</span><span  8</span> <span chkconfig mysqld on
</span><span  9</span> 
<span 10</span> <span 复制MySql默认配置文件,直接覆盖/etc/my.cnf
</span><span 11</span> <span cp</span> /usr/share/mysql/my-medium.cnf /etc/my.cnf
Copy after login

MySql设置:

<span <span 输入如下命令后需要连续输入两次密码确认,有询问输入Y同意即可,成功后显示</span></span><span Thanks for using MySQL!。</span>
Copy after login
<span mysql_secure_installation</span>
Copy after login

  设置成功后需要重启MySql服务:

<span 1</span> /etc/init.d/<span mysqld stop
</span><span 2</span> /etc/init.d/mysqld start
Copy after login

2. Apache服务的安装和配置

  

<span  1</span> <span 安装httpd
</span><span  2</span> <span yum</span> <span install</span><span  httpd
</span><span  3</span> 
<span  4</span> <span 启动Apache服务
</span><span  5</span> /etc/init.d/<span httpd start
</span><span  6</span> 
<span  7</span> <span 编辑apache设置
</span><span  8</span> vim /etc/httpd/conf/<span httpd.conf
</span><span  9</span> <span 查找 <span #ServerName www.example.com:80</span></span>
<span 10</span> <span 修改成 <span ServerName</span> <span www.XXXX.com:80</span></span> 
<span 11</span> <span 其中的&ldquo;www.XXXX.com&rdquo;自己的域名,没有可设置为localhost,如图
</span><span 12</span> 
<span 13</span> <span 设置Apache服务开机启动
</span><span 14</span> <span chkconfig httpd on
</span><span 15</span> 
<span 16</span> <span 重启Apache服务
</span><span 17</span> /etc/init.d/httpd restart
Copy after login

3.安装PHP

  

<span 遇到询问直接输y确认
</span><span yum</span> <span install</span> php
Copy after login

  安装PHP插件:

<span 插件包括MySql支持等,遇到询问一如既往的y确认~~
</span><span yum</span> <span install</span> 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
Copy after login

  安装完成后不要忘了重启Apache和MySql:

/etc/init.d/<span mysqld restart
</span>/etc/init.d/httpd restart
Copy after login
Copy after login

  

  到此软件的安装就结束了,接下来进行配置和测试。

<span  1</span> <span 编辑文件Apache设置文件
</span><span  2</span> <span vi</span> /etc/httpd/conf/<span httpd.conf
</span><span  3</span> 
<span  4</span> <span <span 44行:</span> 修改 <span ServerTokens OS</span>  为 <span ServerTokens Prod
</span></span><span  5</span> <span 防止错误页显示操作系统名字
</span><span  6</span> 
<span  7</span> <span <span 76行:</span> 修改 <span KeepAlive Off</span> 为 <span KeepAlive On
</span></span><span  8</span> <span 允许程序性联机
</span><span  9</span> 
<span 10</span> <span 83行:</span> 修改 <span MaxKeepAliveRequests 100</span> 为 <span MaxKeepAliveRequests 1000</span>
<span 11</span> <span 增加同时连接数
</span><span 12</span> 
<span 13</span> <span <span 331行:</span> 修改 <span Options Indexes FollowSymLinks</span> 为 <span Options Includes ExecCGI FollowSymLinks
</span></span><span 14</span> <span 允许服务器执行CGI及SSI,防止列出目录
</span><span 15</span> 
<span 16</span> <span <span 338行:</span> 修改 <span AllowOverride None</span>  为 <span AllowOverride All
</span></span><span 17</span> <span 允许.htaccess
</span><span 18</span> 
<span 19</span> <span <span 402行:</span> 修改 <span DirectoryIndex index.html index.html.var</span>  为 <span DirectoryIndex index.php Default.php index.html index.htm Default.html Default.htm
</span></span><span 20</span> <span 添加php默认文档
</span><span 21</span> 
<span 22</span> <span <span 536行:</span>  修改 <span ServerSignature On</span> 为 ServerSignature Off 
</span><span 23</span> <span 防止错误页显示Apache版本
</span><span 24</span> 
<span 25</span> <span <span 554行:</span> 修改 <span Options Indexes MultiViews FollowSymLinks</span>  为 <span Options MultiViews FollowSymLinks
</span></span><span 26</span> <span 不显示树状目录结构
</span><span 27</span> 
<span 28</span> <span 759行:</span> 根据需要设置为 <span AddDefaultCharset UTF-8</span><span  或者 <span AddDefaultCharset GB2312
</span></span><span 29</span> <span 笔者大多数时候都在使用UTF-8编码,所以不进行修改
</span><span 30</span> 
<span 31</span> <span 796行:</span> 修改 <span #AddHandler cgi-script .cgi</span> 为 <span AddHandler cgi-script .cgi .pl
</span><span 32</span> <span 允许扩展名为.pl的CGI脚本运行</span>
Copy after login

  修改完成后保存退出并重启Apache,删除测试网页:

/etc/init.d/<span httpd restart

<span 删除测试网页
</span></span><span rm</span> -f /etc/httpd/conf.d/welcome.conf /var/www/error/noindex.html 
Copy after login

<span  1</span> <span 编辑php设置文件
</span><span  2</span> vim /etc/<span php.ini
</span><span  3</span> 
<span  4</span> <span 946行:</span> 修改 <span ;date.timezone = PRC</span> 为 <span date.timezone =</span><span <span  PRC</span>  <span (去掉分号)
</span></span><span  5</span> 
<span  6</span> <span 386行:</span> 修改 <span disable_functions =</span> 为 <span disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix_getppid,posix_getpwnam,posix_getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
</span><span  7</span> <span 设置PHP的禁用函数,若程序中需要使用的函数可以删除掉。
</span><span  8</span> 
<span  9</span> <span 432行:</span> 设置 <span expose_php = Off
</span><span 10</span> <span 禁止显示php版本的信息
</span><span 11</span> 
<span 12</span> <span 745行:</span> 设置 <span magic_quotes_gpc = On
</span><span 13</span> <span 打开magic_quotes_gpc,用于防止SQL注入
</span><span 14</span> 
<span 15</span> <span 229行:</span> 设置 <span short_open_tag = ON
</span><span 16</span> <span 支持php短标签
</span><span 17</span> 
<span 18</span> <span 380行:</span> 设置 <span open_basedir = .:/tmp/</span> 
<span 19</span> <span 设置允许访问的目录和/tmp/目录,防止php木马跨站</span>
Copy after login

  修改完成保存后重启服务:

/etc/init.d/<span mysqld restart
</span>/etc/init.d/httpd restart
Copy after login
Copy after login

  输入“

<span 1</span> <?<span php
</span><span 2</span>     <span phpinfo</span><span ();
</span><span 3</span> ?>
Copy after login

  保存退出后在浏览器中输入localhost查看php信息:

apache.apache -R /var/www/html

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template