在lamp中php和mysql怎么连接,CentOS LAMP环境搭建和测试php与mysql的连接

CentOS LAMP作为服务器,不安装不需要的组件,所以在选择组件的时候,不要选web服务器,因为我们后面要手动编译安装。CentOS LAMP系统约定RPM包和源码包存放位置:

RPM包和源码包存放位置 /usr/local/src

源码包编译安装位置(prefix) /usr/local/XXX

MySQL 数据库位置 /usr/local/mysql/var

网站根目录 /usr/local/apache/htdocs

CentOS LAMP环境搭建

搭建的工作包括APACHE、MYSQL、PHP。可以按照这个顺序来搭建环境。

1、获取软件包

Httpd:  http://www.apache.org/dist/httpd/httpd-2.2.11.tar.gz

mysql: http://mirror.provenscaling.com/mysql/enterprise/source/5.0/mysql-5.0.70.tar.gz

php: http://museum.php.net/php5/php-5.2.2.tar.gz

把它们全部放到 /usr/local/src 下面.>cd /usr/local/src    (定位到安装包目录)

2、安装 mysql

>tar -zxvf mysql-5.0.70.tar.gz

>cd mysql-5.0.70

>./configure --prefix=/usr/local/mysql

>make

>make install

>useradd mysql //添加 mysql 用户

>cd /usr/local/mysql

>bin/mysql_install_db --user=mysql

>chown -R mysql .  //设置权限,注意后面有一个点 "."

>chgrp -R mysql .

>chown -R mysql var

>cp share/mysql/my-medium.cnf /etc/my.cnf

>cp share/mysql/mysql.server /etc/rc.d/init.d/mysqld //开机启动

>chmod 755 /etc/rc.d/init.d/mysqld

>chkconfig --add mysqld

运行了上面第8步后:

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/local/mysql/bin/mysqladmin -u root password 'new-password'

/usr/local/mysql/bin/mysqladmin -u root -h centos5 password 'new-password'

Alternatively you can run:

/usr/local/mysql/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/local/mysql ; /usr/local/mysql/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

cd mysql-test ; perl mysql-test-run.pl

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

The latest information about MySQL is available on the web at

http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

运行以下命令即可启动 MySQL 服务器:>/etc/rc.d/init.d/mysqld start //启动 MySQL(mysql安装完毕)

3、安装Apache

>tar -zxvf httpd-2.2.11.tar.gz

>cd httpd-2.2.11

>./configure --prefix=/usr/local/apache --enable-module=so --enable-module=rewrite --enable-shared=max

>make

>make install

运行上面第3步时,出现下面的问题:

[root@centos5 httpd-2.3.8]# ./configure --prefix=/usr/local/apache --enable-module=so --enable-module=rewrite --enable-shared=max

checking for chosen layout... Apache

checking for working mkdir -p... yes

checking build system type... i686-pc-linux-gnu

checking host system type... i686-pc-linux-gnu

checking target system type... i686-pc-linux-gnu

Configuring Apache Portable Runtime library ...

checking for APR... no

configure: error: APR not found.  Please read the documentation.

到http://apr.apache.org/download.cgi下载 apr-1.4.2.tar.gz。

解压后进入解压目录,进行如下操作:

./configure --prefix=/desired/path/of/apr

make

make test

make install

启动apache服务 >/usr/local/apache/bin/apachectl -k start用浏览器打开 http://127.0.0.1/  如果可以访问则说明apache安装成功。

4、安装PHP

>tar -zxvf php-5.2.2.tar.gz

>cd php-5.2.2

>./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-apxs2=/usr/local/apache/bin/apxs

>make

>make install

>cp php.ini-dist /usr/local/php/lib/php.ini

>vi /usr/local/php/lib/php.ini

5、CentOS LAMP配置httpd

.conf >vi /usr/local/apache/conf/httpd.conf找到"AddType application/x-gzip .tgz"在它的下面添加AddType application/x-httpd-php .phpAddType application/x-httpd-php-source .phps找到"DirectoryIndex index.html在index.html 前添加 index.php

启动apache服务>/usr/local/apache/bin/apachectl -k start将apache设置成开机自启动:在/etc/rc.d/rc.local文件中加入一行并保存: /usr/local/apache /bin/apachectl start (apache+php配置完毕)

6、查看确认 L.A.M.P 环境信息:>vi /usr/local/apache/htdocs/phpinfo.php新增加下面一行,并保存。 <?php phpinfo(); ?>>chmod 755 /usr/local/apache/htdocs/phpinfo.php用浏览器打开 http://127.0.0.1/phpinfo.php检查 phpinfo中的各项信息是否正确。如果可以访问则环境搭建成功

测试php与mysql的连接

>vi /usr/local/apache/htdocs/testdb.php增加下面几行,并保存。

$link=mysql_connect('localhost','root','yourpassword');

if(!$link) echo "fail";

else echo "success";

mysql_close();

?>

>chmod 755 /usr/local/apache/htdocs/testdb.php

>service mysqld start

用浏览器打开 http://127.0.0.1/testdb.php如果输出success表明php与mysql连接成功CentOS LAMP环境搭建完毕

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值