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

Docker's method of building Nginx+PHP+MySQL environment and deploying WordPress

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "Docker how to build Nginx+PHP+MySQL environment and deploy WordPress". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Preparatory work

Use Aliyun kelude

To create a project such as dockerfile. Then we put all the relevant dockerfile and configuration files for the wordpress environment into the centosbz directory.

Use Ali Cloud Image Warehouse

The address of Aliyun docker image repository is used to store docker images. You can use local push images or pull dockerfile from kelude to build images automatically. We log in first, then create a new namespace, such as centos-bz, and then all nginx,php,mysql images will be stored under this namespace.

Install docker-compose

You need to install docker-compose on the host running the docker container, either manually by referring to the official documentation or using ezhttp's one-click installation tool (recommended). Such as:

Wget centos.bz/ezhttp.zip unzip ezhttp.zip cd ezhttp-master. / start.sh

Then a menu pops up, enter 2 to select some useful tools, and then enter 18 to choose to install docker and compose.

Write dockerfile

The dockerfile created in Aliyun kelude above clone is mirrored locally. Create a centos.bz in this project, and then create a mysql,nginx,php directory in the centos.bz directory to store their respective dockerfile and configuration files.

Here we also agree on the following directories:

/ home/docker/nginx/logs/centos.bz: stores the log of the www.centos.bz website

/ home/docker/nginx/www/centos.bz: files stored on the www.centos.bz website

/ home/docker/php: stores the log of php-fpm

/ home/docker/mysql:mysql data directory

Nginx dockerfile

Create a dockerfile file in the nginx directory and write the following:

# install nginxfrom debian:jessie from debian:jessie image # declare the mailbox of this dockerfile maintainer If you have any questions, you can send this email to label maintainer "admin@centos.bz" # to define the software version and the compilation tool variable env nginx_version 1.10.3env openssl_version 1.0.2henv zlib_version 1.2.11env pcre_version 8.40env concat_version 1.2.2env build_tools wget gcc make g++env src_dir / opt/nginx # to switch to the working directory workdir ${src_dir} # to start compiling nginx. We use compilation to install nginx here instead of using the official nginx image because we use a third-party concat module that can only be compiled. # write all installation commands in a run instruction because it can reduce the number of image layers and reduce the image size. It is recommended that you use a backslash and & & to put all installation commands on one line. Run apt-get update\ & & apt-get-y-- no-install-recommends install ca-certificates ${build_tools}\ & & wget http://nginx.org/download/nginx-${nginx_version}.tar.gz\ & & wget https://www.openssl.org/source/openssl-${openssl_version}.tar.gz\ & & wget http://www.zlib.net/zlib-${zlib_version}.tar.gz\ & & wget Https://ftp.pcre.org/pub/pcre/pcre-${pcre_version}.tar.gz\ & & wget https://github.com/alibaba/nginx-http-concat/archive/${concat_version}.tar.gz-o nginx-http-concat-$ {concat_version} .tar.gz\ & & tar xf nginx-$ {nginx_version} .tar.gz\ & & tar xf openssl-$ {openssl_version} .tar.gz\ & & tar xf zlib -${zlib_version} .tar.gz\ & & tar xf pcre-$ {pcre_version} .tar.gz\ & & tar xf nginx-http-concat-$ {concat_version} .tar.gz\ & & cd nginx-$ {nginx_version}\ &. / configure-- prefix=/usr/local/nginx-- with-pcre=../pcre-$ {pcre_version}\-- with-zlib=../zlib-$ {zlib_version}\ -- with-http_ssl_module\-- with-openssl=../openssl-$ {openssl_version}\-- add-module=../nginx-http-concat-$ {concat_version}\ & & make-j$ (nproc)\ & & make install\ & & rm-rf ${src_dir}\ & & apt-get purge-y-- auto-remove ${build_tools}\ & & rm-rf / var/lib/ Apt/lists/* # put the build context directory conf That is, the files under the dockerfile/centos.bz/nginx/conf directory are copied to the container's / usr/local/nginx/conf directory. Copy conf/ / usr/local/nginx/conf/ # defines the command entrypoint ["/ usr/local/nginx/sbin/nginx"] expose 80443 to run when the container is started

For the nginx configuration file under the conf directory, you need to change the log and website directory to the directory location of the following convention.

Php-fpm dockerfile

Create the dockerfile/centos.bz/php-fpm directory, and create the dockerfile file under this directory, as follows:

From debian:jessielabel maintainer "admin@centos.bz" # defines the software version, compiling tools Dependent variables such as env php_version 5.6.30env build_tools M4\ autoconf\ autoconf2.13\ openssl\ wget\ gcc\ make env build_deps libcurl4-gnutls-dev\ libxml2-dev\ zlib1g-dev\ libpcre3-dev\ libjpeg-dev\ libpng12-dev\ libfreetype6-dev\ libmhash-dev\ libmcrypt-dev\ libssl-dev\ libtool env php_location / usr/local/ Phpenv build_arg-- prefix=$ {php_location}\-- with-config-file-path=$ {php_location} / etc\-- enable-fpm\-- enable-bcmath\-- with-pdo_sqlite\-- with-gettext\-- with-iconv\-- enable-ftp\-- with-sqlite3\-- enable-mbstring\-enable-sockets\-- enable-zip\-- enable -soap\-- with-openssl\-- with-zlib\-- with-curl\-- with-gd\-- with-jpeg-dir\-- with-png-dir\-- with-freetype-dir\-- with-mcrypt\-- with-mhash\-- with-mysql=mysqlnd\-- with-mysqli=mysqlnd\-- with-pdo-mysql=mysqlnd\-- without-pear\-- with -libdir=lib64\-- enable-opcache\-- disable-cgi env src_dir / opt/php workdir ${src_dir} # start compiling and installing phprun apt-get update\ & & apt-get-y-- no-install-recommends install ${build_deps} ${build_tools}\ & & wget http://php.net/distributions/php-${php_version}.tar.gz\ & & tar xf php-$ {php_version} .tar .gz\ & & cd php-$ {php_version}\ & & ln-s / usr/lib/x86_64-linux-gnu/libssl.so / usr/lib/libssl.so\ & & ln-s / usr/lib/ usr/lib64\ & &. / configure ${build_arg}\ & & make-j$ (nproc)\ & & make install\ & & cp php.ini-production ${php_location} / etc/php.ini\ & & Cp ${php_location} / etc/php-fpm.conf.default ${php_location} / etc/php-fpm.conf\ & & rm-rf ${src_dir}\ & & apt-get purge-y-- auto-remove ${build_tools}\ & & rm-rf / var/lib/apt/lists/* workdir ${php_location} / etc/ # configure php-fpm Even if you use the sed tool to edit php-fpm.conf and php.ini files, the php-fpm-related configuration commands here are not combined with the above compilation commands to reduce the number of layers because the # configuration file may be changed a lot, so that when the configuration file changes, you can directly use the cache to skip the compilation step and speed up the build. Run set_php_variable () {\ local key=$1;\ local value=$2;\ if grep-Q-e "^ $key\ srooms =" php.ini;then\ sed-I-r "s# ^ $key\ s*=.*#$key=$value#" php.ini;\ else\ sed-I-r "sacks;\ s*$key\ s*=.*#$key=$value#" php.ini;\ fi;\ if! Grep-Q-e "^ $key\ fi =" php.ini;then\ echo "$key=$value" > > php.ini;\ fi \ & & base_dir=/home/docker/php\ & & set_php_variable disable_functions "dl,eval,assert,exec,popen,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close Proc_open "& & set_php_variable expose_php off\ & & set_php_variable error_log ${base_dir} / php_errors.log\ & & set_php_variable request_order" cgp "& & set_php_variable cgi.fix_pathinfo 0\ & & set_php_variable short_open_tag on\ & & set_php_variable date.timezone asia/chongqing\ & & sed-I's / ^ user =. * / user = Www-data/' php-fpm.conf\ & & sed-I's / ^ group =. * / group = www-data/' php-fpm.conf\ & & sed-I "s # Slowlog = log/\ $pool.log.slow#slowlog = ${base_dir} /\ $pool.log.slow# "php-fpm.conf\ & & sed-I's / Request_slowlog_timeout = 0/request_slowlog_timeout = 5gamma 'php-fpm.conf\ & & sed-I's / ^ pm.max _ children.*/pm.max_children = 20 php-fpm.conf\ & sed-I's / ^ pm.start _ servers.*/pm.start_servers = 5gamma' php-fpm.conf\ & & sed-I's / ^ pm.min _ spare_servers.*/pm.min_spare_servers = 3 php-fpm.conf\ & & sed-I's / ^ pm.max _ spare_servers.*/pm.max_spare_servers = 8max 'php-fpm.conf\ & sed-I' /\ [global\] / a\ daemonize = no' php-fpm.conf\ & & sed-I's / ^ global. * / listen = 0.0.0.09000 echo 'php-fpm.conf\ & & echo "[opcache]\ n\ zend_extension=opcache.so\ n\ opcache.memory_consumption=128\ n\ Opcache.interned_strings_buffer=8\ n\ opcache.max_accelerated_files=4000\ n\ opcache.revalidate_freq=60\ n\ opcache.fast_shutdown=1\ n "> > php.ini entrypoint [" / usr/local/php/sbin/php-fpm "] expose 9000

Mysql dockerfile

Create a dockerfile/centos.bz/mysql/dockerfile file as follows:

From mysql:5.6label maintainer "admin@centos.bz" copy my.cnf / etc/mysql/my.cnf

This dockerfile is very simple, directly using the official mysql image, the only difference is that we use our own defined my.cnf configuration file.

For the my.cnf configuration file, you need to point the log and data directory to / home/docker/mysql. A sample my.cnf file is as follows:

# generated by ezhttp at 2016-02-03 01:05:29 [mysql] # client # port = 3306socket = / home/docker/mysql/mysql.sock [mysqld] # general # port = 3306user = mysqldefault-storage-engine = innodbsocket = / home/docker/mysql/mysql.sockpid-file = / home/docker/mysql/mysql.pidskip-name-resolve # myisam # key-buffer-size = 32m # innodb # # innodb-flush-method = o_directinnodb-log-files-in-group = 2innodb-log-file-size = 64minnodb-flush-log-at-trx-commit = 2innodb-file-per-table = 1innodb-buffer-pool-size = 1g # caches and limits # tmp-table-size = 32mmax-heap-table-size = 32mquery-cache-type = 0query-cache-size = 0max-connections = 50open-files-limit = 1024table-definition-cache = 100table-open-cache = 400 # safety # max-allowed-packet = 16mmax-connect-errors = 1000000 # data storage # datadir = / home/docker/mysql # logging # log-error = / home/docker/mysql/mysql-error.loglog-queries-not-using-indexes = 1slow-query-log = 1slow-query-log-file = / home/docker/mysql/mysql-slow.log # binary logging # log-bin = / home/docker/mysql/mysql-binserver-id = 1expire-logs-days = 14sync-binlog = 1

Build an image

Push the file created in the previous step to Aliyun's kelude. Then we log in to Aliyun's docker image repository cr.console.aliyun.com. Here, take setting the automatic construction of nginx image as an example, php and mysql image construction settings are similar.

1. Click "Image list" on the left, and click the warehouse image on the right, as shown in the figure:

two。 In the creation dialog box of repository image, the instructions are as follows:

Region: select the location closest to the deployment of the docker host, and select East China 1 or East China 2 if you are in China.

Namespace and warehouse name: select centos-bz,nginx here.

Set code source: we choose Aliyun code here.

Build settings: check to build images automatically when code changes, overseas machine construction (because domestic host apt-get is slow to install software), dockerfile path fill / centos.bz/nginx

Click the create Warehouse button when you are finished.

As shown in the figure:

3. Go back to the image list, find the nginx image, and click manage.

4. Click "build" on the left and "build now" on the right to start the first build, and then we will build automatically after we change the dockerfile and configuration file to kelude.

5. View the log to view the build process.

Then continue to complete the image build settings for php,mysql.

Startup environment

To facilitate unified management of nginx,php,mysql startup, we use the docker-compose tool. We just need to write a docker-compose.yml file and then use the docker-compose tool to quickly start the docker container. After that, transfer the docker-compose.yml to any host that supports the docker environment, and you can quickly configure the running environment of wordpress.

Docker-compose.yml

Place the docker-compose.yml file in the / home/docker directory.

Version: '3services # defines three services nginx,php,mysqlservices: nginx: # depends on the php service, which means that before starting nginx, start the path to the php depends_on:-php # nginx image image: registry.cn-hangzhou.aliyuncs.com/centos-bz/nginx # container's / home/docker/nginx directory mount the / home/docker/nginx directory in the host # this enables the nginx container to store website files and directories in the host directory Persistence and ease of management volumes:-/ home/docker/nginx:/home/docker/nginx # nginx automatically restarts restart: always # mapping 80 and 443 ports ports:-"80:80"-"443 nginx" # Container name container_name: nginx php: depends_on:-mysql image: registry.cn-hangzhou.aliyuncs.com/centos-bz/php-fpm restart : always volumes:-/ home/docker/nginx/www:/home/docker/nginx/www-/ home/docker/php:/home/docker/php container_name: php mysql: image: registry.cn-hangzhou.aliyuncs.com/centos-bz/mysql volumes:-/ home/docker/mysql:/home/docker/mysql restart: always # set the mysql_root_password environment variable Here is the root password for setting mysql. This is root. Environment: mysql_root_password: root container_name: mysql

Startup environment

Execute in the / home/docker directory:

Docker-compose up

Check whether nginx,php,mysql starts normally. If so, ctrl-c stops, and then executes:

Docker-compose up-d

Here the compose command starts in the background.

Execute docker ps to check the container's running status.

Connection issu

Containers can be connected by the name of the container, such as the code fastcgi_pass php:9000 in the nginx configuration file to connect to php, and the website database configuration file uses mysql:3306.

Daily operation and maintenance

Transfer

For example, host an is migrated to host b It only takes three steps.

1. Package the / home/docker directory of host an and transfer it to the same location of host b

two。 Configure b-host docker environment

3. Execute docker-compose up-d under the / home/docker directory of host b

Export and import database

Import the centos.sql.gz database file into the centos database:

Gunzip

< centos.sql.gz | docker exec -i mysql mysql -uroot -proot centos 把centos数据库导出到centos.sql.gz docker exec -i mysql mysqldump -uroot -proot centos | gzip >

Centos.sql.gz

Backup

It is recommended to use ezhttp one-click backup settings:

Wget centos.bz/ezhttp.zipunzip ezhttp.zipcd ezhttp-master./start.sh

Then a menu pops up, enter 2 to select some useful tools, and then enter 14 to select backup settings. It is important to note that when setting up mysql to use mysqldump backup, when prompted for mysql bin directory, enter docker exec / usr/bin/.

This is the end of the introduction of "Docker's method of building a Nginx+PHP+MySQL environment and deploying WordPress". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report