search
HomeBackend DevelopmentPHP Tutorial【Linux】LAMP架构之以模块方式让php和httpd搭档工作

LAMP就是:Linux,Apache,Mysql,PHP的首字母缩写。


实验环境

Linux :CentOS-3 2.6.32-431.el6.x86_64

Apache:httpd-2.4.16.tar.gz

Mysql :mysql-5.5.24.tar.gz

PHP   :php-5.6.11.tar.bz2


安装顺序为:Apahce --> Mysql --> PHP


实验步骤


方便实验先关闭iptables和selinux

[root@CentOS-3 ~]# service iptables stop

[root@CentOS-3 ~]# setenforce 0


安装以下编译工具和依赖包

[root@CentOS-3 ~]# yum -y install gcc \

> gcc-c++ \

> make \

> cmake \

> pcre-devel 


安装apr和apr-util

apr是为了搭建apache的运行环境


创建apr和apr-util安装目录

[root@CentOS-3 ~]# mkdir /usr/local/apr

[root@CentOS-3 ~]# mkdir /usr/local/apr-util


将源码包解压

[root@CentOS-3 ~]# tar xf /mnt/apr-1.5.2.tar.gz -C /usr/local/src/

[root@CentOS-3 ~]# tar xf /mnt/apr-util-1.5.4.tar.gz -C /usr/local/src/

切换到源码包所在路径

[root@CentOS-3 ~]# cd /usr/local/src/apr-1.5.2/

配置apr指定安装目录

[root@CentOS-3 apr-1.5.2]# ./configure --prefix=/usr/local/apr

以上配置完成如果没有报错则执行编译并安装

[root@CentOS-3 apr-1.5.2]# make && make install

切换到apr-util源码目录下

[root@CentOS-3 ~]# cd /usr/local/src/apr-util-1.5.4/

配置apr-util安装目录并指定apr所在路径

[root@CentOS-3 apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util/ --with-apr=/usr/local/apr

以上无报错即可编译并安装

[root@CentOS-3 apr-util-1.5.4]# make && make install


编译apache

创建apache根目录

[root@CentOS-3 ~]# mkdir /usr/local/apache

解压并切换至apache源码包目录下

[root@CentOS-3 ~]# tar xf /mnt/httpd-2.4.16.tar.gz -C /usr/local/src/

[root@CentOS-3 ~]# cd /usr/local/src/httpd-2.4.16/


定制httpd


[root@CentOS-3 httpd-2.4.16]# ./configure \

> --prefix=/usr/local/apache \     //指定安装目录

> --enable-so \          

//支持动态共享模块,否则php无法以模块方式和apache结合工作

> --enable-rewirte \            //支持URL重写

> --enable-cgi \                //支持CGI

> --enable-cgid \            //使用event或者worker的mpm模式要启用cgid

> --enable-modules=most \    

> --enable-mods-shared=most \         //启动共享模块 

> --enable-mpms-shared=all \        //支持所有mpm模块

> --with-apr=/usr/local/apr \        //支持所有mpm模块

> --with-apr-util=/usr/local/apr-util        //指定apr-util位置

以上无报错即可编译并安装

[root@CentOS-3 httpd-2.4.16]#make && make install


启动httpd

[root@CentOS-3 ~]# /usr/local/apache/bin/apachectl start


测试看httpd是否工作正常

服务器的IP地址为:192.168.10.250


安装Mysql数据库

安装数据需要先安装ncurses-devel

[root@CentOS-3 mysql-5.5.24]# yum -y install ncurses-devel

创建Mysql安装目录

[root@CentOS-3 ~]# mkdir /usr/local/mysql

创建mysql用户,"-s"指定登录shell

[root@CentOS-3 ~]# useradd -s /sbin/nologin mysql

解压Mysql并切换至源码包所在目录

[root@CentOS-3 ~]# tar xf /mnt/mysql-5.5.24.tar.gz -C /usr/local/src/

[root@CentOS-3 ~]# cd /usr/local/src/mysql-5.5.24/


使用cmake工具定义mysql

[root@CentOS-3 mysql-5.5.24]# cmake \

> -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

指定安装目录

> -DMYSQL_DATADIR=/home/mysql \

数据文件路径,如果不指定就是安装目录下的data

> -DSYSCONFDIR=/etc \

配置文件路径

> -DWITH_INNOBASE_STORAGE_ENGINE=1 \

安装INNOBASE存储引擎

> -DWITH_ARCHIVE_STORAGE_ENGINE=1 \

安装ARCHIVE存储引擎

> -DWITH_BLCAKHOLE_STORAGE_ENGINE=1 \

安装BLACKHOLE存储引擎

> -DWITH_READLINE=1 \

能够使用loadinfile来批量导入mysql数据

> -DWITH_SSL=system \

支持基于SSL的会话

-DWITH_SSL=bundled

> -DWITH_ZLIB=system \

压缩库

> -DWITH_LIBWRAP=0 \

> -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \

默认套接字文件路径

> -DEXTRA_CHARSETS=all \

支持所有字符集

> -DDEFAULT_CHARSET=utf8 \

默认的字符集

> -DDEFAULT_COLLATION=utf8_general_ci

字符集排序规则


以上如果没有报错则执行编译并安装数据库

[root@CentOS-3 mysql-5.5.24]# make && make install


为了系统安全将mysql目录下属组改为mysql

[root@CentOS-3 ~]# chown :mysql /usr/local/mysql/


初始化数据库

[root@CentOS-3 mysql-5.5.24]# /usr/local/mysql/scripts/mysql_install_db \

> --user=mysql \

> --ldata=/var/lib/mysql \

> --basedir=/usr/local/mysql \

> --datadir=/home/mysql


创建配置文件

[root@CentOS-3 mysql-5.5.24]# cp support-files/my-large.cnf /etc/my.cnf


创建启动脚本,加入到service管理

[root@CentOS-3 mysql-5.5.24]# cp support-files/mysql.server /etc/init.d/mysqld


配置开机启动

[root@CentOS-3 mysql-5.5.24]# chkconfig --add mysqld

[root@CentOS-3 mysql-5.5.24]# chkconfig mysqld on

添加执行权限

[root@CentOS-3 mysql-5.5.24]# chmod 755 /etc/init.d/mysqld


将mysql添加到$PATH环境变量中

[root@CentOS-3 mysql-5.5.24]# vim /etc/profile.d/mysql.sh

输入以下内容

export PATH=$PATH:/usr/local/mysql/bin

运行脚本使其立即生效

[root@CentOS-3 mysql-5.5.24]# . /etc/profile.d/mysql.sh


启动mysql

[root@CentOS-3 mysql-5.5.24]# service mysqld start


为了后面测试方便授权一个账号

进入mysql数据库

[root@CentOS-3 ~]# mysql -u root

授权tom用户所有权限

mysql> GRANT all ON *.* TO 'tom'@'192.168.10.250' IDENTIFIED BY '123456';

刷新授权信息

mysql> flush privileges;




安装PHP

默认apache只能支持静态的网站要想支持动态网站需要与PHP搭配工作

php与httpd工作有三种方式分别为:CGI、Modules、FastCGI


下面以Modules方式安装PHP


创建安装目录

[root@CentOS-3 ~]# mkdir /usr/local/php

解压PHP并切换至PHP源码包所在目录

[root@CentOS-3 ~]# tar xf /mnt/php-5.6.11.tar.bz2 -C /usr/local/src

[root@CentOS-3 ~]# cd /usr/local/src/php-5.6.11/


安装GD库以及关联程序

[root@CentOS-3 php-5.6.11]# yum -y install \

> libjpeg-devel \

> libpng-devel \

> freetype-devel \

> zlib-devel \

> gettext-devel \

> libXpm-devel \

> libxml2-devel \

> fontconfig-devel \

> openssl-devel \

> bzip2-devel



配置PHP

[root@CentOS-3 php-5.6.11]# ./configure \

> --prefix=/usr/local/php \

指定安装路径

> --with-mysql=/usr/local/mysql \

指定mysql位置

> --with-openssl \

开启openssl功能

> --with-mysqli=/usr/local/mysql/bin/mysql_config \

另一种mysql访问接口

> --enable-mbstring \

> --with-freetype-dir \

加载freetype字体库

> --with-jpeg-dir \

支持图片

> --with-png-dir \

> --with-zlib \

让数据文件先压缩在传送

> --with-libxml-dir=/usr \

指定xml库目录

> --enable-xml \

开启xml支持

> --enable-sockets \

让PHP支持套接字

> --with-apxs2=/usr/local/apache/bin/apxs \

将mysql编译成apache模块

> --with-config-file-path=/etc \

PHP配置文件位置

> --with-config-file-scan-dir=/etc/php.d \

> --with-bz2 \

> --enable-maintainer-zts

如果apache的mpm使用的是event或worker时要加,不是不用


以上如果不报错即可编译并安装

[root@CentOS-3 php-5.6.11]# make && make install


创建配置文件

[root@CentOS-3 php-5.6.11]# cp php.ini-production /etc/php.ini



编辑httpd配置文件,搜索“/AddType”添加如下内容让apache支持php格式页面

[root@CentOS-3 ~]# vim /usr/local/apache/conf/httpd.conf

AddType application/x-httpd-php .php

AddType application/x-httpd-php-source .phps



继续搜索“/DirectoryIndex”在此处添加一下内容以支持php主页面

LoadModule    php5_module    modules/libphp5.so

DirectoryIndex index.php index.html


将httpd的默认主页修改为“.php”,并输入以下代码进行测试

切换至网页文件目录

[root@CentOS-3 ~]# cd /usr/local/apache/htdocs/

改为index.php

[root@CentOS-3 htdocs]# mv index.html index.php

编辑文件删除原有内容,添加如下内容

phpinfo();

?>


重启httpd服务

[root@CentOS-3 htdocs]# /usr/local/apache/bin/apachectl restart


访问服务器测试

出现以上界面说明php与httpd搭档成功


关联数据库测试


编辑网页文件删除原有内容输入以下代码

[root@CentOS-3 htdocs]# vim index.php

    $conn=mysql_connect('192.168.10.250','tom','123456');

    if($conn)

        echo "数据库连接成功!!";

    else

        echo "数据库连接失败!!";

?>


访问服务器测试

以上就是lamp架构的内容了

以模块方式使php和httpd搭档工作,把PHP编译成httpd的模块,不能独立执行。当httpd需要用到php进程的时候直接从磁盘加载进来,在它内部运行即可不需要启动一个新的进程。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

Debunking the Myths: Is PHP Really a Dead Language?Debunking the Myths: Is PHP Really a Dead Language?Apr 16, 2025 am 12:15 AM

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

The PHP vs. Python Debate: Which is Better?The PHP vs. Python Debate: Which is Better?Apr 16, 2025 am 12:03 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.