linux LAMP 平台快速搭建(rpm)

LAMP,也就是Linux+Apache+MySQL+php,经典的web平台,这是简单的讲一下,快速的搭建,并使用

LAMP平台,其它中的L,代表的是linux系统,A 代表的是apache,是一个web软件,M代表的是mysql,是一个数据库,P代表的是php, 也就是 linux + apache + mysql + php 这几个的缩写,为LAMP。

采用这种架构的优点,不必多说,想必大家都了解一二,这些都是开源的软件,并且linux平台比windows平台占用资源低,网上也可以找到N多的文档来支持,你所遇到的问题,绝大多数都可以在网上找到,唯一的缺点,就是linux针对比较专业化,主要针对服务器,在服务器市场,占有率是相当高的,而对于桌面市场,则是windows的天下。

好了,废话不多说,这次简单的来说一下LAMP平台的搭建。此教程使用 yum 安装,适用于对 apache mysql 以及 php版本没有要求的,如有有要求必须要用某某版本的话,则需要使用编译安装。

本次所使用的linux 系统是 CentOS 6.7,版本查看,可以使用如下两个命令

[root@localhost ~]# cat /etc/issue
CentOS release 6.7 (Final)
Kernel \r on an \m

[root@localhost ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)
[root@localhost ~]#

查看系统是否安装了LAMP平台

[root@localhost ~]# rpm -qa | grep -e httpd -e mysql -e php
mysql-libs-5.1.73-5.el6_6.x86_64
[root@localhost ~]#

我这里是没有安装的,如果安装过,则会出来相应的包。

这里 rpm -qa 是显示所有已经安装过的 rpm 包,| 是管道符,这个里标准 IO,这里不做详细描述,grep 则是过滤显示,这里过滤三个词,是 apache mysql php,过虑 rpm -qa 的输出。

由于是快速搭建,这里我们使用 yum 安装,暂时不讲源代码编译安装。

在安装之前,我们可以先查看一下,都有哪些包,可供安装的,使用 yum list 来显示可用的包,再使用 grep 来过滤我们想要的包。

[root@localhost ~]# yum list | grep -e http -e mysql -e php
mysql-libs.x86_64 5.1.73-5.el6_6 @anaconda-CentOS-201508042137.x86_64/6.7
apr-util-mysql.x86_64 1.3.9-3.el6_0.1 base
bacula-director-mysql.x86_64 5.0.0-13.el6 base
bacula-storage-mysql.x86_64 5.0.0-13.el6 base
cups-php.x86_64 1:1.4.2-74.el6 base
dovecot-mysql.x86_64 1:2.0.9-22.el6 base
freeradius-mysql.x86_64 2.2.6-6.el6_7 base
graphviz-php.x86_64 2.26.0-10.el6 base
httpd.x86_64 2.2.15-54.el6.centos updates
httpd-devel.i686 2.2.15-54.el6.centos updates
httpd-devel.x86_64 2.2.15-54.el6.centos updates
httpd-manual.noarch 2.2.15-54.el6.centos updates
httpd-tools.x86_64 2.2.15-54.el6.centos updates
jakarta-commons-httpclient.x86_64 1:3.1-0.9.el6_5 base
jakarta-commons-httpclient-demo.x86_64 1:3.1-0.9.el6_5 base
jakarta-commons-httpclient-javadoc.x86_64 1:3.1-0.9.el6_5 base
jakarta-commons-httpclient-manual.x86_64 1:3.1-0.9.el6_5 base
libdbi-dbd-mysql.x86_64 0.8.3-5.1.el6 base
libmicrohttpd.i686 0.9.33-4.el6 base
libmicrohttpd.x86_64 0.9.33-4.el6 base
libmicrohttpd-devel.i686 0.9.33-4.el6 base
libmicrohttpd-devel.x86_64 0.9.33-4.el6 base
libmicrohttpd-doc.noarch 0.9.33-4.el6 base
mod_auth_mysql.x86_64 1:3.0.0-11.el6_0.1 base
mysql.x86_64 5.1.73-7.el6 base
mysql-bench.x86_64 5.1.73-7.el6 base
mysql-connector-java.noarch 1:5.1.17-6.el6 base
mysql-connector-odbc.x86_64 5.1.5r1144-7.el6 base
mysql-devel.i686 5.1.73-7.el6 base
mysql-devel.x86_64 5.1.73-7.el6 base
mysql-embedded.i686 5.1.73-7.el6 base
mysql-embedded.x86_64 5.1.73-7.el6 base
mysql-embedded-devel.i686 5.1.73-7.el6 base
mysql-embedded-devel.x86_64 5.1.73-7.el6 base
mysql-libs.i686 5.1.73-7.el6 base
mysql-libs.x86_64 5.1.73-7.el6 base
mysql-server.x86_64 5.1.73-7.el6 base
mysql-test.x86_64 5.1.73-7.el6 base
pcp-pmda-mysql.x86_64 3.10.9-6.el6 base
php.x86_64 5.3.3-47.el6 base
php-bcmath.x86_64 5.3.3-47.el6 base
php-cli.x86_64 5.3.3-47.el6 base
php-common.x86_64 5.3.3-47.el6 base
php-dba.x86_64 5.3.3-47.el6 base
php-devel.x86_64 5.3.3-47.el6 base
php-embedded.x86_64 5.3.3-47.el6 base
php-enchant.x86_64 5.3.3-47.el6 base
php-fpm.x86_64 5.3.3-47.el6 base
php-gd.x86_64 5.3.3-47.el6 base
php-imap.x86_64 5.3.3-47.el6 base
php-intl.x86_64 5.3.3-47.el6 base
php-ldap.x86_64 5.3.3-47.el6 base
php-mbstring.x86_64 5.3.3-47.el6 base
php-mysql.x86_64 5.3.3-47.el6 base
php-odbc.x86_64 5.3.3-47.el6 base
php-pdo.x86_64 5.3.3-47.el6 base
php-pear.noarch 1:1.9.4-5.el6 base
php-pecl-apc.x86_64 3.1.9-2.el6 base
php-pecl-apc-devel.i686 3.1.9-2.el6 base
php-pecl-apc-devel.x86_64 3.1.9-2.el6 base
php-pecl-memcache.x86_64 3.0.5-4.el6 base
php-pgsql.x86_64 5.3.3-47.el6 base
php-process.x86_64 5.3.3-47.el6 base
php-pspell.x86_64 5.3.3-47.el6 base
php-recode.x86_64 5.3.3-47.el6 base
php-snmp.x86_64 5.3.3-47.el6 base
php-soap.x86_64 5.3.3-47.el6 base
php-tidy.x86_64 5.3.3-47.el6 base
php-xml.x86_64 5.3.3-47.el6 base
php-xmlrpc.x86_64 5.3.3-47.el6 base
php-zts.x86_64 5.3.3-47.el6 base
qt-mysql.i686 1:4.6.2-28.el6_5 base
qt-mysql.x86_64 1:4.6.2-28.el6_5 base
rrdtool-php.x86_64 1.3.8-7.el6 base
rsyslog-mysql.x86_64 5.8.10-10.el6_6 base
rsyslog7-mysql.x86_64 7.4.10-5.el6 base
uuid-php.x86_64 1.6.1-10.el6 base
[root@localhost ~]#

从上面的信息中,我们可以看到,除去 httpd 和 mysql 的包以外,php的包很多,以 php- 开头,代表的是php的扩展包,如 php-mysql 是表示php支持mysql,php-pdo,php-mbstring 等这些,都是php的一些扩展,可根据需要来决定是否需要安装。这里我们将常用的扩展装上。

使用 yum install 安装。

[root@localhost ~]# yum install httpd httpd-devel mysql mysql-server mysql-devel php php-bcmath php-cli php-devel php-gd php-mbstring php-mysql php-odbc php-pdo php-xml
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: mirrors.yun-idc.com
* extras: mirrors.yun-idc.com
* updates: mirrors.yun-idc.com
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.2.15-54.el6.centos will be installed
--> Processing Dependency: httpd-tools = 2.2.15-54.el6.centos for package: httpd-2.2.15-54.el6.centos.x86_64
--> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-54.el6.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.2.15-54.el6.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.2.15-54.el6.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.2.15-54.el6.centos.x86_64
---> Package httpd-devel.x86_64 0:2.2.15-54.el6.centos will be installed
--> Processing Dependency: apr-util-devel for package: httpd-devel-2.2.15-54.el6.centos.x86_64
--> Processing Dependency: apr-devel for package: httpd-devel-2.2.15-54.el6.centos.x86_64
---> Package mysql.x86_64 0:5.1.73-7.el6 will be installed
--> Processing Dependency: mysql-libs = 5.1.73-7.el6 for package: mysql-5.1.73-7.el6.x86_64
---> Package mysql-devel.x86_64 0:5.1.73-7.el6 will be installed
---> Package mysql-server.x86_64 0:5.1.73-7.el6 will be installed
--> Processing Dependency: perl-DBI for package: mysql-server-5.1.73-7.el6.x86_64
--> Processing Dependency: perl-DBD-MySQL for package: mysql-server-5.1.73-7.el6.x86_64
--> Processing Dependency: perl(DBI) for package: mysql-server-5.1.73-7.el6.x86_64
---> Package php.x86_64 0:5.3.3-47.el6 will be installed
--> Processing Dependency: php-common(x86-64) = 5.3.3-47.el6 for package: php-5.3.3-47.el6.x86_64
---> Package php-bcmath.x86_64 0:5.3.3-47.el6 will be installed
---> Package php-cli.x86_64 0:5.3.3-47.el6 will be installed
---> Package php-devel.x86_64 0:5.3.3-47.el6 will be installed
---> Package php-gd.x86_64 0:5.3.3-47.el6 will be installed
---> Package php-mbstring.x86_64 0:5.3.3-47.el6 will be installed
---> Package php-mysql.x86_64 0:5.3.3-47.el6 will be installed
---> Package php-odbc.x86_64 0:5.3.3-47.el6 will be installed
--> Processing Dependency: libodbc.so.2()(64bit) for package: php-odbc-5.3.3-47.el6.x86_64
---> Package php-pdo.x86_64 0:5.3.3-47.el6 will be installed
---> Package php-xml.x86_64 0:5.3.3-47.el6 will be installed
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.24)(64bit) for package: php-xml-5.3.3-47.el6.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.22)(64bit) for package: php-xml-5.3.3-47.el6.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: php-xml-5.3.3-47.el6.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.13)(64bit) for package: php-xml-5.3.3-47.el6.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: php-xml-5.3.3-47.el6.x86_64
--> Processing Dependency: libxslt.so.1()(64bit) for package: php-xml-5.3.3-47.el6.x86_64
--> Processing Dependency: libexslt.so.0()(64bit) for package: php-xml-5.3.3-47.el6.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.3.9-5.el6_2 will be installed
---> Package apr-devel.x86_64 0:1.3.9-5.el6_2 will be installed
---> Package apr-util.x86_64 0:1.3.9-3.el6_0.1 will be installed
---> Package apr-util-devel.x86_64 0:1.3.9-3.el6_0.1 will be installed
--> Processing Dependency: openldap-devel for package: apr-util-devel-1.3.9-3.el6_0.1.x86_64
--> Processing Dependency: expat-devel for package: apr-util-devel-1.3.9-3.el6_0.1.x86_64
---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be installed
---> Package httpd-tools.x86_64 0:2.2.15-54.el6.centos will be installed
---> Package libxslt.x86_64 0:1.1.26-2.el6_3.1 will be installed
---> Package mailcap.noarch 0:2.1.31-2.el6 will be installed
---> Package mysql-libs.x86_64 0:5.1.73-5.el6_6 will be updated
---> Package mysql-libs.x86_64 0:5.1.73-7.el6 will be an update
---> Package perl-DBD-MySQL.x86_64 0:4.013-3.el6 will be installed
---> Package perl-DBI.x86_64 0:1.609-4.el6 will be installed
---> Package php-common.x86_64 0:5.3.3-47.el6 will be installed
---> Package unixODBC.x86_64 0:2.2.14-14.el6 will be installed
--> Running transaction check
---> Package expat-devel.x86_64 0:2.0.1-11.el6_2 will be installed
---> Package openldap-devel.x86_64 0:2.4.40-12.el6 will be installed
--> Processing Dependency: openldap = 2.4.40-12.el6 for package: openldap-devel-2.4.40-12.el6.x86_64
--> Processing Dependency: cyrus-sasl-devel >= 2.1 for package: openldap-devel-2.4.40-12.el6.x86_64
--> Running transaction check
---> Package cyrus-sasl-devel.x86_64 0:2.1.23-15.el6_6.2 will be installed
---> Package openldap.x86_64 0:2.4.40-5.el6 will be updated
---> Package openldap.x86_64 0:2.4.40-12.el6 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

===============================================
Package Arch Version Repository Size
===============================================
Installing:
httpd x86_64 2.2.15-54.el6.centos updates 833 k
httpd-devel x86_64 2.2.15-54.el6.centos updates 156 k
mysql x86_64 5.1.73-7.el6 base 894 k
mysql-devel x86_64 5.1.73-7.el6 base 130 k
mysql-server x86_64 5.1.73-7.el6 base 8.6 M
php x86_64 5.3.3-47.el6 base 1.1 M
php-bcmath x86_64 5.3.3-47.el6 base 39 k
php-cli x86_64 5.3.3-47.el6 base 2.2 M
php-devel x86_64 5.3.3-47.el6 base 513 k
php-gd x86_64 5.3.3-47.el6 base 111 k
php-mbstring x86_64 5.3.3-47.el6 base 460 k
php-mysql x86_64 5.3.3-47.el6 base 86 k
php-odbc x86_64 5.3.3-47.el6 base 55 k
php-pdo x86_64 5.3.3-47.el6 base 80 k
php-xml x86_64 5.3.3-47.el6 base 108 k
Installing for dependencies:
apr x86_64 1.3.9-5.el6_2 base 123 k
apr-devel x86_64 1.3.9-5.el6_2 base 176 k
apr-util x86_64 1.3.9-3.el6_0.1 base 87 k
apr-util-devel x86_64 1.3.9-3.el6_0.1 base 69 k
apr-util-ldap x86_64 1.3.9-3.el6_0.1 base 15 k
cyrus-sasl-devel x86_64 2.1.23-15.el6_6.2 base 303 k
expat-devel x86_64 2.0.1-11.el6_2 base 120 k
httpd-tools x86_64 2.2.15-54.el6.centos updates 79 k
libxslt x86_64 1.1.26-2.el6_3.1 base 452 k
mailcap noarch 2.1.31-2.el6 base 27 k
openldap-devel x86_64 2.4.40-12.el6 base 1.1 M
perl-DBD-MySQL x86_64 4.013-3.el6 base 134 k
perl-DBI x86_64 1.609-4.el6 base 705 k
php-common x86_64 5.3.3-47.el6 base 530 k
unixODBC x86_64 2.2.14-14.el6 base 378 k
Updating for dependencies:
mysql-libs x86_64 5.1.73-7.el6 base 1.2 M
openldap x86_64 2.4.40-12.el6 base 284 k

Transaction Summary
==============================================
Install 30 Package(s)
Upgrade 2 Package(s)

Total download size: 21 M
Is this ok [y/N]:

看到这里,出现 Is this ok [y/N] 时,输入 y, 然后 回车,即可以看到进度,这里不再进行截图。安装完成后,会出来 Complete,表示都已经成功安装。

关闭 selinux

setenforce 0

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

关闭 iptables 防火墙

service iptables stop

若因为其它原因不能关闭的,可以手动添加规则,如下:

iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

启动 http 和 mysql 服务

[root@localhost ~]# service httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
[root@localhost ~]#
[root@localhost ~]# service mysqld start
Initializing MySQL database: Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

[ OK ]
Starting mysqld: [ OK ]
[root@localhost ~]#

看到这里,可能有童鞋会问了,启动 httpd 的时候怎么会报错呢,不过这个没有关系,这是因为在 httpd.conf 里面,定义的 ServerName 不能解析出来,这个不影响使用,服务还是可以启动的,看那个 OK 的提示,后续我们再配置。

而启动 mysql 时,报一堆信息,这个是因为这个 mysql 刚刚装好,第一次启动,会自己初始化系统的库,这个不用管,后面有提示 OK ,即可。当然,这个了仅限yum安装的,会这样,对于编译安装的 mysql,需要手动初始化系统库。另外,yum 安装的 mysql root 密码默认为空。

下面来访问试一下,在浏览器里输入 IP 。如下截图,就代表 apache 已经安装好,并且正常工作。

lamp-1

下面再来写一个 php 测试的页面,使用 vi ,具体的 VI 编辑器,可以参考我博客里的 VIM 编辑器的使用,默认的网站根目录位于 /var/www/html

vi /var/www/html/test.php
<?php
phpinfo();
?>

这个文件,是打印出来 php 的所有信息,写完之后,保存退出,再使用浏览器来访问这个 test.php 文件,如下截图

lamp-2

出现如图的页面,表示 apache 和 php 已经结合啦。

测试完成后,删除 test.php 信息,因为这是服务器的信息,被黑客什么的看到了可就不好啦。

完成之后,就可以上传你的程序到 /var/www/html 然后配置好数据库信息,就可以访问了,开始你的建站之旅吧。

至于 apache 的其它配置(比如其于IP的,基于端口号以及基于域名的虚拟主机 等等等),我都会在后续的文章里继续更新,欢迎关注。

猜您喜欢