編譯安裝php+apache環境

編譯安裝php+apache環境

注意事項:

1、主要是配合apache的,所以剛開始創建了apache用戶

腳本

#!/bin/bashRED="\033[0;31m"GREEN="\033[0;32m"NO_COLOR="\033[0m"PREFIX=/usr/local/php    #這個還是別改了,好多地方要改SYSCONFDIR=
SRC=/usr/src
FLAG=$1CPUS=`cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l`
CORE=`cat /proc/cpuinfo| grep "cpu cores"| uniq | awk '{print $4}'`
J=$((${CPUS}*${CORE}))
FILEURL='http://ftp.ntu.edu.tw/pub/php/distributions/php-7.3.10.tar.gz'# 判斷是不是rootjudge_root() {
    [ $(id -u) != "0" ] && { echo -e "${RED}Error:${NO_COLOR} You must be root to run this script."; exit 1; }   
}# download download_source() {    cd
    yum install wget -y
    wget ${FILEURL}
    if [ ! "$?" -eq 0 ];then
        echo "download failed!"
        exit 1    fi}# installinstall(){    # Add the "apache" group and user,需要的話改爲nginx
    /usr/sbin/groupadd -g 48 -r apache 2> /dev/null || :
    /usr/sbin/useradd -c "Apache" -u 48 -g apache \
    -s /sbin/nologin -r -d /usr/share/httpd apache 2> /dev/null || : 
    # epel
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    yum install gcc openssl-devel pcre-devel libnghttp2-devel ncurses-devel  lbzip2  bzip2 expat-devel libxml2-devel libxml2  autoconf libtool -y
    tar xf php-7.3.10.tar.gz -C ${SRC}/    cd ${SRC}/php-7.3.10/
    ./configure --prefix=/usr/local/php --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-config-file-path=/usr/loca/php/etc --with-config-file-scan-dir=/usr/local/php/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo
    make -j ${J}
    make install    # config
    cd ${PREFIX}/etc/
    cp php-fpm.conf.default php-fpm.conf    cd ${PREFIX}/etc/php-fpm.d
    cp www.conf.default www.conf    # init file
    cp ${SRC}/php-7.3.10/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    chmod +x /etc/init.d/php-fpm    # modify config file
    sed -i 's@user = nobody@user = apache@' ${PREFIX}/etc/php-fpm.d/www.conf
    sed -i 's@group = nobody@group = apache@' ${PREFIX}/etc/php-fpm.d/www.conf
    sed -i 's@listen = 127.0.0.1:9000@listen = /var/run/php-fpm.sock@' ${PREFIX}/etc/php-fpm.d/www.conf
    sed -i 's@;listen.mode = [email protected] = 0666@' ${PREFIX}/etc/php-fpm.d/www.conf    #reload
    systemctl daemon-reload   
}test_php(){
    systemctl start php-fpm
    sleep 3    if  ps -ef |  grep php-fpm | grep -v grep;then
        echo -e "${GREEN}Maybe php installed successfully...${NO_COLOR}"
    else
        echo -e "${RED}Maybe php installed failed...${NO_COLOR}"
    fi}remove_php() {
    systemctl stop php-fpm
    sleep 3    if  ps -ef |  grep php-fpm | grep -v grep;then
        echo -e "${RED}Php uninstall failed...Killing php process failed...${NO_COLOR}"
        exit 2    fi
    rm -rf ${PREFIX}  ${SRC}/php-7.3.10 /etc/init.d/php-fpm
}judge_uninstall(){if [ "$FLAG" = "uninstall" ];then
    remove_php    exit 0fi}main() {
    judge_uninstall
    judge_root
    download_source
    install
    test_php   
}

main

編譯安裝httpd

#!/bin/bash#**************************************************************#Author:                     哈囉#QQ:                         xxxxxxxxx#Date:                       2019-08-08#FileName:                   install_httpd.sh#URL:                        https://blog.51cto.com/14012942#Description:                The test script#Copyright (C):              2019 Copyright ©  站點名稱  版權所有#************************************************************#set -eRED="\033[0;31m"GREEN="\033[0;32m"NO_COLOR="\033[0m"PREFIX=/usr/local/httpd2.4.41
SYSCONFDIR=/etc/httpd
SRC=/usr/src
FLAG=$1CPUS=`cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l`
CORE=`cat /proc/cpuinfo| grep "cpu cores"| uniq | awk '{print $4}'`
J=$((${CPUS}*${CORE}))
FILEURL='https://ftp.bit.nl/apache/httpd-2.4.41.tar.gz https://archive.apache.org/dist/apr/apr-1.7.0.tar.gz https://archive.apache.org/dist/apr/apr-util-1.6.1.tar.gz'# 判斷是不是rootjudge_root() {
    [ $(id -u) != "0" ] && { echo -e "${RED}Error:${NO_COLOR} You must be root to run this script."; exit 1; }   
}# download download_source() {    cd
    yum install wget -y    for i in ${FILEURL};do
    {
        wget ${i}
        if [ ! "$?" -eq 0 ];then
            echo "download failed!"
            exit 1        fi
    }    done}# installinstall() {    # Add the "apache" group and user
    /usr/sbin/groupadd -g 48 -r apache 2> /dev/null || :
    /usr/sbin/useradd -c "Apache" -u 48 -g apache \
    -s /sbin/nologin -r -d /usr/share/httpd apache 2> /dev/null || : 
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    yum install gcc openssl-devel pcre-devel libnghttp2-devel ncurses-devel  lbzip2  bzip2 expat-devel autoconf libtool -y
    tar xf httpd-2.4.41.tar.gz -C ${SRC}/    #apr這個解壓到srclib目錄即可,官方文檔說的很明白了,如下
    #if you   need to use the APR and APR-Util from the apr.apache.org  project. If the latter, download the latest versions and unpack them to ./srclib/apr and ./srclib/apr-util (no  version numbers in the directory names) and use  ./configure's --with-included-apr option
    tar xf  apr-util-1.6.1.tar.gz -C /usr/src/httpd-2.4.41/srclib/
    tar xf apr-1.7.0.tar.gz -C /usr/src/httpd-2.4.41/srclib/
    mv /usr/src/httpd-2.4.41/srclib/apr-util-1.6.1 /usr/src/httpd-2.4.41/srclib/apr-util
    mv /usr/src/httpd-2.4.41/srclib/apr-1.7.0  /usr/src/httpd-2.4.41/srclib/apr    cd ${SRC}/httpd-2.4.41
    ./configure \
    --prefix=${PREFIX} \
    --sysconfdir=${SYSCONFDIR} \
    --enable-http2 \
    --enable-ssl \
    --enable-so \
    --enable-cgi \
    --enable-rewrite \
    --with-zlib \
    --with-pcre \
    --with-included-apr \
    --enable-modules=most \
    --enable-mpms-shared=all \
    --with-mpm=prefork 
    make -j ${J}
    make install    # 使用system啓動就不要path變量了,不,還得要
    echo "PATH=${PREFIX}/bin:$PATH" >> /etc/profile.d/env.sh    # source /etc/profile.d/env.sh
    cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=simple
EnvironmentFile=${SYSCONFDIR}/httpd.conf
ExecStart=${PREFIX}/bin/apachectl -k start  -DFOREGROUND
ExecReload=${PREFIX}/bin/apachectl  -k graceful
ExecStop=/usr/bin/kill -WINCH ${MAINPID}PrivateTmp=true[Install]
WantedBy=multi-user.target

EOF    #修改apache啓動的用戶
    sed  '/^Group/ s/daemon/apache/' /etc/httpd/httpd.conf  -i
    sed  '/^User/ s/daemon/apache/' /etc/httpd/httpd.conf  -i    #加載一下
    systemctl daemon-reload
}# test_webtest_web() {    #防止啓動太慢,後續可以改
    sed  '/#ServerName www.example.com/a ServerName www.example.com:80' ${SYSCONFDIR}/httpd.conf -i    # apachectl start
    systemctl start httpd
    ss -ltn | grep -q :80
    [ "$?" -eq 0 ] && echo -e "${GREEN}May be web server is ok! \n If not ok,please check selinux and firewalld status.${NO_COLOR}" || \        echo -e "${RED}ERROR,Please check the web server.${NO_COLOR}"}remove_httpd() {    # source /etc/profile.d/env.sh
    # apachectl stop
    systemctl stop httpd
    rm -rf ${PREFIX} ${SYSCONFDIR} ${SRC}/httpd-2.4.41 /usr/lib/systemd/system/httpd.service 
    sed -i '/^PATH/d' /etc/profile.d/env.sh
}judge_uninstall(){if [ "$FLAG" = "uninstall" ];then
    remove_httpd    exit 0fi}main() {
    judge_uninstall
    judge_root
    download_source
    install
    test_web
}

main

測試

cat >> /etc/httpd/httpd.conf <<'EOF'LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
LoadModule proxy_fdpass_module modules/mod_proxy_fdpass.so
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ unix:/var/run/php-fpm.sock|fcgi://localhost/var/www/html
EOF

sed  's/DirectoryIndex index.html/DirectoryIndex index.php index.html/' /etc/httpd/httpd.conf -i

sed -r 's@/usr/local/httpd2.4.41/htdocs@/var/www/html@' /etc/httpd/httpd.conf -i

mkdir /var/www/html -p
cat > /var/www/html/index.php <<EOF
<?
    phpinfo();
?>
EOF

httpd -t 
systemctl restart httpd

卸載

[root@node1 ~]# bash php-7.3.10.sh uninstall[root@node1 ~]# bash httpd.sh uninstall


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章