导航

LAMP实验一:安装Apache+MySQL+PHP基本环境

Posted on 2012-07-18 10:43  eastson  阅读(1113)  评论(0编辑  收藏  举报

安装Httpd

1. 安装httpd。先检查一下httpd是否已经有安装:

[root@centos-server ~]$ rpm -qa | grep httpd

如果httpd没有安装,可以使用下面l命令进行安装:

[root@centos-server ~]$ yum install httpd
[root@centos-server ~]$ chkconfig httpd on
[root@centos-server ~]$ service httpd start

 

2. 测试httpd。httpd的文档根目录位于/var/www/html。我们在该目录下创建一个index.html文件,测试是否可以正常访问。

[root@centos-server /var/www/html]$ echo "<h1>It Works</h1>" > index.html
[root@centos-server /var/www/html]$ curl http://localhost/index.html
<h1>It Works</h1>

 

3. 配置防火墙。CentOS默认的防火墙策略如下:

[root@centos-server /var/www/html]$ iptables -L -n --line-numbers
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  

该策略阻止了其它机器除了22(SSH)端口之外的所有端口对本机的访问。httpd服务需要开启80端口,按照下面的方式开启80端口:

[root@centos-server ~]$ iptables -I INPUT 5 -p tcp -m tcp --dport 80 -m state --state NEW -j ACCEPT
[root@centos
-server ~]$ service iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[root@centos
-server ~]$ service iptables restart iptables: Flushing firewall rules: [ OK ] iptables: Setting chains to policy ACCEPT: filter [ OK ] iptables: Unloading modules: [ OK ] iptables: Applying firewall rules: [ OK ]

OK,现在用其它机器访问一下http://192.168.12.215/index.html(服务器IP地址为192.168.12.215)试试看。

 

安装MySQL

1. 安装MySQL。先检查一下mysql-server是否已经有安装:

[root@centos-server ~]$ rpm -qa | grep mysql-server

如果mysql-server没有安装,可以使用下面l命令进行安装:

[root@centos-server ~]$ yum install mysql-server
[root@centos-server ~]$ chkconfig mysqld on
[root@centos-server ~]$ service mysqld start

 

2. 配置MySQL。MySQL提供了一个名为mysql_secure_installation的工具,协助我们在安装完毕后做一些必要的安全设置。

[root@centos-server ~]$ mysql_secure_installation 
... ...
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):  ###输入当前的root密码, 默认为空
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]   ###是否需要重设root密码?
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]   ###是否移除匿名用户?
 ... 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]    ###是否禁用root远程连接?
 ... 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] n   ###是否删除test数据库?
n
... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]   ###重新载入授权信息?
 ... 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!

 

3. 检查MySQL是否正常运行。

[root@centos-server ~]$ mysqladmin -h localhost -u root -p version
Enter password: 
mysqladmin  Ver 8.42 Distrib 5.1.61, for redhat-linux-gnu on x86_64
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Server version        5.1.61
Protocol version    10
Connection        Localhost via UNIX socket
UNIX socket        /var/lib/mysql/mysql.sock
Uptime:            16 min 14 sec

Threads: 1  Questions: 21  Slow queries: 0  Opens: 15  Flush tables: 1  Open tables: 8  Queries per second avg: 0.21

 

安装PHP

1. 安装php。先检查一下php是否已经有安装:

[root@centos-server ~]$ rpm -qa | grep php

如果httpd没有安装,可以使用下面l命令进行安装:

[root@centos-server ~]$ yum install php

 

2. 测试php。创建文件hello-world.php,文件内容如下:

<?php

print "Hello World!\n";

?>

执行hello-world.php,检查PHP是否有正常运行:

[root@centos-server ~]$ php -f hello-world.php 
Hello World!

 

3. 测试与httpd的集成。在/var/www/html下创建文件php-info.php,文件内容如下:

<?php

phpinfo();

?>

使用浏览器请求PHP文件之前必须重启httpd:

[root@centos-server /var/www/html]$ service httpd restart

然后打开网址http://192.168.12.215/php-info.php试试看:

 

4. 安装php-mysql模块。默认php没有安装该模块。

[root@centos-server ~]$ yum install php-mysql
[root@centos-server ~]$ service httpd restart ###需要重启httpd

再次打开网址http://192.168.12.215/php-info.php,发现找到mysql模块:

其它的php模块也可以比照此种方式安装。使用下面的命令可以查询在CentOS软件源中可用的php模块:

[root@centos-server ~]$ yum search php
cups-php.x86_64 : Common Unix Printing System - php module
graphviz-php.x86_64 : PHP extension for graphviz
php.x86_64 : PHP scripting language for creating dynamic web sites
php-bcmath.x86_64 : A module for PHP applications for using the bcmath library
php-cli.x86_64 : Command-line interface for PHP
php-common.x86_64 : Common files for PHP
php-dba.x86_64 : A database abstraction layer module for PHP applications
php-devel.x86_64 : Files needed for building PHP extensions
php-embedded.x86_64 : PHP library for embedding in applications
php-gd.x86_64 : A module for PHP applications for using the gd graphics library
php-imap.x86_64 : A module for PHP applications that use IMAP
php-intl.x86_64 : Internationalization extension for PHP applications
php-ldap.x86_64 : A module for PHP applications that use LDAP
php-mbstring.x86_64 : A module for PHP applications which need multi-byte string handling
php-mysql.x86_64 : A module for PHP applications that use MySQL databases
php-odbc.x86_64 : A module for PHP applications that use ODBC databases
php-pdo.x86_64 : A database access abstraction module for PHP applications
php-pear.noarch : PHP Extension and Application Repository framework
php-pecl-apc.x86_64 : APC caches and optimizes PHP intermediate code
php-pgsql.x86_64 : A PostgreSQL database module for PHP
php-process.x86_64 : Modules for PHP script using system process interfaces
php-pspell.x86_64 : A module for PHP applications for using pspell interfaces
php-recode.x86_64 : A module for PHP applications for using the recode library
php-snmp.x86_64 : A module for PHP applications that query SNMP-managed devices
php-soap.x86_64 : A module for PHP applications that use the SOAP protocol
php-tidy.x86_64 : Standard PHP module provides tidy library support
php-xml.x86_64 : A module for PHP applications which use XML
php-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol
php-zts.x86_64 : Thread-safe PHP interpreter for use with the Apache HTTP Server
rrdtool-php.x86_64 : PHP RRDtool bindings
uuid-php.x86_64 : PHP support for Universally Unique Identifier library
php-enchant.x86_64 : Human Language and Character Encoding Support
php-pecl-apc-devel.x86_64 : APC developer files (header)
php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon