Skip to content

Commit

Permalink
Feat: 测试环境性能优化,降低资源占用
Browse files Browse the repository at this point in the history
  • Loading branch information
LuRenJiasWorld committed Nov 17, 2022
1 parent 02530ed commit b60c7a7
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 11 deletions.
12 changes: 9 additions & 3 deletions docker/testenv/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ FROM wp-editormd/builder:latest

# Install deps and envs
RUN apt update \
&& npm install -g pm2
&& apt upgrade -y

# Install utilities
RUN apt install -y net-tools

# Install php-fpm
RUN apt install -y php7.4-fpm
Expand All @@ -12,14 +15,17 @@ RUN apt install -y apache2 \
&& rm -rf /etc/apache2/sites-enabled/*

# Configure php-fpm for apache
RUN a2enmod proxy_fcgi setenvif \
RUN a2dismod php7.4 mpm_prefork mpm_event \
&& a2enmod proxy_fcgi setenvif rewrite mpm_worker \
&& a2enconf php7.4-fpm

# Copy scripts and configs
COPY script/start.sh /start.sh
COPY config/php.ini /etc/php/7.4/fpm/php.ini
COPY config/www.conf /etc/php/7.4/fpm/php-fpm.d/www.conf
COPY config/www.conf /etc/php/7.4/fpm/pool.d/www.conf
COPY config/apache2.conf /etc/apache2/apache2.conf
COPY config/mpm_worker.conf /etc/apache2/mods-enabled/
COPY config/php7.4-fpm.conf /etc/apache2/conf-enabled/

# Set scripts executable
RUN chmod +x /start.sh
Expand Down
2 changes: 1 addition & 1 deletion docker/testenv/config/apache2.conf
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ DocumentRoot "/home/htdocs"
# Relax access to content within /var/www.
#
<Directory "/home">
AllowOverride None
AllowOverride All
# Allow open access:
Require all granted
</Directory>
Expand Down
23 changes: 23 additions & 0 deletions docker/testenv/config/mpm_worker.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# worker MPM
# StartServers: initial number of server processes to start
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadLimit: ThreadsPerChild can be changed to this maximum value during a
# graceful restart. ThreadLimit can only be changed by stopping
# and starting Apache.
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestWorkers: maximum number of threads
# MaxConnectionsPerChild: maximum number of requests a server process serves

<IfModule mpm_worker_module>
ServerLimit 8
StartServers 1
MinSpareThreads 8
MaxSpareThreads 8
ThreadLimit 64
ThreadsPerChild 8
MaxRequestWorkers 64
MaxConnectionsPerChild 0
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
37 changes: 37 additions & 0 deletions docker/testenv/config/php7.4-fpm.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Redirect to local php-fpm if mod_php is not available
<IfModule !mod_php7.c>
<IfModule proxy_fcgi_module>
# Enable http authorization headers
<IfModule setenvif_module>
SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
</IfModule>

<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:fcgi://127.0.0.1:9000/"
</FilesMatch>

<Proxy "fcgi://127.0.0.1:9000" enablereuse=off max=8>
ProxySet timeout=600
</Proxy>

# The default configuration works for most of the installation, however it could
# be improved in various ways. One simple improvement is to not pass files that
# doesn't exist to the handler as shown below, for more configuration examples
# see https://wiki.apache.org/httpd/PHP-FPM
# <FilesMatch ".+\.ph(ar|p|tml)$">
# <If "-f %{REQUEST_FILENAME}">
# SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
# </If>
# </FilesMatch>
<FilesMatch ".+\.phps$">
# Deny access to raw php sources by default
# To re-enable it's recommended to enable access to the files
# only in specific virtual host or directory
Require all denied
</FilesMatch>
# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(ar|p|ps|tml)$">
Require all denied
</FilesMatch>
</IfModule>
</IfModule>
13 changes: 7 additions & 6 deletions docker/testenv/config/www.conf
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ group = www-data
; (IPv6 and IPv4-mapped) on a specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /run/php/php7.4-fpm.sock
; listen = /run/php/php7.4-fpm.sock
listen = 9000

; Set listen(2) backlog.
; Default Value: 511 (-1 on FreeBSD and OpenBSD)
Expand Down Expand Up @@ -100,7 +101,7 @@ listen.group = www-data
; pm.process_idle_timeout - The number of seconds after which
; an idle process will be killed.
; Note: This value is mandatory.
pm = dynamic
pm = ondemand

; The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
Expand All @@ -111,12 +112,12 @@ pm = dynamic
; forget to tweak pm.* to fit your needs.
; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
; Note: This value is mandatory.
pm.max_children = 5
pm.max_children = 8

; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic'
; Default Value: (min_spare_servers + max_spare_servers) / 2
pm.start_servers = 2
pm.start_servers = 1

; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
Expand All @@ -126,12 +127,12 @@ pm.min_spare_servers = 1
; The desired maximum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.max_spare_servers = 3
pm.max_spare_servers = 2

; The number of seconds after which an idle process will be killed.
; Note: Used only when pm is set to 'ondemand'
; Default Value: 10s
;pm.process_idle_timeout = 10s;
pm.process_idle_timeout = 15s;

; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
Expand Down
2 changes: 1 addition & 1 deletion docker/testenv/script/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mkdir -p /home/htdocs /run/php
chown -R www-data:www-data /home/htdocs

# Start php-fpm
pm2 start /usr/sbin/php-fpm7.4 --name="php-fpm7.4" -- -F
/usr/sbin/php-fpm7.4 -R -y /etc/php/7.4/fpm/php-fpm.conf

# Start apache
source /etc/apache2/envvars
Expand Down

0 comments on commit b60c7a7

Please sign in to comment.