


Tutorial on deploying LNMP working environment under Docker (detailed steps)
This article brings you a tutorial (detailed steps) on deploying LNMP working environment under Docker. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Ordinary PC environment can also use the following configuration, just replace MYSQL with the official version of mysql.
Docker installation
$ curl -sSL get.docker.com | sudo sh
Working directory
/lnmp/conf stores virtual host configuration
/lnmp/data /mysql Stores mysql database data
/lnmp/log Stores nginx and mysql logs
/lnmp/workspace Stores various project files
/lnmp/.env Environment variable configuration when starting docker
/lnmp/docker-compose.yml Build configuration file
/lnmp/php/Dockerfile Compile PHP configuration file
Build Nginx
Create project
First create a static file index.html
/lnmp/workspace/default/index.html
nbsp;html> <meta> <meta> <meta> <title>Document</title> Hello, Docker!
Add environment variables during build
Edit .env file
SOURCE_DIR=./workspace HTTP_HOST=80 NGINX_VERSION=alpine NGINX_CONFD_DIR=./conf/conf.d NGINX_CONF_FILE=./conf/nginx.conf NGINX_LOG_DIR=./log SOURCE_DIR=./workspace workspace文件夹挂载到nginx下的 /var/www/html HTTP_HOST=80 宿主机的80端口映射到nginx下的80端口 NGINX_VERSION=alpine nginx镜像使用alpine版本 NGINX_CONFD_DIR=./conf/conf.d conf.d文件夹挂载到nginx下的/etc/nginx/conf.d NGINX_CONF_FILE=./conf/nginx.conf nginx.conf挂载nginx的 /etc/nginx/nginx.conf NGINX_LOG_DIR=./log
Nginx.conf
Create /lnmp/conf/nginx.conf file
user nginx; worker_processes 1; pid /var/run/nginx.pid; error_log /var/log/nginx/nginx.error.log warn; 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/nginx.access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
Configure virtual host
Create /lnmp/conf/conf.d/default.conf file
Note: The path under root must start with /var/www/html, default is our local A folder under the workspace
server { listen 80; server_name localhost default-server; root /var/www/html/default; index index.php index.html index.htm; access_log /var/log/nginx/nginx.localhost.access.log main; error_log /var/log/nginx/nginx.localhost.error.log warn; #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; } }
Build Nginx configuration
Edit docker-composer.yml
The following ${} are all configured in the above .env file The variable
version: "3" services: nginx: image: nginx:${NGINX_VERSION} ports: - ${HTTP_HOST}:80 volumes: - ${SOURCE_DIR}:/var/www/html/:rw - ${NGINX_CONFD_DIR}:/etc/nginx/conf.d/:rw - ${NGINX_CONF_FILE}:/etc/nginx/nginx.conf:ro - ${NGINX_LOG_DIR}:/var/log/nginx/:rw restart: always
Start Nginx
After running the command, access the IP of the Raspberry Pi. Normally, Hello, Docker!
$ docker-compose up
# will be displayed. ##Build PHP
Edit /lnmp/.env to add PHP build variablesPHP_CONFIG_FILE=./conf/php.ini PHP_FPM_CONFIG_FILE=./conf/php-fpm.confBecause the php.ini configuration is a bit long, you can copy it at the following address
php.ini
php-fpm.conf
Add Dockerfile
/lnmp/php/DockerfileARG PHP_VERSION FROM php:${PHP_VERSION}-fpm RUN apt update RUN apt-get install -y \ libfreetype6-dev \ libjpeg62-turbo-dev \ libmcrypt-dev \ libpng-dev \ unixodbc-dev \ gcc g++ make autoconf libc-dev pkg-config
Add build PHP
Edit /lnmp/docker-compose.yml fileversion: "3" services: nginx: image: nginx:${NGINX_VERSION} ports: - ${HTTP_HOST}:80 volumes: - ${SOURCE_DIR}:/var/www/html/:rw - ${NGINX_CONFD_DIR}:/etc/nginx/conf.d/:rw - ${NGINX_CONF_FILE}:/etc/nginx/nginx.conf:ro - ${NGINX_LOG_DIR}:/var/log/nginx/:rw restart: always php: build: context: ./php args: PHP_VERSION: 7.2 volumes: - ${SOURCE_DIR}:/var/www/html/:rw - ${PHP_PHP_CONFIG_FILE}:/usr/local/etc/php/php.ini:ro - ${PHP_FPM_CONFIG_FILE}:/usr/local/etc/php-fpm.d/www.conf:rw cap_add: - SYS_PTRACE restart: always networks: - default
Virtual host supports PHP parsing
/lnmp/conf/conf.d/default. confThe php of fastcgi_pass php:9000 below refers to a service name in docker-compose.yml above, which can be used to specify the PHP version for a certain project
#...其它代码 location ~ \.php$ { fastcgi_pass php:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }
Add files
/lnmp/workspace/default/index.php<?php echo ‘Hello, PHP!’;
Start preview
If you have not terminated the process before, you can terminate it with ctrl c Run the following command.If nothing unexpected happens, the IP when accessing the Raspberry Pi will display Hello, PHP!.
$ docker-compose up
Add PHP extension
/lnmp/.env Add variables, The following is a swoole extension demonstration. For other extensions, please refer to the following operations.PHP_SWOOLE_VERSION=4.2.10/lnmp/docker-compose.yml Add variables
php: build: context: ./php args: PHP_VERSION: 7.2 PHP_SWOOLE: ${PHP_SWOOLE_VERSION}/lnmp/php/Dockerfile Download and compile swoole
# ...其它代码 ARG PHP_SWOOLE=false RUN if [ ${PHP_SWOOLE} != false ]; then \ curl -O http://pecl.php.net/get/swoole-${PHP_SWOOLE}.tgz -L \ && pecl install swoole-${PHP_SWOOLE}.tgz \ && docker-php-ext-enable swoole \ ;fiRecompileNote: If the Dockerfile is changed, you must add --build and recompile it once, otherwise it will not take effect when docker-compose up
$ docker-compose up --buildPreviewEdit /lnmp/workspace/default/index.php to save, access the IP of the Raspberry Pi, and as expected you can find the swoole extension
<?php phpinfo();
Build MYSQL
MYSQL persistenceThe Docker container service has been deleted to ensure that data will not be lostCreate the /lnmp/data/mysql folder
MYSQL_HOST_PORT=3306 MYSQL_PASSWORD=1234 MYSQL_DATA_DIR=./data/mysqlBuild configuration
version: "3"
services:
#... 其它代码
mysql:
image: tobi312/rpi-mysql
ports:
- ${MYSQL_HOST_PORT}:3306
volumes:
- ${MYSQL_DATA_DIR}:/var/lib/mysql:rw
environment:
MYSQL_ROOT_PASSWORD: "${MYSQL_PASSWORD}"
StartupAfter starting, use the data connection tool to test, link address: Raspberry Pi IP Port: 3306 Username: root Password: 1234$ docker-compose upSo far, your own LNMP working environment is ready.
The above is the detailed content of Tutorial on deploying LNMP working environment under Docker (detailed steps). For more information, please follow other related articles on the PHP Chinese website!

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
