VM搭建LAMP和局域网内访问之 转

 

 (2012-11-06 14:29:17)
标签: 

 

it

分类: WEB
1.apache:
[root@localhost liuhan]# yum install httpd
...

启动
[root@localhost liuhan]# /etc/init.d/httpd start
Starting httpd:

查看运行状态
[root@localhost liuhan]# /etc/init.d/httpd status
httpd (pid  2633) is running...

http://localhost 或 http://127.0.0.1 ,看到 Apache 2 Test Page 页面.

修改配置文件:修改侦听端口 80 -> 8080
[root@localhost liuhan]# nano /etc/httpd/conf/httpd.conf
...
#Listen 12.34.56.78:80
Listen 8080

重启动服务
/etc/init.d/httpd restart

apache2 这个版本的结构:
/etc/httpd/conf/httpd.conf :最主要的配置文件;
/etc/httpd/conf.d/*.conf :这个是 CentOS 的特色,如果你不想修改原始配置文件 httpd.conf 的话,其他配置的在此独立配置,启动 apache 时,这个文件就会被读入到主要配置文件;
/usr/lib/httpd/modules :apache 支持很多的模块,您想要使用的模块默认都放置在此目录;
/var/www/html :这里是 CentOS 默认的“首页”目录;
/var/www/error :默认的系统错误信息,主机设置错误或浏览器端要求的数据错误,在浏览器上出现的错误提示就以这里的信息为主;
/var/www/icons :提供 apache 的一些小图标;
/var/www/cgi-bin :默认一些可执行的 CGI 程序放置的目录;
/var/log/httpd :日志文件目录,这里的文件很容易变的很大,需要提供足够的空间;
/usr/sbin/apachectl :这是 Apache 的主要执行文件,这个执行文件其实是 shell script ,它可以主动检测系统上的一些设置值,好让您启动 Apache 时更简单;
/usr/sbin/httpd :这是主要的 apache 的二进制文件;
/usr/bin/htpasswd :当您想登陆某些网页时,需要输入账号与密码。那么Apache本身就提供一个最基本的密码保护方式,该密码的产生就是通过这个命令实现的。

2.mysql:
[root@localhost liuhan]# yum install mysql mysql-server
...

启动 MySQL
[root@localhost liuhan]# /etc/init.d/mysqld start
Starting mysqld:                                           [  OK  ]

为 root 用户设置一个密码
[root@localhost liuhan]# mysqladmin -u -root -p password liuhan@big
Enter password:
mysqladmin: Can't turn off logging; error: 'Access denied; you need the SUPER privilege for this operation'

[root@localhost liuhan]# /etc/init.d/mysqld stop
Stopping mysqld:                                           [  OK  ]

启动
 mysql> update user set password=password('liuhan@9') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> quit
Bye
[root@localhost liuhan]# mysql -u root -p
Enter password:
...
mysql>

MySQL 有几个重要目录与文件:
/etc/my.cnf :这是Mysql的配置文件,包括 mysql 数据库的优化;
/usr/lib/mysql :这个目录是 MySQL 数据库放置的位置,务必在备份时将此目录完整的备份下来。

3,php:
[root@localhost liuhan]# yum install php

然后需要必须重新启动 Apache :
[root@localhost liuhan]# /etc/init.d/httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]


Apache 网站的默认文档的路径是 /var/www/html ,在这个目录里创建一个简单文件 info.php ,并且在浏览器中调用 http://localhost/info.php 将会显示很多 PHP5 的安装信息.
[root@localhost liuhan]# nano /var/www/html/info.php
[root@localhost liuhan]# cat /var/www/html/info.php
< ? php

// Show all information, defaults to INFO_ALL
phpinfo();

// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);

? >

4,PHP5 支持 MySQL:

[root@localhost liuhan]# yum search ph
...
php.i686 : PHP scripting language for creating dynamic web sites
php-bcmath.i686 : A module for PHP applications for using the bcmath library
php-cli.i686 : Command-line interface for PHP
php-common.i686 : Common files for PHP
php-dba.i686 : A database abstraction layer module for PHP applications
php-devel.i686 : Files needed for building PHP extensions
php-embedded.i686 : PHP library for embedding in applications
php-gd.i686 : A module for PHP applications for using the gd graphics library
php-imap.i686 : A module for PHP applications that use IMAP
php-intl.i686 : Internationalization extension for PHP applications
php-ldap.i686 : A module for PHP applications that use LDAP
php-mbstring.i686 : A module for PHP applications which need multi-byte string
                  : handling
php-mysql.i686 : A module for PHP applications that use MySQL databases
php-odbc.i686 : A module for PHP applications that use ODBC databases
php-pdo.i686 : A database access abstraction module for PHP applications
php-pear.noarch : PHP Extension and Application Repository framework
php-pecl-apc.i686 : APC caches and optimizes PHP intermediate code
php-pgsql.i686 : A PostgreSQL database module for PHP
php-process.i686 : Modules for PHP script using system process interfaces
php-pspell.i686 : A module for PHP applications for using pspell interfaces
php-recode.i686 : A module for PHP applications for using the recode library
php-snmp.i686 : A module for PHP applications that query SNMP-managed devices
php-soap.i686 : A module for PHP applications that use the SOAP protocol
php-tidy.i686 : Standard PHP module provides tidy library support
php-xml.i686 : A module for PHP applications which use XML
php-xmlrpc.i686 : A module for PHP applications which use the XML-RPC protocol
php-zts.i686 : Thread-safe PHP interpreter for use with the Apache HTTP Server
...

[root@localhost liuhan]# yum install php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc
...


[root@localhost liuhan]# yum install php-mysql
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: mirror.bit.edu.cn
 * extras: mirror.bit.edu.cn
 * updates: mirror.bit.edu.cn
Setting up Install Process
Package php-mysql-5.3.3-14.el6_3.i686 already installed and latest version
Nothing to do

重启pache

在浏览器中重新加载 http://localhost/info.php 这个页面,就能看到多了刚刚安装的 MySQL 模块。

5. 设置 Apache 和 MySQL 开机启动
[root@localhost liuhan]# chkconfig --levels 3 httpd on
[root@localhost liuhan]# chkconfig --list httpd
httpd              0:off    1:off    2:off    3:on    4:off    5:off    6:off
[root@localhost liuhan]# chkconfig --levels 3 mysqld on
[root@localhost liuhan]# chkconfig list mysqld
chkconfig version 1.3.49.3 - Copyright (C) 1997-2000 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License.

usage:   chkconfig [--list] [--type <type>] [name]
         chkconfig --add <name>
         chkconfig --del <name>
         chkconfig --override <name>
         chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>

6:访问LAMP
 
把httpd.conf里监听端口设置回80
#Listen 12.34.56.78:80
Listen 80
 
[root@localhost liuhan]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@localhost liuhan]# /etc/rc.d/init.d/iptables saved
Usage: iptables {start|stop|restart|condrestart|status|panic|save}
[root@localhost liuhan]# /etc/rc.d/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@localhost liuhan]# /etc/init.d/iptables restart
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]
[root@localhost liuhan]#
在我的win7下输入http://192.168.56.129/info.php就可以访问虚拟机里的lamp了。

局域网内其它PC访问LAMP:
把NAT改为桥接模式,自动获取或者手动配置就行。
 
Use bridged networking(桥接模式)
    在这种模式下,VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机,它可以访问网内任何一台机器。在桥接模式下,你需要手工为虚拟系统配置IP地址、子网掩码,而且还要和宿主机器处于同一网段,这样虚拟系统才能和宿主机器进行通信。同时,配置好网关和DNS的地址后,以实现通过局域网的网关或路由器访问互联网。

Use network address translation(NAT模式)
使用NAT模式,就是让虚拟系统借助NAT(网络地址转换)功能,通过宿主机器所在的网络来访问公网。也就是说,使用NAT模式可以实现在虚拟系统里访问互联网。NAT模式下的虚拟系统的TCP/IP配置信息是由VMnet8(NAT)虚拟网络的DHCP服务器提供的,无法进行手工修改,因此虚拟系统也就无法和本局域网中的其他真实主机进行通讯。采用NAT模式最大的优势是虚拟系统接入互联网非常简单,只需要宿主机器能访问互联网,你不需要配置IP地址,子网掩码,网关,但是DNS地址还是要根据实际情况填的。添加DNS地址除了在网卡属性中填写,还可以在虚拟机中的“虚拟网络编辑器”中的NAT选项卡中点击“编辑”按钮中来添加。

如果仅仅是让虚拟机能上网,两种模式都可以的,用桥接的话只要你在局域网内有合法的地址,比如你的ADSL猫是带路由功能的,如果是在单位,那就要网管给你合法IP才行(现在公司都是mac和ip绑定的)。
现在是主机和虚拟机互通,如果你的adsl带路由功能,那关闭虚拟机的dhcp,选桥接,检查2机是否分配同网段的IP,关闭防火墙。

 

posted on 2015-06-16 12:46  areu-me  阅读(206)  评论(0编辑  收藏  举报

导航