您的位置:首页 > 运维架构 > Apache

Linux 下Apache+MySQL+PHP安装及配置过程

2009-09-26 23:42 991 查看
环境:本文中的Linux操作系统为CentOS 5.2,Linux 2.6+ 内核 ,Apache+MySQL+PHP 安装及基本配置过程记录下来以供参考:

一. Apache Web服务器的源代码安装,Tarball安装方式:

1. 源码包:httpd-2.2.6.tar.gz 从 www.apache.org
下载到此目录下:/usr/local/src 同时在 /usr/local 下创建一个httpd目录:mkdir httpd

2. 解压安装包:tar -zxvf httpd-2.2.6.tar.gz

3. 进入解压后的安装包:cd httpd-2.2.6

4. 配置:./configure --prefix=/usr/local/webserver/httpd --enable-cgi
( 意义支持CGI)

5. 编译:make

6. 安装:make install

7. 启动web服务:

   /usr/local/httpd/bin/apachectl start 或 service httpd start

   或者:开机后立即启动web服务

   vi /etc/rc.d/rc.local

   新加一行

   /usr/local/httpd/bin/apachectl start 或 chkconfig --level 345 httpd on

8. 配置Apache服务器

   Apache 配制文件及目录是:/usr/local/
webserver/
httpd/conf/httpd.conf

   Apache默认存放主页的位置应该是:/usr/local/
webserver/
httpd/apache/htdocs

   DocumentRoot "/usr/local/
webserver/
httpd/htdocs" 这个是存放网页的目录,我们应该把网页的目录指定到哪里,这样当访问网址时,就调用这个目录的文件。

   <Directory "/opt/apache/htdocs"  这句应该和DocumentRoot 的目录保持一致。

    系统自动添加了这一行,如果没有则自己加入这一行。

   LoadModule php5_module        modules/libphp5.so

   找到: DirectoryIndex index.html index.html.var

   改为: DirectoryIndex index.html index.html.var index.php

   找到: AddType application/x-tar .tgz

   添加: AddType application/x-httpd-php .php
(注意空格)   

   这两项配置就是告诉Apache Server,以后收到的Url用户请求,凡是以php作为后缀,就需要调用php5_module模块mod_php5.so/ php5apache2.dll)进行处理。
mod_php 是把PHP做为APACHE一个内置模块。让apache http服务器本身能够支持PHP语言,不需要每一个请求就启动PHP解释器来解释PHP


unix/linux 下,so后缀文件是一个DSO文件,DSO与windows系统下的dll是等价概念,都是把一堆函数封装在一个二进制文件中。调用它们的进程把它们装入内存后,会将其映射到自己的地址空间。DSO全称为Dynamic Shared Object,即动态共享对象。DLL全称为Dynamic Link Library 即动态链接库。



:对存放网页的目录执行命令:chmod 755 目录名 chmod -R 755 目录名

9. 重启apache服务器:/usr/local/httpd/bin/apachectl restart

二、编译安装MySQL (源码包安装)

1. /usr/sbin/groupadd mysql  
         // 建立mysql组

2. /usr/sbin/useradd -g mysql mysql 
  // 建立mysql用户并且加入到mysql组中

3.
tar zxvf mysql-5.1.38.tar.gz

4. cd mysql-5.1.38/

5.  ./configure --prefix=/usr/local/webserver/mysql/ --enable-thread-safe-client

6. make && make install

7. cp /usr/local/src/mysql/support-files/my-medium.cnf /etc/my.cnf

support-files目录下有4个模版文件,我们选择其中一个Mysql的配置文件,覆盖/etc/my.cnf(系统默认的配置,其中设置了性能参数和Mysql的一些路径参数);

8. cd /usr/local/mysql 
   //进入mysql目录

9. /usr/local/webserver/mysql/bin/mysql_install_db --basedir=/usr/local/webserver/mysql --datadir=

/usr/local/webserver/mysql

/data --user=mysql 

//初试化表并且规定用mysql用户来访问。初始化表以后就开始给mysql和root用户设定访问权限;

10. chown -R root .
      //设定root能访问/usr/local/mysql;

11. chown -R mysql data
  //设定mysql用户能访问/usr/local/mysql/data ,里面存的是mysql的数据库文件.这个目录是在/etc/my.cnf中有配置,mysql_install_db时产生;

12. chown -R mysql data/. 
//设定mysql用户能访问/usr/local/mysql/data/mysql下的所有文件;

13. chgrp -R mysql . 
  //(此处 .前面有空格哦)设定mysql组能够访问/usr/local/mysql;

14. /usr/local/mysql/bin/mysqld_safe --user=mysql &
  //运行mysql;

15. /usr/local/src/mysql/bin/mysqladmin -u root password 'yourpassword'

//MYSQL默认安装密码为空,为mysql设置密码,利用的是/usr/local/src/mysql/bin/下的mysqladmin文件;

16. 启动MySQL服务:

    cp /usr/local/src/mysql/support-files/mysql.server /etc/init.d/mysql

    vi /etc/init.d/mysql

    修改如下定义使其内容为:

    basedir=/usr/local/webserver/mysql

    datadir=/usr/local/webserver/mysql/data

    chmod +x mysql   //赋予shell脚本可执行权限:

    chkconfig --level 345 mysql on 

    service mysql restart

    Shutting down MySQL...                  [  OK  ]

    Starting MySQL                               [  OK  ] 

17. 连接:

   /usr/local/src/mysql/bin/mysql -u root -p

   Enter password:

   Welcome to the MySQL monitor.  Commands end with ; or /g.

   Your MySQL connection id is 5 to server version: 4.1.14-standard

   Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

   mysql>

三、PHP的源代码安装,Tarball安装方式:

1. 源码包:php-5.2.4.tar.gz,从www.php.net
下载到此目录下:/usr/local/src 同时在/usr/local下创建一个php目录:mkdir php

2. 解压安装包:tar -zxvf php-5.2.4.tar.gz

3. 进入解压后的安装包:cd php-5.2.4

4. 配置:./configure --prefix=/usr/local/

webserver/

php
  设置PHP5 的安装路径。

    --with-apxs2=/usr/local/

webserver/

httpd/bin/apxs
   告诉PHP查找Apache 2.0的地方

    --with-libxml-dir=/usr/local/src/libxml2 
          告诉PHP放置libxml2库的地方。(在安装PHP检查是否先安装了libxml套件)

    --with-pdo-mysql=/usr/local/

webserver/

mysql  
        变量激活PDO(PHP数据对象抽象层)

    --with-mysql=/usr/local/

webserver/

mysql  
               变量激活regularMySQL扩展功能。

    --with-mysqli=/usr/local/

webserver/

mysql/bin/mysql_config
  变量激活新增加的MySQL功能。

    --with-config-file-path=/usr/local/

webserver/

php
      是php的配置文件php.ini放置的目录

5. 编译:make

6. 安装:make install

7. 转存PHP基本配置文件并命名为php.ini:cp php.ini-dist /usr/local/
webserver/
php/php.ini

最后,重启Apache服务器,测试php是否一切正常工作 可以直接通过PHP程序内置的解析程序来运行一个.php文件。

[root@localhost /]# /usr/local/

webserver/

php/bin/php -f /var/www/html/phpinfo.php



:在我用的CentOS 5.2 的版本中 httpd 是系统默认安装的,编译安装php时通常要指定apxs的位置,这时编译php时会返回错误信息,会找不到路径。 这时只要将 httpd-devel 包装上便可以使用。

[root@localhost bin]# yum install httpd-devel

这时路径为: /usr/sbin/apxs

另其他的配置和目录位于:

# /etc/httpd/conf/httpd.conf

# /etc/httpd/conf.d/*.conf

# /usr/lib/httpd/modules/

# /var/www/html/

# /var/www/error/

# /var/www/icons/

# /var/www/cgi-bin/

# /var/log/httpd/

# /usr/sbin/apachectl

# /usr/sbin/httpd

# /usr/bin/htpasswd

 

当启动web服务器时 提示:


Starting httpd:
httpd: Syntax error on line 206 of /etc/httpd/conf/httpd.conf: Cannot
load /usr/lib/httpd/modules/libphp5.so into server:
/usr/lib/httpd/modules/libphp5.so: cannot restore segment prot after
reloc: Permission denied

原因:如果激活了SELinux ,新内核可能无法安装加载SELIinx

正确处理方法:

方法一 将/etc/selinux/config中设置的SELinux=disabled。之后reboot

编辑/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 - SELinux is fully disabled.

SELINUX=enforcing

把 SELINUX=enforcing 注释掉:#SELINUX=enforcing ,然后新加一行为:

SELINUX=disabled

保存,关闭。

编辑/etc/sysconfig/selinux,找到:
# 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 - SELinux is fully disabled.

SELINUX=enforcing

如果SELINUX已经是 SELINUX=disabled,那么就不用改了,否则就把SELINUX=enforcing 注释掉,新加一行:

SELINUX=disabled

保存,退出。

如果碰到其他类似提示:

cannot restore segment prot after reloc: Permission denied

哪应该是SELinux的问题,可以考虑把它关闭。

-------------------------------------------------------------------------------------

如果将 SELinux 关闭后还是不行.执行下

chcon -t texrel_shlib_t

如: chcon -t texrel_shlib_t /usr/lib/httpd/modules/libphp5.so

方法二如果不想禁用SELinux,可安装selinux-policy-targeted-1.25-4.noarch.rpm的高版本。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息