Linux中间件(nginx搭建、LNMP服务搭建)
以下是使用Docker搭建Nginx和PHP-FPM的示例代码:
首先,创建一个名为docker-compose.yml
的文件,内容如下:
version: '3'
services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./html:/usr/share/nginx/html
depends_on:
- php-fpm
networks:
- app-network
php-fpm:
image: php:7.4-fpm
volumes:
- ./html:/usr/share/nginx/html
networks:
- app-network
networks:
app-network:
driver: bridge
然后,创建一个名为nginx.conf
的文件,用于配置Nginx:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/html;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
最后,在html
目录中创建一个简单的index.php
文件,以便Nginx可以处理PHP请求:
<?php
echo "Hello, World!";
在这个配置中,Nginx接收到.php
请求时会转发给php-fpm
服务,而php-fpm
服务处理这些请求。
要启动服务,只需在包含这些文件的目录中运行以下命令:
docker-compose up -d
这将启动Nginx和PHP-FPM容器,并在后台运行。打开浏览器,访问服务器的IP地址或域名,你应该能看到Hello, World!
的输出。
评论已关闭