Linux自学笔记——手动编译安装LAMP

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

本文主要演示编译安装LAMP:

第一部分:httpd 2.4.9 + mariadb-5.5.46 + php-5.4.26编译安装过程:

一、   编译安装apache

1.      解决依赖关系

httpd-2.4.9需要教新版本的apr和apr-util,因此需要事先对其进行升级。升级方式有两种,一种是通过源代码编译安装,一种是直接升级rpm包。这里选择使用编译源代码的方式运行。

首先下载这三个包httpd-2.4.9,apr-1.5.0.tar.bz2,apr-util-1.5.3.tar.bz2

image.png

准备开发环境,安装Development Tools ,Server Platform Development;使用命令yum groupinstall,这里不多阐述;

            1)      编译安装apr-1.5.0.tar.bz2

a.       解压缩apr-1.5.0.tar.bz2:

image.png

b.      进入apr-1.5.0目录;

image.png

c.       依次执行以下命令;

# ./configure --prefix=/usr/local/apr            设置安装目录;

# make && make install                           编译及安装;

            2)      编译安装apr-util

a.       解压apr-util-1.5.3.tar.bz2,并进入解压后的apr-util-1.5.3目录;

image.png

b.      依次执行以下命令;

# ./configure --prefix=/usr/local/apr-util  --with-apr=/usr/local/apr  

# make && make install

       附:apache官方对APR的介绍:

The mission of the Apache Portable Runtime (APR) project is to create and maintain software libraries that provide a predictable and consistent interface to underlying platform-specific implementations. The primary goal is to provide an API to which software developers may code and be assured of predictable if not identical behaviour regardless of the platform on which their software is built, relieving them of the need to code special-case conditions to work around or take advantage of platform-specific deficiencies or features.

        3)      Httpd-2.4.9编译过程也要依赖于pcre-devel软件包,需要事先安装。此软件为系统光盘自带,找到并安装即可;

            image.png

2.      编译安装httpd-2.4.9

首先下载httpd2.4.9到本地,然后执行如下的安装过程;

        1)      解压缩httpd-2.4.9.tar.bz2包并进入解压后的目录;

            image.png


        2)      依次执行以下命令;

            设置安装路径等选项:# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-                    rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --            with-mpm=event

            image.png

编译安装:#make && make install

补充:

a.       构建MPM为静态模块

        在全部平台中,MPM都可以构建为静态模块。在构建时选择一种MPM,链接到服务器中。如果要改变MPM,必须重新构建。为了使用指定的MPM,请在执行configure脚本时,使用参数--with-mpm=NAME。NAME时指定的MPM名称。编译完成后,,可以使用./httpd –l 来选择MPM。此命令会列出编译到服务器程序中的所有模块,包括MPM。

    b.      构建MPM为动态模块

        UNIX或类似平台中,MPM可以构建为动态模块,与其它动态模块一样在运行时加载。构建MPM为动态模块允许通过修改LoadModule指令来改变MPM,而不用重新构建服务器程序。在执行configure脚本时,使用--enable-mpms-shared选项即可启用此特性。当给出的参数为all时,所有此平台支持的MPM模块都会被安装。还可以在参数中给出模块列表。默认MPM,可以自动选择或者在执行configure脚本时通过--with-mpm选项来之指定,然后出现在生成的服务器配置文件中。编辑Loadmodule指令内容可以选择不同的MPM。

        3)      修改httpd的主配置文件,设置其pid路径;

            编辑/etc/httpd24/httpd.conf,添加如下行即可;

            PidFile “/var/run/httpd/httpd.pid”

            image.png

       4)      提供脚本配置文件/etc/rc.d/init.d/httpd24;

            修改相应为如下内容;

            image.png

            配置文件所有内容如下:

            #!/bin/bash

            #

            # httpd        Startup script for the Apache HTTP Server

            #

            # chkconfig: - 85 15

            # description: Apache is a World Wide Web server.  It is used to serve \

            #        HTML files and CGI.

            # processname: httpd

            # config: /etc/httpd/conf/httpd.conf

            # config: /etc/sysconfig/httpd

            # pidfile: /var/run/httpd.pid

             

            # Source function library.

            . /etc/rc.d/init.d/functions

             

            if [ -f /etc/sysconfig/httpd ]; then

                    . /etc/sysconfig/httpd

            fi

             

            # Start httpd in the C locale by default.

            HTTPD_LANG=${HTTPD_LANG-"C"}

             

            # This will prevent initlog from swallowing up a pass-phrase prompt if

            # mod_ssl needs a pass-phrase from the user.

            INITLOG_ARGS=""

             

            # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server

            # with the thread-based "worker" MPM; BE WARNED that some modules may not

            # work correctly with a thread-based MPM; notably PHP will refuse to start.

             

            # Path to the apachectl script, server binary, and short-form for messages.

            apachectl=/usr/local/apache/bin/apachectl

            httpd=${HTTPD-/usr/local/apache/bin/httpd}

            prog=httpd

            pidfile=${PIDFILE-/var/run/httpd.pid}

            lockfile=${LOCKFILE-/var/lock/subsys/httpd}

            RETVAL=0

             

            start() {

                    echo -n $"Starting $prog: "

                    LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS

                    RETVAL=$?

                    echo

                    [ $RETVAL = 0 ] && touch ${lockfile}

                    return $RETVAL

            }

             

            stop() {

              echo -n $"Stopping $prog: "

              killproc -p ${pidfile} -d 10 $httpd

              RETVAL=$?

              echo

              [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}

            }

            reload() {

                echo -n $"Reloading $prog: "

                if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then

                    RETVAL=$?

                    echo $"not reloading due to configuration syntax error"

                    failure $"not reloading $httpd due to configuration syntax error"

                else

                    killproc -p ${pidfile} $httpd -HUP

                    RETVAL=$?

                fi

                echo

            }

             

            # See how we were called.

            case "$1" in

              start)

              start

              ;;

              stop)

              stop

              ;;

              status)

                    status -p ${pidfile} $httpd

              RETVAL=$?

              ;;

              restart)

              stop

              start

              ;;

              condrestart)

              if [ -f ${pidfile} ] ; then

                stop

                start

              fi

              ;;

              reload)

                    reload

              ;;

              graceful|help|configtest|fullstatus)

              $apachectl $@

              RETVAL=$?

              ;;

              *)

              echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"

              exit 1

            esac

             

            exit $RETVAL

       5)      为此脚本赋予权限;

            image.png

        6)      加入服务列表;

            image.png

       7)      测试;

            image.png

 

二、   安装mariadb-5.5.46

1.      准备数据存放的文件系统;

新建一个逻辑卷,并将其挂在至特定目录即可。这里建逻辑卷的过程不再阐述;

这里假设其逻辑卷挂载目录为/mydata,而后需要创建/mydata/data目录作为mysql数据的存放目录。

image.png

2.      新建用户以安全方式运行进程:

image.png

3.      安装并初始化mariadb-5.5.46

        1)      首先下载二进制mariadb安装包至本地,

            image.png

        2)      解压缩文件至/usr/local目录;

            image.png

        3)      创建软连接;

            image.png

        4)      进入mysql目录,修改目录内文件属主属组;

            image.png

        5)      运行脚本,生成元数据库;指明用户和数据库存放位置;

            image.png

4.      为mysql提供主配置文件;

        1)      复制文件;

            image.png


        2)      编辑配置文件/etc/my.cnf;

            image.png


5.      为mysql提供sysv服务脚本;

        1)      复制脚本文件;

            image.png

        2)      添加脚本文件执行权限;

            image.png

        3)      添加服务列表;

            image.png

        4)      测试使用mysql;

              image.png


    为了使用mysql的安装符合系统使用规范,并将其开发组件导出给系统使用,这里还需要执行如下步骤;

6.      输出mysql的man手册至man命令的查找路径;

编辑/etc/man.config,添加如下行;

image.png


7.      输出mysql头文件至系统头文件路径下/usr/include;可以通过简单的创建链接实现;

image.png


8.      输出mysql的库文件给系统查找路径;

image.png


9.      修改PATH环境变量,让系统可以直接使用mysql相关命令。

image.png


至此,mariadb全部安装完成;

 

三、   编译安装php-5.4.26

1.      解决依赖关系:

首先配置号yum源(系统安装及epel源)后执行如下命令

# yum -y groupinstall "Desktop Platform Development"

# yum -y install bzip2-devel libmcrypt-devel libxml2-devel

2.      编译安装php-5.4.26

        1)      下载源码至本地目录;

            image.png

        2)      解压缩文件并进入php-5.4.26目录;

          image.png

        3)      编译安装php,执行如下指令;

        # ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2  --enable-maintainer-zts

        image.png  

 

    各参数解释:

       --prefix=/usr/local/php 默认安装路径

--with-mysql=/usr/local/mysql 指明mysql安装路径,如果有特定路径就使用等号"="后面跟上路径,没有则省略等号"="

--with-openssl 使用OpenSSL

--with-mysqli=/usr/local/mysql/bin/mysql_config 定义mysqli接口

--enable-mbstring 支持多字节字符串支持

--with-freetype-dir 支持各种字体

--with-jpeg-dir 支持处理jpeg格式图片

--with-png-dir 支持处理png格式图片

--with-zlib 支持压缩库

--with-libxml-dir=/usr 支持处理xml文档

--enable-xml 支持xml

--enable-sockets 使php支持以sockets方式通信

--with-apxs2=/usr/local/apache/bin/apxs (关键)表示把php编译成Apache的模块

--with-mcrypt 支持加密解密库

--with-config-file-path=/etc 定义php配置文件(php.ini)放置路径

--with-config-file-scan-dir=/etc/php.d 其他配置文件查找路径

--with-bz2 支持bz2格式加密

--enable-maintainer-zts 仅针对mpm为event和worker的情况,编译成zts模块,如果是prefork则不需要

 

    说明:

        a.       这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。

        b.      --with-config-file-path=/etc:指定配置文件php.ini地址;

        c.       –with-config-file-scan-dir=/etc/php.d:指定额外的ini文件目录;

        d.      如果使用php5.3以上版本,为了链接MYSQL数据库,可以指定mysqld,这样在本机就不需要先安装mysql或mysql开发包了。Mysqlnd从php5.3开始可用,可以编译时绑定到它(而不用具体的mysql客户端库绑定形成依赖),但从php5.4开始它就是默认设置了。

        #./configure --with-msyql=mysqlnd --with-pdo-mysql=mysqlnd --with-msyqli=msyqlnd

         4)      编译;

            #make && make install

         5)      为php提供配置文件;

            #cp php.ini-production /etc/php.ini

            image.png

            6)      编辑apache配置文件httpd.conf,以apache支持php

                #vim /etc/httpd24/httpd.conf

a.       添加以下两行;

    AddType application/x-httpd-php .php

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

   image.png 

b.      定位至DirectoryIndex index.html

    修改为

        DirectoryIndex       index.php  index.html

    image.png

c.       编辑index.php

    image.png

            7)      测试;

               image.png 

 

四、     安装xcache,为php加速:

1.      压力测试,首先我们用ab命令访问在这台机器上部署的wordpress,进行压力测试,然后与安装xcache之后的压力测试做对比;

    image.png

2.      按如下步骤安装;

        1)      解压源码包等操作;

    image.png

        2)      编译安装;

        # ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config

        # make && make install

               安装结束时,会出现类似如下行:

               Build complete.

        Don't forget to run 'make test'.

        Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20100525/

3.      编辑php.ini,整合php和xcache:

        1)      首先将xcache提供的样例配置文件导入php.ini

            image.png


          说明:xcache.ini文件在xcache目录中;

        2)      接下来编辑/etc/php.d/xcache.ini,找到extension开头的行,修改为如下行:

            extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so

            image.png


            Note:如果xcache.ini中有多条extension指令行,要确保此新增的行排在第一位;

        3)      再次访问压力测试;

          image.png

    可以发现,传输速率得到了很大的提升;

 

第二部分:配置apache-2.4.9以fpm方式的php-5.4.26

一、     apache、mysql的安装与前一部分相同;根据以上安装;

二、     编译安装php-5.4.26

1.      解决依赖关系

首先配置号yum源(系统安装及epel源)后执行如下命令

# yum -y groupinstall "Desktop Platform Development"

# yum -y install bzip2-devel libmcrypt-devel libxml2-devel

2.      编译安装php-5.4.26

        1)      下载源码至本地目录;

image.png  

        2)      解压缩文件并进入php-5.4.26目录;

          image.png 

        3)      编译安装php,执行如下指令;

        # ./configure --prefix=/usr/local/php5-fpm--with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --enable-fpm --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2

        4)      编译及安装;

        make && make install

3.      为php提供配置文件;

    #cp php.ini-production  /etc/php.ini

    image.png

4.      配置php-fpm;

        1)      为php-fpm提供SysV init脚本,将其添加至服务列表;

           image.png 

        2)      为php-fpm提供配置文件;

           image.png

        3)      编辑php-fpm配置文件;

            配置fpm的相关选项为你所需要的值,并启用pid文件;

            image.png

            启用pid文件:

            image.png

        4)      启动服务并测试;

          image.png  

5.      配置httpd,使apache支持php-fpm;

        1)      启动以下两个模块;

            image.png

        2)      添加文件类型;

            image.png

        3)      定位到DirectoryIndex  index.html,修改为以下;

            image.png

        4)      配置虚拟主机;

            image.png

    Note

    ProxyRequests Off:关闭正向代理

    ProxyPassMatch:把以.php结尾的文件请求发送到php-fpm进程,php-fpm至少需要知道运行的目录和URI,所以这里直接在fcgi://127.0.0.1:9000后指明了这两个参数,其它的参数的传递已经被mod_proxy_fcgi.so进行了封装,不需要手动指定。

6.      测试;

        1)      编辑index.php测试;

           image.png 

        2)      网页输入地址;

          image.png




本文转自 claude_liu 51CTO博客,原文链接:http://blog.51cto.com/claude666/2056707,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3天前
|
Linux 编译器 Android开发
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
在Linux环境下,本文指导如何交叉编译x265的so库以适应Android。首先,需安装cmake和下载android-ndk-r21e。接着,下载x265源码,修改crosscompile.cmake的编译器设置。配置x265源码,使用指定的NDK路径,并在配置界面修改相关选项。随后,修改编译规则,编译并安装x265,调整pc描述文件并更新PKG_CONFIG_PATH。最后,修改FFmpeg配置脚本启用x265支持,编译安装FFmpeg,将生成的so文件导入Android工程,调整gradle配置以确保顺利运行。
24 1
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
|
4月前
|
Linux 应用服务中间件 PHP
Linux下搭建PHP环境-源码编译安装PHP7.2
Linux下搭建PHP环境-源码编译安装PHP7.2
153 0
|
1月前
|
算法 Linux
【Linux笔记】压缩、解压文件的 4 种方式。tar、gzip、gunzip、zip、unzip、7z命令使用方法
【Linux笔记】压缩、解压文件的 4 种方式。tar、gzip、gunzip、zip、unzip、7z命令使用方法
|
2天前
|
Linux Android开发
Linux(6)CH9434 SPI调试笔记
Linux(6)CH9434 SPI调试笔记
10 0
|
2天前
|
Linux
Linux(5)WIFI/BT调试笔记
Linux(5)WIFI/BT调试笔记
16 0
|
15天前
|
关系型数据库 Linux PHP
linux 编译安装php7.2 实测!!
linux 编译安装php7.2 实测!!
10 0
|
19天前
|
Linux API C语言
FFmpeg开发笔记(一)搭建Linux系统的开发环境
本文指导初学者如何在Linux上搭建FFmpeg开发环境。首先,由于FFmpeg依赖第三方库,可以免去编译源码的复杂过程,直接安装预编译的FFmpeg动态库。推荐网站<https://github.com/BtbN/FFmpeg-Builds/releases>提供适用于不同系统的FFmpeg包。但在安装前,需确保系统有不低于2.22版本的glibc库。详细步骤包括下载glibc-2.23源码,配置、编译和安装。接着,下载Linux版FFmpeg安装包,解压至/usr/local/ffmpeg,并设置环境变量。最后编写和编译简单的C或C++测试程序验证FFmpeg环境是否正确配置。
37 8
FFmpeg开发笔记(一)搭建Linux系统的开发环境
|
1月前
|
Linux 网络安全 开发工具
【Linux笔记】常用的Linux的指令
【Linux笔记】常用的Linux的指令
|
2月前
|
Linux 数据安全/隐私保护 Android开发
10 个最佳 Linux 开源笔记软件
10 个最佳 Linux 开源笔记软件
115 1
|
2月前
|
缓存 Shell Linux
《linux鸟哥的私房菜》笔记(下)
《linux鸟哥的私房菜》笔记(下)
24 0