search
HomeBackend DevelopmentPHP TutorialUse docker to create an integrated service lnmp environment

Use docker to create an integrated service lnmp environment

Apr 09, 2018 pm 03:14 PM
dockerServeintegrated

This article mainly introduces the use of docker to create an integrated service lnmp environment. Now I share it with everyone and provide a reference for friends in need.

After mastering the basic commands of docker, I also wanted to use docker to create some actual supporting environments, so I used my most commonly used lnmp environment for testing. The order of running the supporting environment is mysql->php->nginx. The reason why will be explained below.

1.MySQL

Unless there are special instructions, the images for running services are all pulled from the official images. For small businesses and individual developers, the official The mirror is safer and saves worry and effort.

# 拉取镜像
$ docker pull mysql
# 运行MySQL
$ docker run MySQL --name mysql -d \
    -p 3306:3306 \
    -v /var/lib/mysql/:/var/lib/mysql/ \
    -e MYSQL_ROOT_PASSWORD=ilovec \

The following will explain each of the above running parameters in turn

  1. --name: the specified running container The name

  2. -d: Run the container in the background

  3. -p: The mapping of the host and container ports

  4. -v: Mount the container to the local directory mapping

  5. -e: Specify the environment variable for running the container

2.PHP

Pull the official image php-fpm and download it according to the PHP version you need. However, some commonly used PHP packages are not included in the official image, so we need to use Rebuild the dockerfile. The following is to install the two PHP extension packages mysqli and pdo in the Dockerfile.

FROM php:7.1-fpm
# Install modules
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
RUN docker-php-ext-install pdo_mysql
CMD ["php-fpm"]

Run php-fpm

docker run -d -p 9000:9000 \
  --name php-fpm \
  --link mysql \
  -v /data/wwwroot/:/data/wwwroot/ \
  php-fpm

Note the parameters The --link parameter is very useful for connecting between containers. It will add a domain name resolution in /etc/hosts of the current container. Through this domain name, you can connect to the corresponding container, such as the above-mentioned php-fpm Inside, link to mysql, then the php program in php-fpm can connect to the just-running mysql container through the mysql string, and you can see the parsing records inside by cat /etc/hosts.

172.17.0.2 mysql b41d2569c06d

3.Nginx

Run nginx through the following command, because nginx needs to pass 127.0.0.1 :9000 port to connect to php-fpm to parse php files, so you need to connect to php-fpm through link.

docker run -d -p 80:80 \
  --name nginx \
  --link php-fpm \
  -v /data/wwwroot/:/data/wwwroot/ \
  nginx

It is worth noting that when nginx parses a file, if a static file is requested, the file in the nginx container will be directly returned to On the client side, if the request is for a php file, he will forward the request to php-fpm, and then php-fpm will go locally to find the php file for parsing, which is the file of the php-fpm container itself.

After running the above three service startup commands in sequence, we can build our common lnmp. However, it is a bit troublesome to run the above command every time you run the service. We can use the docker-compose command for centralized management.

To use docker-compose

you only need to create a lnmp directory, and then create docker-compose.yml in the lnmp directory and enter the following command to manage the integrated environment .
In fact, you can easily know the meaning of each command through the name of the command.

version: Since docker-compose is a developing tool, it is very likely that the instructions for each version will be different, so the applicable version of the docker-compose instructions needs to be declared at the beginning.

image: refers to the image through which the service runs.

depends_on: This specifies which software the software depends on. In fact, it also declares the order in which the software runs.

version: '2'

services:
  mysql:
    image: "mysql"
    ports:
      - "3306:3306"
    volumes:
      - /var/lib/mysql/:/var/lib/mysql/
    environment:
      MYSQL_ROOT_PASSWORD: password
      
  php-fpm:
    image: "php-fpm"
    depends_on:
      - mysql
    links:
      - mysql
    ports:
      - "9000:9000"
    volumes:
      - /data/wwwroot/:/data/wwwroot/

  nginx:
    image: "nginx"
    depends_on:
      - php-fpm
    links:
      - php-fpm
    volumes:
      - /data/wwwroot/:/data/wwwroot/
    ports:
      - "80:80"

After that, execute compose related commands in this lnmp directory to control it.

# 运行docker-compose服务
$ docker-compose up -d
# 停止服务
$ docker-compose stop
# 删除该服务相关的容器
$ docker-compose rm
# 运行已存在docker-compose的服务
$ docker-compose start

Related recommendations:

Building a PHP development environment in Docker


The above is the detailed content of Use docker to create an integrated service lnmp environment. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

How do you retrieve data from a PHP session?How do you retrieve data from a PHP session?May 01, 2025 am 12:11 AM

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

How can you use sessions to implement a shopping cart?How can you use sessions to implement a shopping cart?May 01, 2025 am 12:10 AM

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),