Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Separate deployment of LNMP Architecture based on Docker Container

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

First, environment preparation: a centos 7.5 server, running docker services; for the installation of docker services, refer to the blog article: detailed installation configuration of Docker. Each container IP:192.168.1.2 (Nginx), 192.168.1.3 (PHP), 192.168.1.4 (MySQL).

Considerations for building LNMP:

The data of each container is persistent; assign a fixed IP address to the container to prevent the IP address from changing after the container is rebuilt, which only adds unnecessary trouble; because the client only needs to access port 80 of Nginx, and then calls PHP to connect to the database through Nginx, you only need to map port 80 of Nginx to the host using the "- p" option. Other containers do not need to be mapped, which is relatively safe. 2. Start configuration 1. Solve the fixed IP problem [root@docker ~] # docker network create-d bridge-- subnet 192.168.1.0 lnmp# 24-- gateway 192.168.1.1 create a custom network, and specify network segments and gateways. Only network segments are defined. Only then can you use this network to assign a fixed IP2, Run Nginx container [root@docker] # docker run-d-- name test nginx # first run any nginx container [root@docker] # mkdir / data # create the corresponding directory [root@docker ~] # docker cp test:/etc/nginx / data # copy a copy of the nginx configuration file directory in the container [root@docker ~] # docker cp test:/usr/share/nginx/html / data # to root the web page A copy of the catalog is also copied # the reason why This is because the configuration file in the container is overwritten when data persistence is implemented. [root@docker ~] # docker run-d-- name nginx-- network lnmp-- ip 192.168.1.2-p 80:80-v / data/nginx:/etc/nginx-v / data/html:/usr/share/nginx/html nginx# runs the Nginx container based on the newly created Nginx network and specifies its IP address; # use the "- v" option to mount the directory to the configuration file of nginx and the root directory of the web page, respectively, in order to achieve data persistence # in the process of data persistence, it has also solved the problem that some basic commands cannot be used in the nginx container. # if you need to change the nginx configuration file or network root directory, you can directly perform the corresponding operations in the local / data directory. 3. Run the MySQL container

Because in the production environment, most companies are very taboo to run services like MySQL on virtualization technology, usually a single server, only running MySQL services, so, for simplicity here, just run the MySQL container, and do not do the persistence of the MySQL data (mainly lazy to find those directories to achieve data persistence).

[root@docker] # docker run-d-- name mysql-e MYSQL_ROOT_PASSWORD=123.com-- network lnmp-- ip 192.168.1.4 mysql:5.7 4, run PHP container [root@docker] # docker run-d-name phpfpm-v / data/html:/usr/share/nginx/html-- network lnmp-- ip 192.168.1.3 php:7.2-fpm# if the container fails directly Then you need to execute the "docker pull php:7.2-fpm" command to download the image, and then execute the above run container command # since you need the same web page directory as the Nginx server, mount the same directory 5 and modify the Nginx configuration file In order to associate the PHP container [root@docker ~] # vim / data/nginx/conf.d/default.conf # edit the Nginx configuration file directly in the host. # omit part of the content location / {root / usr/share/nginx/html Index index.html index.htm index.php; # add "index.php"} # add the following location {} configuration segment location ~\ .php$ {root / usr/share/nginx/html; fastcgi_pass 192.168.1.3 php$ 9000; # specify port 9000 fastcgi_index index.php of the PHP container Fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;}. # omit part of the content # save and exit after editing. [root@docker ~] # docker restart nginx # restart the Nginx container so that the configuration takes effect [root@docker ~] # vim / data/html/test.php # Edit the php web page file to test whether Nginx and PHP work together 6. Client access test:

1) directly access the IP address of the docker server (default is port 80):

2) visit the test.php page of the docker server:

3) Test the coordination between PHP container and MySQL:

You can see the above page, which shows that there is no problem with the coordination between the Nginx container and the PHP container. What about the MySQL container? Although you can test the coordination between the PHP and the MySQL database by writing a PHP web page file (you can refer to it in the previous LNMP or LAMP), this time you can test it in a different way, that is, using the web tool PHPmyadmin (managing the MySQL database through the web page).

Download phpmyadmin:

[root@docker ~] # cd / data/html/ # switch to the root directory of the web page [root@docker html] # wget https://files.phpmyadmin.net/phpMyAdmin/5.0.0-alpha1/phpMyAdmin-5.0.0-alpha1-all-languages.zip# download phpmyadmin source code package [root@docker html] # unzip phpMyAdmin-5.0.0-alpha1-all-languages.zip# decompress source code package [root@docker html] # mv phpMyAdmin-5.0 .0-alpha1-all-languages phpmyadmin# changes the unzipped directory name [root@docker html] # vim / data/nginx/conf.d/default.conf # Editing the Nginx main configuration file # does not edit and will not affect the next operation. # omit part of the content Add the following "location {}" field location / phpmyadmin {root / usr/share/nginx/html Index index.html index.htm index.php;} location ~ / phpmyadmin/ (? (. *)\. (php | php5)? $) {root / usr/share/nginx/html; fastcgi_pass 192.168.1.3 phpmyadmin/ 9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params } # after editing, save and exit. [root@docker html] # docker restart nginx # restart the Nginx container for the changes to take effect

Visit the host's IP address + phpmyadmin to test (phpmyadmin is the directory under the root directory of the web page):

OK, when you see an error, it means that there is no problem with what you have done. Normally, you will prompt this error, because when you compile and install PHP normally, you need to add "--with-mysql...". Looking at this page, it is obvious that the PHP container we are running does not add options for the database, and then we will solve these problems.

7. Solve the problem that the PHP container does not support associated MySQL databases

Log in to the docker website, search for "PHP" and click to enter, as follows:

After clicking to enter, drop down the page and find the following paragraph:

The general idea of the above paragraph is that if you want to extend the PHP container, you need to add the following before the Dockerfile file. The translation of the web page is as follows:

Well, now that the official website has seen the answer, let's generate a new image based on the original PHP image by writing Dockerfile:

[root@docker html] # vim Dockerfile # write Dockerfile file The file begins with the copied code FROM php:7.2-fpmRUN apt-get update & & apt-get install-y\ libfreetype6-dev\ libjpeg62-turbo-dev\ libpng-dev\ & & docker-php-ext-install-j$ (nproc) iconv\ & & docker-php-ext-configure gd-- with-freetype-dir=/usr/include/-- with-jpeg-dir=/usr/include/\ & & docker-php-ext-install-j$ (nproc) gd\ # add slash & & docker-php-ext-install mysqli pdo pdo_mysql # add line # after writing Save and exit [root@docker html] # docker build-t newphp:7.2-fpm. # generate a new image, do not leave the "." at the end of the command. [root@docker html] # docker rm-f phpfpm # original PHP container can delete [root@docker html] # docker rmi php:7.2-fpm # original image can also be deleted [root@docker html] # docker images # query existing image Confirm that there is a newly created "newphp:7.2-fpm" REPOSITORY TAG IMAGE ID CREATED SIZEnewphp 7.2-fpm f5fd4d8e8447 3 minutes ago 423MBnginx latest 540a289bab6c 14 hours ago 126MBmysql 5.7 cd3ed0dfff7e 6 days ago 437MBcentos latest 0f3e07c0138f 3 weeks ago 220MB [root@docker html] # docker run-d-- name phpfpm-v / data/html:/usr/share/nginx/html-- network lnmp-- ip 192.168.1.3 newphp:7.2-fpm # run the new PHP container based on the newly created PHP image

If you visit the host IP+phpmyadmin again to test, you will see the following page (if it is still a failure page, you can refresh it):

You think that's it? It's a little naive. You can log in with the root user and the "123.com" password specified when running the container, which must indicate that you can't log in to the MySQL server.

You also need to change the configuration file for phpmyadmin to specify the MySQL database IP address:

[root@docker phpmyadmin] # pwd # determine the current working path / data/html/phpmyadmin [root@docker phpmyadmin] # cp config.sample.inc.php config.inc.php # rename the main configuration file [root@docker phpmyadmin] # vim config.inc.php # Edit the main configuration file. # in the omitted section Allow $cfg ['Servers'] [$I] [' host'] = '192.168.1.4' # change the original "localhost" to the IP address of the MySQL container. # omit part of the content # after the change is completed, save and exit

Login test again (username "root", password "123.com"):

OK!!! If you can see the above interface, it means that there is no problem with the coordination among Nginx, MySQL, and PHP containers, that is, the separation and deployment of LNMP is completed.

-this is the end of this article. Thank you for reading-

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report