• 从零开始Apache2配置php服务并支持网页访问


    前言

    最近打算再做一版服务器状态查看的dashboard,采用“子服务器主动向主服务器推送自己的状态”的方案。一种方案是用php写,Apache作为后台服务器。简单写一下构建过程。

    TL;DR

    github有个docker compose做的lamp全套环境,只需要git clonedocker compose up -d两个命令就能直接搭好linux端的apache+mysql+php环境。试了下确实直接能用,可以参考https://github.com/sprintcube/docker-compose-lamp

    准备Apache2环境

    以下均为Ubuntu 22.04的wsl,以root运行。配置apache2需要apr、apr-util的runtime包,可以从https://apr.apache.org/download.cgi下载,以及正则、压缩传输、加密、xml的库。

    wget https://dlcdn.apache.org/apr/apr-1.7.4.tar.gz
    wget https://dlcdn.apache.org/apr/apr-util-1.6.3.tar.gz
    wget https://dlcdn.apache.org/httpd/httpd-2.4.58.tar.gz
    tar xf httpd-2.4.58.tar.gz
    tar xf apr-util-1.6.3.tar.gz
    tar xf apr-1.7.4.tar.gz
    mv apr-util-1.6.3 httpd-2.4.58/srclib/apr-util
    mv apr-1.7.4 httpd-2.4.58/srclib/apr
    
    apt install libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev libexpat1 libexpat1-dev
    
    cd httpd-2.4.58 && ./configure --prefix=/usr/local/apache2 # 可以设置下path
    make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    准备PHP环境

    https://www.php.net/downloads.php下载最新的php包,比如

    # 准备php所需要的一些环境
    wget https://pkgconfig.freedesktop.org/releases/pkg-config-0.29.2.tar.gz
    tar xf pkg-config-0.29.2.tar.gz
    cd pkg-config-0.29.2 && ./configure --with-internal-glib
    make install
    
    # 非常重要
    export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/lib/x86_64-linux-gnu/pkgconfig
    
    # 最简化安装,详细方式见下方
    apt install libxml2 libxml2-dev sqlite3 libsqlite3-dev
    wget https://www.php.net/distributions/php-8.2.12.tar.gz
    tar xf php-8.2.12.tar.gz
    cd php-8.2.12
    ./configure --prefix=/usr/local/src/php-8.2.12 --sysconfdir=/etc/php-8.2.12 --with-apxs2=/usr/local/src/apache2/bin/apxs  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    也可以参考这篇文章,配置更丰富的configure,以下的部分为参考该文章配置的,即

    apt install build-essential autoconf automake libtool libsqlite3-dev pkg-config libjpeg-dev libpng-dev libxml2-dev libbz2-dev libcurl4-gnutls-dev libssl-dev libffi-dev libwebp-dev libonig-dev libzip-dev libjpeg-dev
    cd php-8.2.12 && ./configure --prefix=/usr/local/src/php-8.2.12 --sysconfdir=/etc/php-8.2.12 --with-openssl --with-zlib --with-bz2 --with-curl --enable-bcmath --enable-gd --with-webp --with-jpeg --with-mhash --enable-mbstring --with-imap-ssl --with-mysqli --enable-exif --with-ffi --with-zip --enable-sockets --with-pcre-jit --enable-fpm --with-pdo-mysql --enable-pcntl --with-apxs2=/usr/local/src/apache2/bin/apxs
    
    
    • 1
    • 2
    • 3

    配置服务器

    如果按以上方式安装,则修改/usr/local/apache2/conf/httpd.conf文件的ServerName选项到实际ip和端口。比如ServerName 172.19.202.144:80。这里的ip可以运行ifconfig看一下。

    为了使apache对php文件进行解释执行,在httpd.conf文件添加:

    
        SetHandler application/x-httpd-php
    
    
    • 1
    • 2
    • 3

    设置文档目录,比如

    
    #
    # DirectoryIndex: sets the file that Apache will serve if a directory
    # is requested.
    #
    
        DirectoryIndex index.php index.html
    
    
    
    
    #
    # DocumentRoot: The directory out of which you will serve your
    # documents. By default, all requests are taken from this directory, but
    # symbolic links and aliases may be used to point to other locations.
    #
    DocumentRoot "/var/www/html"
    
        #
        # Possible values for the Options directive are "None", "All",
        # or any combination of:
        #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
        #
        # Note that "MultiViews" must be named *explicitly* --- "Options All"
        # doesn't give it to you.
        #
        # The Options directive is both complicated and important.  Please see
        # http://httpd.apache.org/docs/2.4/mod/core.html#options
        # for more information.
        #
        Options Indexes FollowSymLinks
    
        #
        # AllowOverride controls what directives may be placed in .htaccess files.
        # It can be "All", "None", or any combination of the keywords:
        #   AllowOverride FileInfo AuthConfig Limit
        #
        AllowOverride None
        #
        # Controls who can get stuff from this server.
        #
        Require all granted
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    检查下php模块是不是打开了:

    LoadModule php_module         modules/libphp.so
    
    • 1

    启动服务器

    apachestl start # 假设在准备环境阶段设置了path
    
    • 1

    随后本地浏览器直接访问localhost即可。

  • 相关阅读:
    亚马逊、ozon、美客多等平台的测评技术核心:提升跨境电商业绩的关键要素
    Day23-JDBC
    地平线旭日X3派小白上手
    go语言基础 -- 面向对象编程
    Qgis根据区域划分点、线面
    Java中System.getProperty()方法具有什么功能呢?
    npm i 依赖下载失败
    【Hash表】无重复字符的最长字串-力扣 3 题
    2022.07.01 C++并发编程笔记01
    基于PHP+MySQL班级信息发布和管理系统的设计与实现
  • 原文地址:https://blog.csdn.net/weixin_43483799/article/details/134338873