On the server, docker can help us quickly set up the lnmp running environment, and any operation in the container has no effect on the host.This paper uses docker to create multiple containers (mysql,php-fpm,nginx) to set up lnmp running environment.
Since the three containers are for communication, containers are created in the order mysql,php-fpm,nginx.
step
1. Pull mirror
//mysql mirror, version 5.7 selected here docker pull image mysql:5.7 //php-fpm mirror, version 7.3 used here doker pull php:7.3-fpm //nginx mirror, using version 1.17.5 here docker pull nginx:1.17.5 //View all mirrors [root@hzj ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 d5cea958d330 4 days ago 440MB php 7.3-fpm c926fc177576 4 days ago 398MB redis latest 44d36d2c2374 3 weeks ago 98.2MB nginx 1.17.5 540a289bab6c 4 months ago 126MB
2. Create mysql container
sudo docker run \ --name mysql5.7 \ --restart always \ --privileged=true \ -p 4306:3306 \ -v /opt/mysql/config/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf \ -v /var/mysql/data:/var/lib/mysql \ -e MYSQL_USER="user_name" \ -e MYSQL_PASSWORD="user_password" \ -e MYSQL_ROOT_PASSWORD="root_password" \ -d mysql:5.7
Parameter description:
- - name: Specify the container name
- - restart always: turn on startup
- - privileged=tree: Give container operation privileges
- -p:4306:3306:port 336 of host 4306 port mapping container
- -v dir1:dir2: host dir1 maps to container dir2 directory
- -e MYSQL_USER:Set User Name
- -e MYSQL_PASSWORD:Set Password
- -e MYSQL_ROOT_PASSWORD: Set root user password
- -d:Background running
Be careful
- mysql's data must be mounted locally to prevent data loss after container deletion.
- my.cnf configuration files can also be mounted locally.
3. Create php-fpm container
sudo docker run \ --name php-fpm \ --link=mysql:mysql \ -d -p 9000:9000 \ -v /home/wwwroot:/home/wwwroot \ -v /home/wwwlogs:/home/wwwlogs \ -v /tmp:/tmp \ -v /etc/localtime:/etc/localtime\ --privileged=true \ php:7.3-fpm
Parameter description:
- - link=mysql:mysql2:Connect the MySQL container with the alias mysql2 inside
- -v/home/wwwroot:/home/wwwroot: Code file mapped to container
- -v/etc/localtime:/etc/localtime:container and host time synchronization
4. Create ngixn container
sudo docker run --name nginx \ --link=mysql:mysql \ --link=php:phpfpm \ -d -p 80:80 \ -v /home/wwwroot:/home/wwwroot \ -v /tmp:/tmp \ -v /var/log/nginx:/var/log/nginx \ nginx:1.17.5
Modify Profile
Since nginx and php-fpm are not in the same container here, the site configuration file needs to be modified to forward requests to the php-fpm container.
Major modifications:
server_name test.com;//domain name location / { root /home/wwwroot;//php code store directory index index.php index.html index.htm;//First page file index.php #try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { root html; fastcgi_pass php:9000;//Forward a request using either the php-fpm container name or an alias. The ports are consistent or the request cannot be processed. fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/wwwroot/$fastcgi_script_name;//Previously written code storage directory in php-fpm container include fastcgi_params; }
- After modifying the configuration file, use the service nginx configtest command to check for syntax errors.Once confirmed, reload the configuration file using service nginx reload
At this point, the php-fpm environment has been set up.