Docker installation Lnmp environment
Date: November 5, 2021
Original link: https://www.cnblogs.com/sochishun/p/15532133.html
One stop rapid installation process
If you encounter any problems, pull directly to the detailed explanation section at the back of the article to find the answer.
Step 1: check the host environment
Check Docker service status
docker version | docker -v | docker info | docker help # View docker version information, etc docker-compose -v systemctl start docker # Start docker service
Create Docker data directory
Create a dockerdata unified data directory in the root directory
mkdir -p /dockerdata/etc/{nginx/conf,php-fpm,mysql} mkdir -p /dockerdata/var/log/{nginx,php-fpm,mysql} mkdir -p /dockerdata/var/lib/mysql mkdir -p /dockerdata/www/{html,backup} mkdir -p /dockerdata/temp/
Step 2: install Nginx
Pull the image and create the container
Pull image
docker pull nginx # Pull image
Create container
docker run --name nginx80 -p 80:80 -d nginx # Run container (test) docker exec -it nginx80 /bin/bash # Enter container management
Directory structure within the Nginx container
Main directory structure in nginx container
/usr/share/nginx/html/ # Site root /etc/nginx/nginx.conf # nginx configuration file /etc/nginx/conf.d/default.conf # nginx site profile directory /var/log/nginx/ # Log file directory
Unified mapping directory corresponding to the host
mkdir -p /dockerdata/www/html # Create site directory mkdir -p /dockerdata/var/log/nginx/ # Create log directory mkdir -p /dockerdata/etc/nginx/conf/ # Create nginx configuration directory
Directory mapping for the Nginx container
The directory inside the container is mapped to the host directory outside the container
docker run --name nginx80 -p 80:80 -v /dockerdata/www/html:/usr/share/nginx/html -d nginx # Run the container and bind the directory
Full mapping command
docker run --name nginx80 -p 80:80 \ -v /dockerdata/etc/nginx/nginx.conf:/etc/nginx/nginx.conf \ -v /dockerdata/etc/nginx/conf.d:/etc/nginx/conf.d \ -v /dockerdata/www/html:/usr/share/nginx/html \ -v /dockerdata/var/log/nginx:/var/log/nginx \ -d nginx
test result
echo "hello nginx" > /dockerdata/www/html/index.html # Create index.html test file curl http://localhost:80/index.html # test results
Step 3: install PHP FPM
Pull the image and create the container
Pull image
# docker pull php # By default, the latest php does not contain fpm. docker pull php:8.0.12-fpm # PHP version number https://www.php.net/ see
Create container
docker run --name php9000 -p 9000:9000 -v /dockerdata/www/html:/var/www/html -d php:8.0.12-fpm docker exec -it php9000 bash
Directory structure within PHP container
Main directory structure in PHP container
/var/www/html/ # Site root /usr/local/var/log/ # Log directory /usr/local/etc/ # PHP FPM configuration file directory
Unified mapping directory corresponding to the host
mkdir -p /dockerdata/www/{html,backup} # Create site directory mkdir -p /dockerdata/var/log/php-fpm/ # Create log directory mkdir -p /dockerdata/etc/php-fpm/ # Create PHP FPM configuration directory
Directory mapping for PHP containers
The directory inside the container is mapped to the host directory outside the container
docker run --name php9000 -p 9000:9000 -v /dockerdata/www/html:/var/www/html -d php:8.0.12-fpm # Run the container and bind the directory
Full mapping command
docker run --name php9000 -p 9000:9000 \ -v /dockerdata/www/html:/var/www/html \ -v /dockerdata/var/log/php-fpm:/usr/local/var/log \ -v /dockerdata/etc/php-fpm:/usr/local/etc \ -d php:8.0.12-fpm # Run the container and bind the directory
test result
echo "<?php echo 'hello php';" > /dockerdata/www/html/index.php # Create the index.php test file docker exec php9000 bash -c "php index.php" # test result
Step 4: configure Nginx to support PHP scripts
View the IP address of the Nginx container
View the container intranet IP automatically assigned by Docker
docker inspect php9000 # The IP address found is 172.17.0.3 docker inspect --format='{{.NetworkSettings.IPAddress}}' php9000
Enter the Nginx container to modify the configuration file
Enter Nginx container
docker exec -it nginx80 bash # Container into interactive mode nginx -t # View the directory of the configuration file (the result is / etc/nginx/nginx.conf)
Modify the contents of the Nginx configuration file
cd /etc/nginx/conf.d/ cp default.conf default.conf.bak # Back up vi default.conf # Open default profile
Modifying the Nginx web site profile
# index.php is added to the default homepage location / { index index.html index.htm index.php; } # Uncomment the php configuration and modify the IP and directory path. # The IP can be the external IP of the host or the IP of the Docker container. Here, the IP of the Docker container is used. Note: the intranet IP assigned by Docker may change every time the Docker service is restarted. Therefore, it is recommended to use external IP. # The path uses the mapping path configured when the php container starts. location ~ \.php$ { root html; fastcgi_pass 172.17.0.3:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; include fastcgi_params; }
Restart the Nginx service
nginx -s reload
test result
curl http://localhost/index.php
Step 5: install mysql
Pull the image and create the container
Pull image
docker pull mysql
Create container
docker run --name mysql3306 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=iPwd000000 -d mysql
Parameter description
-e MYSQL_ROOT_PASSWORD: Set the environment variables in the container, root The password of the account is iPwd000000
Directory structure in MySQL container
Main directory structure in MySQL container
/usr/sbin/mysqld # mysql startup file /var/run/mysqld/mysqld.sock # mysqld.sock file /etc/mysql/conf.d/ # mysql configuration file directory /var/log/ # mysql log file directory /var/lib/mysql/ # mysql data file directory
Unified mapping directory corresponding to the host
mkdir -p /dockerdata/var/{log/mysql,lib/mysql} # Create log directory and data directory mkdir -p /dockerdata/etc/mysql/ # Create mysql configuration directory
Directory mapping for MySQL container
The directory inside the container is mapped to the host directory outside the container
docker run --name mysql3306 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=iPwd000000 -d mysql
Full mapping command
docker run --name mysql3306 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=iPwd000000 \ -v /dockerdata/etc/mysql:/etc/mysql/conf.d \ -v /dockerdata/var/log/mysql:/var/log \ -v /dockerdata/var/lib/mysql:/var/lib/mysql \ -d mysql # Run the container and bind the directory
Enter the container to log in to MySQL
docker exec -it mysql3306 bash mysql -h localhost -u root -p
test result
show databases; select `User`, `Host`, SUBSTRING(authentication_string, 1, 20) as `Password` from mysql.user;
Step 6: LNMP comprehensive test
Create the test.php file
vi /dockerdata/www/html/test.php 100dd # Delete the first 100 lines (empty the file contents)
Copy and paste the following code into the test.php file
<?php // Note: the IP here should use the external network IP, not 127.0.0.1. Remember to open the 3306 port of the host. $dsn = 'mysql:host=1xx.3x.2xx.2xx;port=3306'; $dbuser = 'root'; $dbpwd = 'iPwd000000'; $pdo = null; echo 'Before connecting...', PHP_EOL; try { $pdo = new PDO($dsn, $dbuser, $dbpwd); } catch (\PDOException $ex) { echo 'Connect Fail: ', $ex->getMessage(), PHP_EOL; exit; } try { $list = $pdo->query('select `User`, `Host`, SUBSTRING(authentication_string, 1, 20) as `Password` from mysql.user;', PDO::FETCH_ASSOC); foreach ($list as $row) { echo $row['User'], ' | ', $row['Host'], ' | ', $row['Password'], PHP_EOL; } } catch (\PDOException $ex) { echo 'SQL ERROR: ', $ex->getMessage(), PHP_EOL; } $pdo = null;
Test result failed
curl http://localhost/test.php
Prompt that the database connection fails (no driver is found) because PHP MySQL extension needs to be installed.
docker exec -it php9000 bash # Enter php container php -m # Check the installed php extensions (is it found that there is no pdo#u MySQL) ls /usr/local/bin # View the installation and configuration tools of the system in the container # docker-php-ext-install -j$(nproc) bcmath calendar exif gettext sockets dba mysqli pcntl pdo_mysql shmop sysvmsg sysvsem sysvshm # Installation of official extensions docker-php-ext-install -j$(nproc) pdo_mysql docker restart php9000 # Restart the container, re-enter the container and execute php-m, and you can see the newly installed extension name.
The test result is successful
curl http://localhost/test.php
Reference materials
- [Docker Hub] https://registry.hub.docker.com/
- [Docker command] https://www.runoob.com/docker/docker-command-manual.html
- [alicloud mirror] https://developer.aliyun.com/mirror/
- [building LNMP environment based on Docker] https://www.cnblogs.com/hovin/p/15009420.html
- [docker set container fixed ip] https://blog.csdn.net/wanghao_0206/article/details/79583325
- [Docker installs PHP extensions] https://www.jianshu.com/p/eff03dc28d21
Expand knowledge
Linux general commands
cat /etc/os-release | cat /etc/debian_version # View release information lsof -i:80 # Check whether port 80 is occupied curl http://localhost:80 # access the web address of the local port 80 wget http://localhost:80 # download the resources of the local port 80
Docker common commands
Mirror management commands
docker images # View local mirror docker search nginx # Find mirror docker pull nginx # Pull image to local docker rmi nginx # Remove mirror docker run --name hi_nginx -p 80:80 nginx # Run mirror to container
Container management commands
docker ps # List all containers docker run --name hi_nginx -p 80:80 nginx docker rename hi_nginx my_nginx # Rename container docker stop my_nginx # Stop container docker start my_nginx # Start container docker restart my_nginx # Restart container docker rm my_nginx # Remove container docker exec -it my_nginx /bin/bash # Enter the container (- it keyword must follow exec)
Mirror run command
docker run --name my_nginx -p 81:80 -v /dockerdata/www/html:/usr/share/nginx/html -d nginx docker run --name my_php -p 9000:9000 -v /dockerdata/www/html:/usr/share/nginx/html -itd php:8.0.12-fpm
Parameter details:
--name: Specify a name for the container -p: Add host to container port mapping (host port):Container port) -v: Add host to container directory mapping (host directory):Container directory) (Host Directory needs to be created manually in advance) -i: Run the container in interactive mode, usually with -t Simultaneous use -t: Reassign an input pseudo terminal to the container, usually with -i Simultaneous use -d: Run the container in the background and return to the container ID(Resident (background operation) -e: Setting environment variables -m: Sets the maximum amount of memory used by the container --link: Add a link to another container (so that the service in another container can be used in the current container) --expose: Open a port or group of ports ro: Read-only rights
Configure acceleration source in container
# sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list # ubuntu mirror source sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list # debian image source apt-get clean apt-get update
Install management software in container
apt-get update # You must update before installing other software. Remember to set the domestic image source, otherwise it will be very slow apt-get install -y vim # Install vim apt-get install -y procps # Install ps apt-get install -y net-tools # Install netstat ps -ef | grep "nginx" # View nginx process status and PID by name ps aux | grep "nginx" | greo -v grep # Check whether the nginx process is started according to the name netstat -anop | grep 0.0.0.0:80 # View PID by port
View the start path of the process
ps -ef | grep "nginx" # View the PID of nginx process by name (the second column, the result is 1) netstat -anop | grep 0.0.0.0:80 # Or view the PID according to the port (the result is 1/nginx) ls -l /proc/1/exe # List the exe file information of the process (the result is / usr/sbin/nginx)
Nginx container management command
Nginx common commands
nginx -h # View the default log file and configuration file path, etc nginx -s reload # Reload the configuration file (gracefully restart the nginx service) nginx -s quit # Stop the service after all requests are processed (gracefully stop the nginx service) nginx -v # display version information nginx -V # Displays all configuration information at the time of installation nginx -t # Detect whether the configuration file has syntax errors (you can view the configuration file directory) # Two ways to view the profile directory nginx -t ps -ef | grep "nginx" nginx -V
Nginx configuration management
nginx -h # View the directory of the configuration file vi /etc/nginx/conf.d/default.conf # View default access directory cd /usr/share/nginx/html
Dockerdata unified data directory
Docker container to host data mapping directory.
directory structure
/dockerdata/ - etc/ - nginx/ - nginx.conf # nginx.conf configuration file - conf.d/ # nginx website configuration directory - php-fpm/ - php-fpm.conf # PHP FPM configuration file - mysql - mysql.conf # mysql configuration file - var/ - log/ - nginx/ # nginx log directory - php-fpm/ # PHP FPM log directory - mysql/ # mysql log directory - lib/ - mysql/ # mysql database datadir directory - www/ - html/ - backup/ - temp/ - gitdata/
shell script for batch directory creation
mkdir -p /dockerdata/etc/{nginx/conf,php-fpm,mysql} mkdir -p /dockerdata/var/log/{nginx,php-fpm,mysql} mkdir -p /dockerdata/var/lib/mysql mkdir -p /dockerdata/www/{html,backup} mkdir -p /dockerdata/temp/