{"id":442546,"date":"2026-04-04T12:19:17","date_gmt":"2026-04-04T04:19:17","guid":{"rendered":"https:\/\/idc.net\/help\/?p=442546"},"modified":"2026-03-28T12:22:04","modified_gmt":"2026-03-28T04:22:04","slug":"docker-compose%e9%83%a8%e7%bd%b2lnmp%e7%8e%af%e5%a2%83%e5%ae%8c%e6%95%b4%e6%95%99%e7%a8%8b%ef%bc%9a%e4%b8%80%e9%94%ae%e5%90%af%e5%8a%a8nginxmysqlphp","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/442546\/","title":{"rendered":"Docker Compose\u90e8\u7f72LNMP\u73af\u5883\u5b8c\u6574\u6559\u7a0b\uff1a\u4e00\u952e\u542f\u52a8Nginx+MySQL+PHP"},"content":{"rendered":"<h2>\u4e3a\u4ec0\u4e48\u7528 Docker Compose \u90e8\u7f72 LNMP\uff1f<\/h2>\n<p>\u4f20\u7edf\u65b9\u5f0f\u5728\u670d\u52a1\u5668\u4e0a\u5b89\u88c5 Nginx\u3001MySQL\u3001PHP \u9700\u8981\u5904\u7406\u5927\u91cf\u4f9d\u8d56\u51b2\u7a81\u548c\u7248\u672c\u95ee\u9898\uff0c\u8fc1\u79fb\u65f6\u66f4\u662f\u9ebb\u70e6\u3002Docker Compose \u5c06\u6574\u4e2a LNMP \u73af\u5883\u6253\u5305\u6210\u51e0\u4e2a\u5bb9\u5668\uff0c<strong>\u4e00\u6761\u547d\u4ee4\u542f\u52a8\uff0c\u4e00\u6761\u547d\u4ee4\u9500\u6bc1\uff0c\u8fc1\u79fb\u53ea\u9700\u590d\u5236\u914d\u7f6e\u6587\u4ef6<\/strong>\uff0c\u6781\u5927\u964d\u4f4e\u8fd0\u7ef4\u590d\u6742\u5ea6\u3002<\/p>\n<hr \/>\n<h2>\u4e00\u3001\u73af\u5883\u51c6\u5907<\/h2>\n<pre><code># \u5b89\u88c5 Docker\r\ncurl -fsSL https:\/\/get.docker.com | bash\r\n\r\n# \u5b89\u88c5 Docker Compose\r\nsudo apt install docker-compose-plugin -y\r\n\r\n# \u9a8c\u8bc1\u5b89\u88c5\r\ndocker --version\r\ndocker compose version<\/code><\/pre>\n<hr \/>\n<h2>\u4e8c\u3001\u76ee\u5f55\u7ed3\u6784\u89c4\u5212<\/h2>\n<pre><code>mkdir -p ~\/lnmp\/{nginx\/conf.d,php,mysql\/data,www}\r\ncd ~\/lnmp<\/code><\/pre>\n<p>\u76ee\u5f55\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li><code>nginx\/conf.d\/<\/code>\uff1aNginx \u7ad9\u70b9\u914d\u7f6e\u6587\u4ef6<\/li>\n<li><code>php\/<\/code>\uff1aPHP \u81ea\u5b9a\u4e49\u914d\u7f6e<\/li>\n<li><code>mysql\/data\/<\/code>\uff1aMySQL \u6570\u636e\u6301\u4e45\u5316\u76ee\u5f55<\/li>\n<li><code>www\/<\/code>\uff1a\u7f51\u7ad9\u6839\u76ee\u5f55<\/li>\n<\/ul>\n<hr \/>\n<h2>\u4e09\u3001\u7f16\u5199 docker-compose.yml<\/h2>\n<pre><code>nano docker-compose.yml<\/code><\/pre>\n<pre><code>version: '3.8'\r\n\r\nservices:\r\n  nginx:\r\n    image: nginx:1.25-alpine\r\n    container_name: lnmp_nginx\r\n    ports:\r\n      - \"80:80\"\r\n      - \"443:443\"\r\n    volumes:\r\n      - .\/nginx\/conf.d:\/etc\/nginx\/conf.d\r\n      - .\/www:\/var\/www\/html\r\n      - .\/nginx\/logs:\/var\/log\/nginx\r\n    depends_on:\r\n      - php\r\n    networks:\r\n      - lnmp_net\r\n    restart: unless-stopped\r\n\r\n  php:\r\n    image: php:8.2-fpm-alpine\r\n    container_name: lnmp_php\r\n    volumes:\r\n      - .\/www:\/var\/www\/html\r\n      - .\/php\/php.ini:\/usr\/local\/etc\/php\/conf.d\/custom.ini\r\n    networks:\r\n      - lnmp_net\r\n    restart: unless-stopped\r\n\r\n  mysql:\r\n    image: mysql:8.0\r\n    container_name: lnmp_mysql\r\n    environment:\r\n      MYSQL_ROOT_PASSWORD: your_root_password\r\n      MYSQL_DATABASE: mydb\r\n      MYSQL_USER: myuser\r\n      MYSQL_PASSWORD: your_db_password\r\n    volumes:\r\n      - .\/mysql\/data:\/var\/lib\/mysql\r\n    ports:\r\n      - \"127.0.0.1:3306:3306\"\r\n    networks:\r\n      - lnmp_net\r\n    restart: unless-stopped\r\n\r\nnetworks:\r\n  lnmp_net:\r\n    driver: bridge<\/code><\/pre>\n<p><strong>\u5b89\u5168\u6ce8\u610f\uff1a<\/strong>MySQL \u7aef\u53e3\u53ea\u7ed1\u5b9a\u5230 127.0.0.1\uff0c\u4e0d\u5bf9\u5916\u7f51\u66b4\u9732\u3002<\/p>\n<hr \/>\n<h2>\u56db\u3001\u914d\u7f6e Nginx \u7ad9\u70b9<\/h2>\n<pre><code>nano nginx\/conf.d\/default.conf<\/code><\/pre>\n<pre><code>server {\r\n    listen 80;\r\n    server_name your-domain.com;\r\n    root \/var\/www\/html;\r\n    index index.php index.html;\r\n\r\n    location \/ {\r\n        try_files $uri $uri\/ \/index.php?$args;\r\n    }\r\n\r\n    location ~ \\.php$ {\r\n        fastcgi_pass php:9000;\r\n        fastcgi_index index.php;\r\n        include fastcgi_params;\r\n        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\r\n    }\r\n\r\n    location ~ \/\\.ht {\r\n        deny all;\r\n    }\r\n}<\/code><\/pre>\n<hr \/>\n<h2>\u4e94\u3001\u914d\u7f6e PHP<\/h2>\n<pre><code>nano php\/php.ini<\/code><\/pre>\n<pre><code>upload_max_filesize = 64M\r\npost_max_size = 64M\r\nmax_execution_time = 300\r\nmemory_limit = 256M\r\ndate.timezone = Asia\/Shanghai<\/code><\/pre>\n<p>PHP \u955c\u50cf\u9ed8\u8ba4\u4e0d\u5305\u542b\u5e38\u7528\u6269\u5c55\uff0c\u9700\u8981\u5b89\u88c5 mysqli \u548c pdo_mysql\uff1a<\/p>\n<pre><code># \u8fdb\u5165 PHP \u5bb9\u5668\u5b89\u88c5\u6269\u5c55\r\ndocker compose exec php sh -c \"docker-php-ext-install mysqli pdo_mysql &amp;&amp; docker-php-ext-enable mysqli\"\r\ndocker compose restart php<\/code><\/pre>\n<hr \/>\n<h2>\u516d\u3001\u542f\u52a8\u5e76\u9a8c\u8bc1<\/h2>\n<pre><code># \u542f\u52a8\u6240\u6709\u670d\u52a1\r\ndocker compose up -d\r\n\r\n# \u67e5\u770b\u8fd0\u884c\u72b6\u6001\r\ndocker compose ps\r\n\r\n# \u67e5\u770b\u65e5\u5fd7\r\ndocker compose logs -f nginx<\/code><\/pre>\n<p>\u521b\u5efa\u6d4b\u8bd5\u6587\u4ef6\u9a8c\u8bc1 PHP \u8fd0\u884c\uff1a<\/p>\n<pre><code>echo \"<!--?php phpinfo(); ?-->\" &gt; www\/info.php<\/code><\/pre>\n<p>\u6d4f\u89c8\u5668\u8bbf\u95ee <code>http:\/\/\u670d\u52a1\u5668IP\/info.php<\/code>\uff0c\u770b\u5230 PHP \u4fe1\u606f\u9875\u9762\u8bf4\u660e LNMP \u73af\u5883\u8fd0\u884c\u6b63\u5e38\u3002<\/p>\n<hr \/>\n<h2>\u4e03\u3001\u5e38\u7528\u7ba1\u7406\u547d\u4ee4<\/h2>\n<pre><code># \u505c\u6b62\u6240\u6709\u5bb9\u5668\r\ndocker compose down\r\n\r\n# \u91cd\u542f\u5355\u4e2a\u670d\u52a1\r\ndocker compose restart nginx\r\n\r\n# \u8fdb\u5165\u5bb9\u5668\u5185\u90e8\r\ndocker compose exec mysql mysql -u root -p\r\n\r\n# \u66f4\u65b0\u955c\u50cf\u5e76\u91cd\u542f\r\ndocker compose pull &amp;&amp; docker compose up -d<\/code><\/pre>\n<hr \/>\n<h2>\u603b\u7ed3<\/h2>\n<p>Docker Compose \u90e8\u7f72 LNMP \u7684\u6838\u5fc3\u4f18\u52bf\u662f\uff1a\u73af\u5883\u9694\u79bb\u3001\u914d\u7f6e\u7248\u672c\u5316\u3001\u4e00\u952e\u8fc1\u79fb\u3002\u6574\u4e2a\u90e8\u7f72\u6d41\u7a0b\u7ea6 10 \u5206\u949f\uff0c\u9002\u5408\u5728\u9999\u6e2f VPS \u4e0a\u5feb\u901f\u642d\u5efa Web \u73af\u5883\uff0c\u4e5f\u9002\u5408\u9700\u8981\u540c\u65f6\u7ef4\u62a4\u591a\u4e2a\u9879\u76ee\u7684\u5f00\u53d1\u8005\u3002<\/p>\n<p>\u5982\u9700\u8fd0\u884c\u672c\u6587\u6559\u7a0b\uff0c<a title=\"\u9999\u6e2f\u4e91\u670d\u52a1\u5668\" href=\"https:\/\/idc.net\/cloud-hk\/\">IDC.Net \u9999\u6e2f\u4e91\u670d\u52a1\u5668<\/a>\u9996\u6708 10 \u5143\u8d77\uff0c\u652f\u6301 Ubuntu 22.04\uff0c\u81ea\u52a9\u9762\u677f\u968f\u65f6\u91cd\u88c5\uff0cCN2 GIA \u76f4\u8fde\u5927\u9646\uff0c\u652f\u4ed8\u5b9d\u4ed8\u6b3e\u5373\u53ef\u5f00\u901a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4ec0\u4e48\u7528 Docker Compose \u90e8\u7f72 LNMP\uff1f \u4f20\u7edf\u65b9\u5f0f\u5728\u670d\u52a1\u5668\u4e0a\u5b89\u88c5 Nginx\u3001MySQL\u3001PH [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,34],"tags":[],"class_list":["post-442546","post","type-post","status-publish","format-standard","hentry","category-nginx","category-linux"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/442546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=442546"}],"version-history":[{"count":1,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/442546\/revisions"}],"predecessor-version":[{"id":442547,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/442546\/revisions\/442547"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=442546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=442546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=442546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}