我试图让Browsersync,WordPress和Apache在WSL 2(Ubuntu)的Docker中工作。几年前,我在Windows上的XAMPP中使用了Browsersync,但我意识到这是一个更复杂的数量级。
除了Browsersync之外,一切都可以正常工作,我可以在Windows上通过端口80和443在mysite.localhost
访问WordPress,但如果我在http://mysite.localhost:3000
上使用Browsersync访问它,Apache日志不会响应,浏览器返回以下内容:
This site can’t be reached
mysite.localhost unexpectedly closed the connection
ERR_CONNECTION_CLOSED
如果我更改了一个文件,Browsersync会在日志中看到更改,但我无法让浏览器看到任何内容。我花了一个星期的时间,我发现最接近的是this question,作者自己回答说,虽然他们使用Nginx作为代理和Laravel,不知道有多大的区别。
以下是我的Docker Compose的相关部分:
services:
node:
build:
context: .
dockerfile: ./docker/node/Dockerfile
image: node:18.16.1-build
container_name: node
restart: always
depends_on:
- wordpress
# user: "node"
expose:
- 3000
- 3001
volumes:
- ./wordpress/source/themes/omikuji/src:/home/node/omikuji/src
- ./wordpress/source/themes/omikuji/dist:/home/node/omikuji/dist
- ./wordpress/source/themes/omikuji/bs-config.js:/home/node/omikuji/bs-config.js
command: "npm run dev"
networks:
- apache
database:
#...database stuff
networks:
- apache
wordpress:
build:
context: .
dockerfile: ./docker/wordpress/Dockerfile
image: wordpress:6.2.2-php8.1-build
container_name: wordpress
restart: always
hostname: mysite.localhost
depends_on:
database:
condition: service_healthy
expose:
- 80
volumes:
- ./wordpress/source/themes/omikuji/dist:/var/www/html/wp-content/themes/omikuji
environment:
TZ: ${TZ} # Set local time
WORDPRESS_DB_HOST: database
WORDPRESS_DB_USER: ${MARIADB_USER}
WORDPRESS_DB_PASSWORD: ${MARIADB_PASSWORD}
WORDPRESS_DB_NAME: ${MARIADB_DATABASE}
WORDPRESS_DEBUG: 1
networks:
- apache
phpmyadmin:
# ...phpmyadmin stuff
networks:
- apache
apache:
build:
context: .
dockerfile: ./docker/apache/Dockerfile
image: httpd:2.4-build
container_name: apache
ports:
- 80:80
- 443:443
- 3000:3000
- 3001:3001
environment:
TZ: ${TZ} # Set local time
networks:
- apache
volumes:
db_data:
external: true
networks:
apache:
name: apache
browsersync配置:
module.exports = {
"logLevel": "debug",
"logConnections": false,
"logFileChanges": true,
"logSnippet": true,
"open": false,
"files": ["./dist/style.css", "./dist/js/*.js", "./dist/*.php"],
"proxy": "http://node:3000",
"host": "node",
"port": 3000,
"reloadDebounce": 2000,
"watchOptions": {
"awaitWriteFinish": true
}
};
httpd-vhosts.conf中的Apache虚拟主机:
<VirtualHost *:80>
ServerName localhost
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
<VirtualHost *:80>
ServerName mysite.localhost
# SSL redirection is in .htaccess
# Reverse proxy
ProxyPreserveHost On
ProxyPass / http://wordpress:80/
ProxyPassReverse / http://wordpress:80/
RequestHeader set X-Forwarded-Proto http
</VirtualHost>
<VirtualHost *:443>
ServerName mysite.localhost
# Reverse proxy
ProxyPreserveHost On
ProxyPass / http://wordpress:80/
ProxyPassReverse / http://wordpress:80/
RequestHeader set X-Forwarded-Proto https
# Enable SSL
SSLEngine on
SSLCertificateFile /etc/ssl/certs/mysite.localhost.pem
SSLCertificateKeyFile /etc/ssl/private/mysite.localhost-key.pem
</VirtualHost>
<VirtualHost *:3000>
ServerName mysite.localhost
# Reverse proxy
ProxyPreserveHost On
ProxyPass / http://node:3000/
ProxyPassReverse / http://node:3000/
RequestHeader set X-Forwarded-Proto http
# Enable SSL
SSLEngine on
SSLCertificateFile /etc/ssl/certs/mysite.localhost.pem
SSLCertificateKeyFile /etc/ssl/private/mysite.localhost-key.pem
</VirtualHost>
在WordPress目录的根目录下重写.htaccess中的规则:
RewriteEngine On
# If the request is not https on the development server (reverse proxy)
RewriteCond %{HTTP:X-Forwarded-Proto} !https
# If the request is not https on the production server
RewriteCond %{HTTPS} !=on
# Redirect to SSL
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Apache Dockerfile:
FROM httpd:2.4
# Copy the configuration files
WORKDIR /usr/local/apache2/conf
COPY ./localhost/apache/httpd.conf httpd.conf
# Copy the configuration extra files
WORKDIR /usr/local/apache2/conf/extra
COPY ./localhost/apache/httpd-vhosts.conf httpd-vhosts.conf
COPY ./localhost/apache/httpd-ssl.conf httpd-ssl.conf
COPY ./localhost/apache/proxy-html.conf proxy-html.conf
# Copy the SSL certificates
WORKDIR /etc/ssl/certs
COPY ./localhost/certs/localhost.pem localhost.pem
COPY ./localhost/certs/mysite.localhost.pem mysite.localhost.pem
COPY ./localhost/certs/phpmyadmin.localhost.pem phpmyadmin.localhost.pem
# Copy the SSL certificate private keys
WORKDIR /etc/ssl/private
COPY ./localhost/private/localhost-key.pem localhost-key.pem
COPY ./localhost/private/mysite.localhost-key.pem mysite.localhost-key.pem
COPY ./localhost/private/phpmyadmin.localhost-key.pem phpmyadmin.localhost-key.pem
# Change to default directory
WORKDIR /usr/local/apache2
Node Dockerfile:
FROM node:18.16.1
# Copy SSL certificates
WORKDIR /etc/ssl/certs
COPY ./localhost/certs/mysite.localhost.pem mysite.localhost.pem
WORKDIR /etc/ssl/private
COPY ./localhost/private/mysite.localhost-key.pem mysite.localhost-key.pem
# Copy configuration files to root
RUN mkdir /home/node/omikuji
WORKDIR /home/node/omikuji
COPY ./wordpress/source/themes/omikuji/.stylelintrc.json .stylelintrc.json
COPY ./wordpress/source/themes/omikuji/package.json package.json
COPY ./wordpress/source/themes/omikuji/package-lock.json package-lock.json
# Install npm packages
RUN npm install
# Add environment variables after installing npm
ENV NPM_CONFIG_LOGLEVEL info
ENV NODE_ENV development
节点日志响应文件更改:
* Executing task in folder Development: docker logs --tail 1000 -f 78ed8100eca903cd446d057b7da9f05fefac45f31d97d865e9b32a9264c15665
npm info using [email protected]
npm info using [email protected]
> [email protected] dev
> npm-run-all --parallel localhost dev:scss
npm info using [email protected]
npm info using [email protected]
npm info using [email protected]
npm info using [email protected]
> [email protected] dev:scss
> sass --watch --verbose --style=expanded ./src/scss/style.scss:./dist/style.css
> [email protected] localhost
> browser-sync start --config 'bs-config.js'
[Browsersync] [debug] -> Starting Step: Finding an empty port
[Browsersync] [debug] Found a free port: 3000
[Browsersync] [debug] Setting Option: {cyan:port} - {magenta:3000
[Browsersync] [debug] + Step Complete: Finding an empty port
[Browsersync] [debug] -> Starting Step: Getting an extra port for Proxy
[Browsersync] [debug] + Step Complete: Getting an extra port for Proxy
[Browsersync] [debug] -> Starting Step: Checking online status
[Browsersync] [debug] Resolved www.google.com, setting online: true
[Browsersync] [debug] Setting Option: {cyan:online} - {magenta:true
[Browsersync] [debug] + Step Complete: Checking online status
[Browsersync] [debug] -> Starting Step: Resolve user plugins from options
[Browsersync] [debug] + Step Complete: Resolve user plugins from options
[Browsersync] [debug] -> Starting Step: Set Urls and other options that rely on port/online status
[Browsersync] [debug] Setting multiple Options
[Browsersync] [debug] + Step Complete: Set Urls and other options that rely on port/online status
[Browsersync] [debug] -> Starting Step: Setting Internal Events
[Browsersync] [debug] + Step Complete: Setting Internal Events
[Browsersync] [debug] -> Starting Step: Setting file watchers
[Browsersync] [debug] + Step Complete: Setting file watchers
[Browsersync] [debug] -> Starting Step: Merging middlewares from core + plugins
[Browsersync] [debug] Setting Option: {cyan:middleware} - {magenta:List []
[Browsersync] [debug] + Step Complete: Merging middlewares from core + plugins
[Browsersync] [debug] -> Starting Step: Starting the Server
[Browsersync] [debug] Proxy running, proxing: {magenta:http://node:3000}
[Browsersync] [debug] Running mode: PROXY
[Browsersync] [debug] + Step Complete: Starting the Server
[Browsersync] [debug] -> Starting Step: Starting the HTTPS Tunnel
[Browsersync] [debug] + Step Complete: Starting the HTTPS Tunnel
[Browsersync] [debug] -> Starting Step: Starting the web-socket server
[Browsersync] [debug] Setting Option: {cyan:clientEvents} - {magenta:List [ "scroll", "scroll:element", "input:text", "input:toggles", "form:submit", "form:reset", "click" ]
[Browsersync] [debug] + Step Complete: Starting the web-socket server
[Browsersync] [debug] -> Starting Step: Starting the UI
[Browsersync] [debug] Setting Option: {cyan:session} - {magenta:1690129836067
[Browsersync] [UI] Starting Step: Setting default plugins
[Browsersync] [UI] Step Complete: %s Setting default plugins
[Browsersync] [UI] Starting Step: Finding a free port
[Browsersync] [UI] Step Complete: %s Finding a free port
[Browsersync] [UI] Starting Step: Setting options also relevant to UI from BS
[Browsersync] [UI] Step Complete: %s Setting options also relevant to UI from BS
[Browsersync] [UI] Starting Step: Setting available URLS for UI
[Browsersync] [debug] Getting option via path: {magenta:[ 'urls' ]
[Browsersync] [UI] Step Complete: %s Setting available URLS for UI
[Browsersync] [UI] Starting Step: Starting the Control Panel Server
[Browsersync] [UI] Using port 3001
[Browsersync] [UI] Step Complete: %s Starting the Control Panel Server
[Browsersync] [UI] Starting Step: Add element events
[Browsersync] [UI] Step Complete: %s Add element events
[Browsersync] [UI] Starting Step: Registering default plugins
[Browsersync] [UI] Step Complete: %s Registering default plugins
[Browsersync] [UI] Starting Step: Add options setting event
[Browsersync] [UI] Step Complete: %s Add options setting event
[Browsersync] [debug] + Step Complete: Starting the UI
[Browsersync] [debug] -> Starting Step: Merge UI settings
[Browsersync] [debug] Setting Option: {cyan:urls} - {magenta:Map { "local": "http://localhost:3000", "external": "http://node:3000", "ui": "http://localhost:3001", "ui-external": "http://localhost:3001" }
[Browsersync] [debug] + Step Complete: Merge UI settings
[Browsersync] [debug] -> Starting Step: Init user plugins
[Browsersync] [debug] Setting Option: {cyan:userPlugins} - {magenta:
[Browsersync] [debug] + Step Complete: Init user plugins
[Browsersync] Proxying: http://node:3000
[Browsersync] Access URLs:
----------------------------------
Local: http://localhost:3000
External: http://node:3000
----------------------------------
UI: http://localhost:3001
UI External: http://localhost:3001
----------------------------------
[Browsersync] Watching files...
Sass is watching for changes. Press Ctrl-C to stop.
[2023-07-23 16:30] Compiled src/scss/style.scss to dist/style.css.
[Browsersync] File event [change] : dist/style.css
1条答案
按热度按时间k4aesqcs1#
Browsersync中的一些东西似乎与Node不兼容,因为我写了自己的Node服务器,它在第一次尝试时连接到同一个docker WordPress服务的同一个目标地址和端口。
我的服务器完全可以使用live reload和SSL(hat tip chatGPT),我不再需要Browsersync。如果其他人有兴趣学习如何做到这一点,这些是我使用的Node包。
chokidar包用于监视文件的更改,socket.io包用于实时重新加载,其他包用于核心服务器,SSL,代理请求到Docker WordPress容器以及注入Socket.io所需的HTML。
在Apache虚拟主机中,代理地址指向Docker中的Node容器的名称和端口,其中我的Node容器名称为
node
,端口为3000
。还请注意
upgrade=websocket
指令。这是支持WebSocket的更新方法,是您所需要的全部,比不推荐的方法更容易使用WebSocket重写和路径重定向。