CentOS 6.3安装配置LAMP服务器(Linux+Apache+MySQL+PHP5)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介:
服务器系统环境:CentOS 6.3
客户端系统环境:Windows 7 ultimate(x86)sp1 简体中文旗舰版
 
本文档描述了如何在Linux服务器配置Apache、Mysql、PHP5
 
LAMP(Linux-Apache-MySQL-PHP)网站架构是目前国际流行的Web框架,该框架包括:Linux操作系统,Apache网络服务器,MySQL数据库,Perl、PHP或者Python编程语言,所有组成产品均是开源软件,是国际上成熟的架构框架,很多流行的商业应用都是采取这个架构,和Java/J2EE架构相比,LAMP具有Web资源丰富、轻量、快速开发等特点,微软的.NET架构相比,LAMP具有通用、跨平台、高性能、低价格的优势,因此LAMP无论是性能、质量还是价格都是企业搭建网站的首选平台。
 
安装前准备:
1、检查防火墙iptables状态
 [root@server ~]# service iptables status
注意:用法:iptables {start|stop|restart|condrestart|status|panic|save}
表格:filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source             destination         
1    ACCEPT     all  --  0.0.0.0/0           0.0.0.0/0           state RELATED,ESTABLISHED 
2    ACCEPT     icmp --  0.0.0.0/0           0.0.0.0/0           
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     tcp  --  0.0.0.0/0           0.0.0.0/0           state NEW tcp dpt:22 
5    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination      
 
查看本机关于 IPTABLES的配置前情况
[root@server ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere           anywhere            state RELATED,ESTABLISHED 
ACCEPT     icmp --  anywhere           anywhere            
ACCEPT     all  --  anywhere           anywhere            
ACCEPT     tcp  --  anywhere           anywhere            state NEW tcp dpt:ssh 
REJECT     all  --  anywhere            anywhere            reject-with icmp-host-prohibited 
 
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 
 
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   
 
2、配置防火墙,开启80端口、3306端口
[root@server ~]# vim /etc/sysconfig/iptables
添加:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT    #允许80(http)端口通过防火墙
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT  #允许3306(mysql)端口通过防火墙
备注:
很多网友把这两条规则添加到防火墙配置的最后一行,导致防火墙启动失败,正确的应该是添加到默认的22端口这条规则的下面。
如下所示:
# 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     #允许80(http)端口通过防火墙
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT   #允许3306(mysql)端口通过防火墙
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
:wq   #保存退出
 
重启防火墙使配置生效
[root@server ~]# service iptables restart
iptables:清除防火墙规则:                               [确定]
iptables:将链设置为政策 ACCEPT:filter                    [确定]
iptables:正在卸载模块:                                 [确定]
iptables:应用防火墙规则:                               [确定]
 
最后,检查防火墙iptables状态是否生效
[root@server ~]# service iptables status
表格:filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
5    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:80 
6    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:3306 
7    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination      
 
3、关闭SELINUX
[root@server ~]# vim /etc/selinux/config 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
#SELINUX=enforcing   #注释掉强制
SELINUX=disabled     #关闭
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
#SELINUXTYPE=targeted   #注释掉
:wq   #保存退出
 
重启系统
[root@server ~]# reboot
 
安装软件包:
4、安装Apache
检查是否安装httpd,使用YUM 自动安装:
[root@server ~]# rpm -qa |grep httpd
[root@server ~]# yum -y install httpd    #根据提示,输入Y安装即可成功安装
 
启动Apache
[root@server ~]# service httpd start
正在启动 httpd:httpd: apr_sockaddr_info_get() failed for server
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [确定]
 
设为开机启动
[root@server ~]# chkconfig httpd on
 
重启Apache
[root@server ~]# service httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:httpd: apr_sockaddr_info_get() failed for server
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [确定]
 
查看启动Apache运行状态
[root@server ~]# service httpd status
httpd (pid  1290) 正在运行...
                                                           
浏览器访问:http://10.1.4.44

                                                       

5、安装MySQL
检查是否安装mysql,使用YUM 自动安装:
[root@server ~]# rpm –qa |grep mysql
 [root@server ~]# yum -y install mysql mysql-server  #是否要安装,输入Y即可自动安装,直到安装完成
 
启动MySQL
[root@server ~]# service mysqld start
                                                           [确定]
正在启动 mysqld:                                          [确定]
 
设为开机启动
[root@server ~]# chkconfig mysqld on
 
[root@server ~]# cp /etc/my.cnf /etc/my.cnfbak
[root@server ~]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y   #拷贝配置文件(注意:如果/etc目录下面默认有一个my.cnf,直接覆盖即可)
 
为root账户设置密码
[root@server ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 
In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
 
Enter current password for root (enter for none):   #按回车键
OK, successfully used password, moving on...
 
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
 
Set root password? [Y/n] y      #输入y
New password:               #输入新密码
Re-enter new password:        #重输入新密码
Password updated successfully!
Reloading privilege tables..
 ... Success!
 
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
 
Remove anonymous users? [Y/n] y    #输入y
 ... Success!
 
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
 
Disallow root login remotely? [Y/n] y    #输入y
 ... Success!
 
By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
 
Remove test database and access to it? [Y/n] y    #输入y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
 
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
 
Reload privilege tables now? [Y/n] y    #输入y
 ... Success!
 
Cleaning up...
 
All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.
 
Thanks for using MySQL!    #最后出现:Thanks for using MySQL!
 
MySql密码设置完成,重新启动 MySQL:
[root@server ~]# service mysqld restart
停止 mysqld:                                              [确定]
正在启动 mysqld:                                          [确定]
[root@server ~]# service mysqld stop
停止 mysqld:                                              [确定]
[root@server ~]# service mysqld start
正在启动 mysqld:                                          [确定]
[root@server ~]# 
 
6、安装PHP5
检查是否安装php,使用YUM 自动安装:
[root@server ~]# rpm –qa |grep php
[root@server ~]# yum -y install php    #根据提示输入Y直到安装完成
 
安装PHP组件,使 PHP5 支持 MySQL
[root@server ~]# 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回车
 
重启MySql
[root@server ~]# service mysqld restart
停止 mysqld:                                              [确定]
正在启动 mysqld:                                          [确定]
 
重启Apche
[root@server ~]# service httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:httpd: apr_sockaddr_info_get() failed for server
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [确定]
 
配置编辑文件:                                                          
7、Apache配置
个人习惯,修改配置文件首先要备份。
[root@server ~]# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.confbak  #备份
[root@server ~]# vim /etc/httpd/conf/httpd.conf   #编辑文件
小提示:
:set nu    #显示行号
:set nonu  #取消行号
pgUp        #上一页
pgDn        #下一页
 
 44 ServerTokens OS  #在44行 修改为:ServerTokens Prod (在出现错误页的时候不显示服务器操作系统的名称)
536 ServerSignature On  #在536行 修改为:ServerSignature Off (在错误页中不显示Apache的版本)
331 Options Indexes FollowSymLinks  #在331行 修改为:Options Includes ExecCGI FollowSymLinks(允许服务器执行CGI及SSI,禁止列出目录)
796 #AddHandler cgi-script .cgi #在796行 取消“#” 修改为:AddHandler cgi-script .cgi .pl (允许扩展名为.pl的CGI脚本运行)
338 AllowOverride None  #在338行 修改为:AllowOverride All (允许.htaccess)
759 AddDefaultCharset UTF-8 #在759行 修改为:AddDefaultCharset GB2312 (添加GB2312为默认编码)
554 Options Indexes MultiViews FollowSymLinks #在554行 修改为 Options MultiViews FollowSymLinks(不在浏览器上显示树状目录结构)
402 DirectoryIndex index.html index.html.var #在402行 修改为:DirectoryIndex index.html index.htm Default.html Default.htm index.php Default.php index.html.var (设置默认首页文件,增加index.php)
 76 KeepAlive Off #在76行 修改为:KeepAlive On (允许程序性联机)
 83 MaxKeepAliveRequests 100  #在83行 修改为:MaxKeepAliveRequests 1000 (增加同时连接数)
:wq   #保存退出
 
重启
[root@server ~]# service httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:httpd: apr_sockaddr_info_get() failed for server
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [确定]
[root@server ~]# cp /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.confbak    #备份
[root@server ~]# cp /var/www/error/noindex.html /var/www/error/noindex.htmlbak       #备份
[root@server ~]# rm -f /etc/httpd/conf.d/welcome.conf /var/www/error/noindex.html     #删除默认2个测试页
 
重启
[root@server ~]# service httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:httpd: apr_sockaddr_info_get() failed for server
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [确定]
 
浏览器访问:http://10.1.4.44 

                                                                                       8、php配置

个人习惯,修改配置文件首先要备份。
[root@server ~]# cp /etc/php.ini /etc/php.inibak  #备份
[root@server ~]# vim /etc/php.ini              #编辑
小提示:
:set nu    #显示行号
:set nonu  #取消行号
pgUp        #上一页
pgDn        #下一页
 
946 ;date.timezone = PRC #在946行 把前面的分号去掉,改为date.timezone = PRC
386 disable_functions = #在386行 添加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列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。
432 expose_php = On #在432行 修改为:expose_php = Off禁止显示php版本的信息
745 magic_quotes_gpc = Off #在745行 修改为:magic_quotes_gpc = On打开magic_quotes_gpc来防止SQL注入
229 short_open_tag = Off #在229行 修改为short_open_tag = On,支持php短标签
380 ;open_basedir = #在380行 把前面的分号去掉,改为open_basedir = .:/tmp/ 设置表示允许访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录,可以防止php木马跨站,如果改了之后安装程序有问题,可以注销此行,或者直接写上程序的目录/data/www.osyunwei.com/:/tmp/
:wq   #保存退出
 
重启MySql
[root@server ~]# service mysqld restart
停止 mysqld:                                              [确定]
正在启动 mysqld:                                          [确定]
 
重启Apche
[root@server ~]# service httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:httpd: apr_sockaddr_info_get() failed for server
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [确定]
 
在/var/www/html/目录下,创建一个index.php文件,并且编辑,如下:
[root@server ~]# cd /var/www/html/
[root@server html]# ll
总用量 0
[root@server html]# touch index.php
[root@server html]# vim index.php   #编辑输入下面内容
<?php
phpinfo();
?>
:wq   #保存退出
 
在客户端浏览器输入服务器:http://10.1.4.44地址,可以看到相关的配置信息!

 

注意:
apache默认的程序目录是/var/www/html
权限设置:
[root@server ~]# chown apache.apache -R /var/www/html
 
至此,CentOS 6.3安装配置LAMP服务器(Linux+Apache+PHP5+MySQL)完成!



本文转自 yhw85 51CTO博客,原文链接:http://blog.51cto.com/yanghuawu/1062949,如需转载请自行联系原作者
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
打赏
0
0
0
0
265
分享
相关文章
阿里云特惠云服务器99元与199元配置与性能和适用场景解析:高性价比之选
2025年,阿里云长效特惠活动继续推出两款极具吸引力的特惠云服务器套餐:99元1年的经济型e实例2核2G云服务器和199元1年的通用算力型u1实例2核4G云服务器。这两款云服务器不仅价格亲民,而且性能稳定可靠,为入门级用户和普通企业级用户提供了理想的选择。本文将对这两款云服务器进行深度剖析,包括配置介绍、实例规格、使用场景、性能表现以及购买策略等方面,帮助用户更好地了解这两款云服务器,以供参考和选择。
|
12天前
|
KingBase服务器优化:详解Kylin参数配置。
通过适当的调整和优化这些关键参数,你的Kylin可以运行得更加流畅和高效。就像一个经过精心调校的赛车,无论是在赛道的直道还是弯道上,都能展现出卓越的性能。希望这次深入参数“操控盘”的旅行,能让你更好地理解和优化你的Kylin配置。记住,优化是一个持续的过程,不断地试验和改进,你的Kylin才能越来越强大。
42 20
源码编译安装LAMP(HTTP服务,MYSQL ,PHP,以及bbs论坛)
通过以上步骤,你可以成功地在一台Linux服务器上从源码编译并安装LAMP环境,并配置一个BBS论坛(Discuz!)。这些步骤涵盖了从安装依赖、下载源代码、配置编译到安装完成的所有细节。每个命令的解释确保了过程的透明度,使即使是非专业人士也能够理解整个流程。
45 18
|
26天前
|
Windows系统云服务器配置多用户登录
本教程介绍了在Windows云服务器上配置远程桌面服务的详细步骤,包括安装桌面会话主机和远程桌面授权、允许多用户远程连接以及配置新用户并加入远程桌面用户组。通过添加角色和功能、设置组策略以及管理用户权限,实现多用户同时登录和远程访问。按照指引操作,可顺利完成服务器的远程访问配置,提升管理和使用效率。
62 0
做网站如何选择云服务器?(一)配置篇
选择服务器需考虑网站规模、技术需求和地理位置。个人博客适合基础配置,企业官网需4核CPU、8GB内存,电商平台则需更高配置并配备负载均衡。技术上,展示型网站用PHP+MySQL,互动社区需Node.js/Python环境,视频类网站要大带宽。地理位置影响服务器选择,国内用户选腾讯云/阿里云,海外用户选AWS等。主流服务器类型包括虚拟主机(入门级)、云服务器(推荐)和物理服务器(高端需求)。云服务商普遍提供免费试用,便于评估性能。
64 1
【已解决】Matomo本地SMTP配置可以发邮件,但部署到阿里云ECS就发不了邮件
在阿里云ECS上使用Matomo和PHPMailer发送邮件时遇到问题,邮件无法发出且接口调用Pending。经过排查,发现是ECS安全组未开放25/465端口,导致SMTP请求无法正常通信。解决方法为在安全组中配置并开放25/465端口,从而恢复邮件发送功能。
在阿里云ECS云服务器上安装、配置及高效使用Docker与Docker Compose
本文介绍了在阿里云ECS上使用Ubuntu系统安装和配置Docker及Docker Compose的详细步骤。通过这些工具,可以快速部署、弹性扩展和高效管理容器化应用,满足开发和运维需求。内容涵盖Docker的安装、镜像源配置、创建Web程序镜像以及使用Docker Compose部署WordPress等实际操作,并分享了使用体验,展示了阿里云实例的高性能和稳定性。
311 4
阿里云国际站:阿里云服务器端口配置
悟空云@CloudWuKong阿里云是全球领先的云计算服务提供商,为用户提供弹性计算、数据库、存储、网络安全等一系列云计算服务。在使用阿里云服务器时,合理配置端口非常重要,可以提高服务器安全性和稳定性。
2025年阿里云服务器配置选择全攻略:CPU、内存、带宽与系统盘详解
在2025年,阿里云服务器以高性能、灵活扩展和稳定服务助力数字化转型,提供轻量应用服务器、通用型g8i实例等多样化配置,满足个人博客至企业级业务需求。针对不同场景(如计算密集型、内存密集型),推荐相应实例类型与带宽规划,强调成本优化策略,包括包年包月节省成本、ESSD云盘选择及地域部署建议。文中还提及安全设置、监控备份的重要性,并指出未来可关注第九代实例g9i支持的新技术。整体而言,阿里云致力于帮助用户实现性能与成本的最优平衡。 以上简介共计238个字符。
怎么根据自己的业务选择阿里云服务器配置大小?
本文指导如何根据业务需求精准选择阿里云服务器配置,涵盖个人轻量级至企业级、计算密集型等场景,推荐不同实例类型、存储与带宽方案,并提供成本优化策略,如包年包月节省成本、按需升级配置及选用性价比高的自研ARM架构实例。帮助用户在数字化转型中实现性能与成本的平衡。 注:以上配置与价格基于阿里云2025年官方数据,实际信息可能有所调整,请以官网实时页面为准。
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等