debian12或centos9下编译apache2.4和php7.4
一、apache和php目录及安装运行组件创建apache用户
OS:debian12或centos9系
目录和配置文件地址
apache:/usr/local/apache
apache:/usr/local/apache/etc
php:/usr/local/php
php:/usr/local/php/php.ini
创建运行用户groupadd apache&&useradd -g apache -s /sbin/nologin apache
二、安装支持组件
debian/ubuntu
apt update -y
apt upgrade -y
apt install build-essential autoconf automake libtool flex bison libpcre3 libpcre3-dev libssl-dev zlib1g-dev -y
apt install libnghttp2-dev libexpat1-dev libxml2-dev libjansson-dev brotli libcurl4-openssl-dev -y
apt install sqlite3 libsqlite3-dev bzip2 libbz2-dev libpng-dev libwebp-dev libjpeg-dev libfreetype6-dev libonig-dev libreadline-dev libzip-dev libpng-dev libjpeg-dev libsasl2-dev lua5.4 liblua5.4-dev libluajit2-5.1-dev -y
centos9系或以上系统
yum update -y
yum groupinstall "Development Tools" -y
yum install pcre-devel openssl-devel nghttp2 libnghttp2-devel expat-devel -y
yum install cmake libtool automake autoconf libcurl-devel jansson-devel lua-devel luajit-devel libxml2-devel sqlite-devel bzip2-devel libwebp-devel freetype-devel oniguruma-develreadline-devel libzip-devel brotli-devel -y
三、下载安装apache
wget https://dlcdn.apache.org/httpd/httpd-2.4.63.tar.gz
wget https://mirrors.aliyun.com/apache/apr/apr-1.7.5.tar.gz
wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.3.tar.gz
tar zxf httpd-2.4.63.tar.gz
tar zxf apr-1.7.5.tar.gz
tar zxf apr-util-1.6.3.tar.gz
编译apache
cd apr-1.7.5
sed -i 's:$RM "$cfgfile":#$RM "$cfgfile":g' configure
./configure --prefix=/usr/local/apr
make -j4&&make install
cd ..
cd apr-util-1.6.3
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr --with-crypto --with-openssl
make -j4&&make install
cd ..
cd httpd-2.4.63
./configure --prefix=/usr/local/apache --sysconfdir=/usr/local/apache/etc --enable-so --enable-ssl --with-ssl --enable-cgi --enable-proxy --enable-session --enable-http2 --enable-rewrite --enable-deflate --with-zlib --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre --enable-mpms-shared=all --with-mpm=prefork --libdir=/usr/lib64 --enable-ssl --with-ssl --enable-cache --enable-disk-cache --enable-mem-cache --enable-expires --enable-mods-shared=all --enable-brotli --with-brotli --with-curl
make -j4&&make install
cd ..
取消注释一些需要的组件
sed -i 's:#LoadModule session_module modules/mod_session.so:LoadModule session_module modules/mod_session.so:g' /usr/local/apache/etc/httpd.conf
sed -i 's:#LoadModule deflate_module modules/mod_deflate.so:LoadModule deflate_module modules/mod_deflate.so:g' /usr/local/apache/etc/httpd.conf
sed -i 's:#LoadModule proxy_module modules/mod_proxy.so:LoadModule proxy_module modules/mod_proxy.so:g' /usr/local/apache/etc/httpd.conf
sed -i 's:#LoadModule ssl_module modules/mod_ssl.so:LoadModule ssl_module modules/mod_ssl.so:g' /usr/local/apache/etc/httpd.conf
sed -i 's:#LoadModule http2_module modules/mod_http2.so:LoadModule http2_module modules/mod_http2.so:g' /usr/local/apache/etc/httpd.conf
sed -i 's:#LoadModule cache_module modules/mod_cache.so:LoadModule cache_module modules/mod_cache.so:g' /usr/local/apache/etc/httpd.conf
sed -i 's:#LoadModule cache_disk_module modules/mod_cache_disk.so:LoadModule cache_disk_module modules/mod_cache_disk.so:g' /usr/local/apache/etc/httpd.conf
sed -i 's:#LoadModule rewrite_module modules/mod_rewrite.so:LoadModule rewrite_module modules/mod_rewrite.so:g' /usr/local/apache/etc/httpd.conf
sed -i 's:#LoadModule vhost_alias_module modules/mod_vhost_alias.so:LoadModule vhost_alias_module modules/mod_vhost_alias.so:g' /usr/local/apache/etc/httpd.conf
sed -i 's:#LoadModule socache_shmcb_module modules/mod_socache_shmcb.so:LoadModule socache_shmcb_module modules/mod_socache_shmcb.so:g' /usr/local/apache/etc/httpd.conf
sed -i 's:#LoadModule session_module modules/mod_session.so:LoadModule session_module modules/mod_session.so:g' /usr/local/apache/etc/httpd.conf
sed -i 's:#LoadModule filter_module modules/mod_filter.so:LoadModule filter_module modules/mod_filter.so:g' /usr/local/apache/etc/httpd.conf
sed -i 's:#LoadModule brotli_module modules/mod_brotli.so:LoadModule brotli_module modules/mod_brotli.so:g' /usr/local/apache/etc/httpd.conf
sed -i 's:#LoadModule expires_module modules/mod_expires.so:LoadModule expires_module modules/mod_expires.so:g' /usr/local/apache/etc/httpd.conf
sed -i 's/User daemon/User apache/' /usr/local/apache/etc/httpd.conf
sed -i 's/Group daemon/Group apache/' /usr/local/apache/etc/httpd.conf
开启GZIP,在/usr/local/apache/etc/httpd.conf的最后录入以下代码
<IfModule mod_deflate.c>
DeflateCompressionLevel 9
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
AddOutputFilter DEFLATE css js
</IfModule>
开启br,在/usr/local/apache/etc/httpd.conf的最后录入以下代码
<IfModule brotli_module>
SetOutputFilter BROTLI_COMPRESS;DEFLATE
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip no-brotli dont-vary
BrotliCompressionQuality 6
BrotliCompressionWindow 18
AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css text/xml
AddOutputFilterByType BROTLI_COMPRESS application/x-javascript application/javascript
AddOutputFilterByType BROTLI_COMPRESS application/rss+xml
AddOutputFilterByType BROTLI_COMPRESS application/xml
AddOutputFilterByType BROTLI_COMPRESS application/json
</IfModule>
替换成你的服务器IP地址做测试
sed -i 's/#ServerName www.example.com:80/ServerName 你的服务器ip:80/g' /usr/local/apache/etc/httpd.conf
添加为系统服务
vi /lib/systemd/system/apache.service
输入以下代码
[Unit]
Description=apache
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecReload=/usr/local/apache/bin/apachectl restart
ExecStop=/usr/local/apache/bin/apachectl stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
加入系统启动systemctl enable apache
开启apachesystemctl start apache
重启apachesystemctl restart apache
停止apachesystemctl stop apache
四、下载编译安装php
wget https://www.php.net/distributions/php-7.4.33.tar.gz
tar zxf php-7.4.33.tar.gz
cd php-7.4.33
wget https://source.loshub.com/linux/php/patch/php-7.4-openssl3.0.patch
patch -p1 < php-7.4-openssl3.0.patch
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --with-openssl --with-pdo-mysql --with-mysqli --with-iconv --with-zlib --with-curl --with-xmlrpc --with-gettext --with-pear --disable-rpath --enable-fileinfo --enable-fpm --enable-mysqlnd --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-intl --enable-gd-jis-conv --enable-pcntl --enable-sockets --enable-opcache --enable-maintainer-zts --enable-gd --with-jpeg --with-freetype --with-zip --with-webp --with-bz2 --with-readline --with-libxml --enable-xml
make -j4&&make install
cp php.ini-production /usr/local/php/php.ini
sed -i 's#;session.save_path = "/tmp"#session.save_path = "/tmp"#g' /usr/local/php/php.ini
sed -i 's/disable_functions =/disable_functions=eval,passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,disk_total_space,disk_free_space,error_log,putenv,popen,ini_set,chmod,assert,pcntl_exec,phpfunc;/' /usr/local/php/php.ini;
sed -i 's/post_max_size/;post_max_size/' /usr/local/php/php.ini;
echo 'post_max_size=100M' >>/usr/local/php/php.ini;
sed -i 's/upload_max_filesize/;upload_max_filesize/' /usr/local/php/php.ini;
echo 'upload_max_filesize=100M' >>/usr/local/php/php.ini
sed -i 's/max_execution_time/;max_execution_time/' /usr/local/php/php.ini
sed -i 's/;cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/php.ini
sed -i 's/;date.timezone =.*/date.timezone = PRC/g' /usr/local/php/php.ini
sed -i 's/short_open_tag =.*/short_open_tag = On/g' /usr/local/php/php.ini
echo 'max_execution_time=300' >>/usr/local/php/php.ini
echo 'zend_extension = opcache.so ' >>/usr/local/php/php.ini
echo 'opcache.enable=1' >>/usr/local/php/php.ini
echo 'opcache.memory_consumption = 128' >>/usr/local/php/php.ini
echo 'opcache.interned_strings_buffer = 8' >>/usr/local/php/php.ini
echo 'opcache.max_accelerated_files = 10000' >>/usr/local/php/php.ini
echo 'opcache.revalidate_freq = 2' >>/usr/local/php/php.ini
echo 'opcache.enable_cli = 1' >>/usr/local/php/php.ini
五、apache结合php
在主配置文件/usr/local/apache/etc/httpd.conf<IfModule mime_module><\IfModule>
之间插入行: AddType application/x-httpd-php .php
或都直接运行下面的命令
hhifmime=`grep -n "<IfModule mime_module>" /usr/local/apache/etc/httpd.conf|cut -d ':' -f 2 --complement`
let result=$hhifmime+6
sed -i "${result}aAddType application/x-httpd-php .php\n" /usr/local/apache/etc/httpd.conf
测试在/usr/local/apache/htdocs下创建一个php信息文件
info.php
录入以下代码<?php phpinfo(); ?>
完成
六、多站点
在/usr/local/apache/etc/目录下创建vhosts目录mkdir -p /usr/local/apache/etc/vhosts
并修改/usr/local/apache/etc/httpd.conf文件,直接运行以下代码
vhosts=`grep -n "# Virtual hosts" /usr/local/apache/etc/httpd.conf|cut -d ':' -f 2 --complement`
let result=$vhosts+2
sed -i "${result}aIncludeOptional etc/vhosts/*.conf\n" /usr/local/apache/etc/httpd.conf
安全生产防跨域,找到<Directory的下一行,
语法:php_admin_value open_basedir 目录
例默认的
php_admin_value open_basedir "${SRVROOT}/htdocs"
多站点样式
站点目录/home/wwwroot/test.com/wwwroot
权限设置chown apache:apache /home/wwwroot/test.com -R
在/usr/local/apache/etc/vhosts下创建一个test.com.conf
录入以下信息
<VirtualHost *:80>
ServerName www.test.com
DocumentRoot "/home/wwwroot/test.com/wwwroot"
<Directory "/home/wwwroot/test.com/wwwroot">
DirectoryIndex index.html index.htm index.php default.php
php_admin_value open_basedir "/home/wwwroot/test.com"
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
CustomLog /home/wwwroot/test.com/access.log combined
ErrorLog /home/wwwroot/test.com/error.log
</VirtualHost>
开启sslsed -i 's:#Include etc/extra/httpd-ssl.conf:Include etc/extra/httpd-ssl.conf:g' /usr/local/apache/etc/httpd.conf
编辑/usr/local/apache/etc/extra/httpd-ssl.conf
从
在/usr/local/apache/etc/vhosts/test.com.conf的最后录入以下信息
#以下是SSL
<VirtualHost *:443>
ServerName www.test.com
DocumentRoot "/home/wwwroot/test.com/wwwroot"
<Directory "/home/wwwroot/test.com/wwwroot">
DirectoryIndex index.html index.htm index.php default.php
php_admin_value open_basedir "/home/wwwroot/test.com"
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
SSLEngine On
SSLCertificateFile "/home/wwwroot/test.com/ssl.pem"
SSLCertificateKeyFile "/home/wwwroot/test.com/ssl.key"
CustomLog /home/wwwroot/test.com/access.log combined
ErrorLog /home/wwwroot/test.com/error.log
</VirtualHost>
本文系作者 @天边的云 原创发布在Loshub站点。未经许可,禁止转载。
暂无评论数据