Reprint it on my blog

As an iOS front-end engineer, I have recently developed an interest in PHP. I think this is not an idle job. As a programmer, I should not limit myself to a closed field, but should always be curious

If you want to do a good job, you must first build a good tool. First of all, you must build a development environment. Because I don’t like the development environment to mess up my computer and install some dependent development packages randomly, so I choose virtual machine +Docker to build the environment ~

Let’s get started:

Docker to prepare

I chose the CentOS7 distribution of Linux, which is fully compatible with Redhat. I downloaded Linux and installed the system on the vm, which is omitted here. When Linux is ready, I can start installing Docker.

Delete the old version

If you want to reinstall the new version, please use the following command to uninstall the old version of Docker, please go to the second step for the first installation

$ sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine
Copy the code

Docker installation Settings

Docker provides two ways of installation. It is officially recommended to install Docker in the way of software warehouse, which is convenient for updating and other operations. The other way is to install Docker in the way of RPM, which will not be introduced here

Install it directly in the official recommended way, first set up the Docker software warehouse

Set up the official software repository of Docker

  1. Install the software packages that Docker depends on

    $ sudo yum install -y yum-utils \
      device-mapper-persistent-data \
      lvm2
    Copy the code
  2. Set the stable version of the Docker warehouse address, Docker also provides the test version of the address, here we do not need, as long as the stable version is ok.

    $ sudo yum-config-manager \
        --add-repo \
        https://download.docker.com/linux/centos/docker-ce.repo
    Copy the code

Docker is officially installed

  1. Install the latest version of Docker

    $ sudo yum install docker-ce
    Copy the code
  2. Start the Docker service

    $ sudo systemctl start docker
    Copy the code
  3. Test whether Docker works properly

    $ sudo docker run hello-world
    Copy the code

    If hello world is printed, Docker is installed properly

Write Dockerfile

Dockerfile is a series of Docker command scripts used to make images. The Docker build command will generate our customized images according to the Settings in the Dockerfile.

First, prepare a directory structure as follows:

├─ ├─ phptest # └ ├─ index.php # A simple PHP code, like <? php phpinfo(); ? # > ├ ─ ─ mysql mysql database related directory │ ├ ─ ─ the conf # mysql configuration file directory │ └ ─ ─ data # mysql data directory ├ ─ ─ nginx # nginx related directory │ ├ ─ ─ the conf # │ nginx configuration file directory ├ ─ ├ ─ uninhibit-dockerfile └─ uninhibit-dockerfile └─ uninhibit-dockerfile └─ uninhibit-dockerfile ├ ─ uninhibit-dockerfileCopy the code

In the php-mysqli Dockerfile file, enter the following:

RUN docker-php-ext-install mysqli RUN docker-php-ext-install mysqli FROM PHP :7.2.11-fpmCopy the code

After completion, there is no need to manually run docker build command to generate the image, and later use docker-compose to automatically build the image for us.

Install the docker – compose

Docker-compose is a tool provided by Docker for managing multiple Docekr containers. Docker recommends that one container run a service process. The environment we build here includes: Docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose

  1. Download the docker-compose command file

    Sudo curl - L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname - s) - $(uname -m)" - o /usr/local/bin/docker-composeCopy the code
  2. Grant executable permissions to the docker-compose command file

    sudo chmod +x /usr/local/bin/docker-compose
    Copy the code
  3. Test that the installation is correct

    $ sudo docker-compose --version
    Copy the code

    If the version number is displayed, the installation is complete

Write the docker – compose. Yml

Docker-compose command can automatically manage the docker image according to the docker-compose. Yml file we wrote.

Create a new docker-comemess. yml file under the root directory we created and type the following:

Docker-compose docker-compose docker-compose docker-compose docker-compose docker-compose docker-compose docker-compose docker-compose Phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin /phpmyadmin Nginx: image: nginx:latest ports: - "80/80" # depends_on: - "PHP" volumes: # mount the specified file or directory - ". / nginx/conf/nginx. Conf: / etc/nginx/nginx. Conf "-". / HTML: / home: ro "networks: - app_net container_name: Image: PHP :7.2.11-fpm-mysqli ports: - "9000:9000" depends_on: - mysql volumes: # - "./ HTML :/home" networks: - "9000:9000" depends_on: - mysql volumes: # -app_net container_name: "composing - PHP" mysql: image: mysql:5.7.24 # Ports: - "3306:3306" Volumes: - "./mysql/data:/var/lib/mysql" - "./mysql/conf/my.cnf:/etc/mysql/my.cnf" environment: -mysql_root_password = ZHAOfucheng Networks: APP_net: ipv4_address: 10.10.10.1 # specify the IP address of the internal network where container_name: "comemag-mysql" networks: app_net: driver: bridge - subnet: 10.10.0.0/16 # Use CIDR to divide the subnetCopy the code

Prepare configuration files for Nginx and Mysql

Here we need to put the configuration file in our designated directory. Later configuration changes do not need to go into the container, and we can better manage the configuration of our service.

There are two ways to prepare an image file: one is to write it from scratch; the other is to copy a default image file from the image and modify it according to your needs. In this case, we choose to copy the image file from the image.

Nginx configuration file

Prepare the Nginx configuration file

Copy the Nginx file

  1. Start a temporary container

    $ sudo docker run --name tmp-nginx-container -d nginx
    Copy the code
  2. Copy the configuration files from the temporary nginx image into our own nginx/conf directory

    $ sudo docker cp tmp-nginx-container:/etc/nginx/nginx.conf ./nginx/conf/nginx.conf
    Copy the code
  3. Delete the created temporary container

    $ sudo docker rm -f tmp-nginx-container
    Copy the code

Edit the Nginx configuration file

Edit the file as follows:

user nginx; worker_processes 2; Error_log /var/log/nginx/error.log warn; error_log /var/log/nginx/error.log warn pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; Log_format main '$remote_addr - $remote_user [$time_local] "$request" "$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; # access log directory address access_log/var/log/nginx/access. Log the main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; server { listen 80; Server_name test.ng; Root /home/phptest = root /home/phptest The path of the PHP access_log # project/var/log/nginx/access. Log the main; error_log /var/log/nginx/error.log error; location / { index index.html index.htm index.php; } # location ~ \.php${fastcgi_pass PHP: 9000; Docker-comemage. yml fastcgi_index index.php; SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; Include fastcgi.conf; Because I'm going to introduce a new configuration file, I'm going to go to the trouble of using this method and I'm going to choose it according to my personal preference.Copy the code

Mysql configuration file

Also copy the Mysql configuration file

  1. Start a temporary Mysql container

    $ sudo docker run --name tmp-mysql-container -dMysql: 5.7.24
    Copy the code
  2. Copy the configuration file from the temporary Mysql container to the directory we specified

    $ sudo docker cp tmp-mysql-container:/etc/mysql/my.cnf ./mysql/conf/my.conf
    Copy the code
  3. Delete the temporary Mysql container

    $ sudo docker rm -f tmp-mysql-container
    Copy the code

    Here we’ll just use the default mysql configuration.

Add the VM IP address to the host file

Enter the following information on the VM terminal:

$ ifconfig eth0 					Check the default nic informationeth0: Flags = 4163 < UP, BROADCAST, RUNNING, MULTICAST > mtu 1500 inet 192.168.31.150 netmask 255.255.255.0 BROADCAST 192.168.31.255 inet6 fe80::caec:3251:7864:4814 prefixlen 64 scopeid 0x20<link> ether 00:1c:42:a4:5b:ef txqueuelen 1000 (Ethernet) RX Packets 2341561 bytes 1335991534 (1.2 GiB) RX errors 0 dropped 0 Overruns 0 frame 0 TX packets 520557 bytes 56885302 (54.2 MiB) TX errors 0 Dropped 0 Overruns 0 carrier 0 collisions 0Copy the code

Inet 192.168.31.150 is the IP address of our VIRTUAL machine. My computer is a Mac system, so I go back to the terminal of my Mac system and enter:

$ sudo vim /etc/hosts
Copy the code

Add the specified virtual host name and IP address to the host file in the following format and save and exit.

Ng #Ngxin service address 192.168.31.150:8080 phpadmin.netCopy the code

Later we can access the Nginx test directly by typing http://test.ng/ in the browser.

Start our service

Back in the virtual machine, our file structure so far is as follows:

Testphp ├ ─ ─ docker - compose. Yml ├ ─ ─ HTML │ └ ─ ─ phptest │ ├ ─ ─ index. The PHP │ ├ ─ ─ testmysql. PHP ├ ─ ─ mysql │ ├ ─ ─ the conf │ │ └ ─ ─ My. CNF │ └ ─ ─ data ├ ─ ─ nginx │ ├ ─ ─ the conf │ │ └ ─ ─ nginx. Conf │ └ ─ ─ logs └ ─ ─ PHP - mysqli └ ─ ─ DockerfileCopy the code

Edit our index.php file and enter the following:


      
phpinfo();
? >
Copy the code

Some people don’t type

Testmysql.php to test whether you can connect to the Mysql database:


      
  $db = new mysqli('10.10.10.1:3306'.'root'.'zhaofucheng'.'testdatabase');
  if (mysqli_connect_errno()) {
    echo 'Error: Could not connect to database. Please try again later.';
    exit;
  } else {
    echo 'The connection is successful.';
  }
  $db->close();
? >
Copy the code

Make sure we type in our self-defined root directory:

testphp]$ sudo /usr/local/bin/docker-compose up -d
testphp]$ sudo docker ps
Copy the code

If you can see that all four containers are started after input, it means we have succeeded. If there is any error, please go back to the above to carefully check the configuration item, or Google, Baidu and message to me to solve ~

Test our service

First enter the address http://test.ng/ in the browser of our own computer. The successful page is shown as follows:

This means that Nginx and PHP are working perfectly together

To test whether phpMyAdmin is also successfully started, enter the address http://phpadmin.net in the browser to visit, the successful page is as follows:

The above figure shows that phpMyAdmin has been successfully started. If you can log in normally by entering the Mysql password configured by us, it means that phpMyAdmin and Mysql cooperate perfectly ~

Create a new database called testDatabase to test whether PHP can connect to Mysql properly.

Now to witness The miracle of time ~, enter http://test.ng/testmysql.php in your browser to access, if The page displays The connection is successful. That means success and you can have fun.

Mysql8.0 error causing PHP connection error when changing default password authentication mode

When I use Mysql8.0 image to configure, I find that THE default CONFIGURATION of PHP cannot log in to Mysql, resulting in connection error. Originally, Mysql8.0 has changed the password authentication mode, if you want to normal connection, Select mysql_native_password from caching_sha2_password to mysql_native_password. Select mysql_native_password from mysql_sha2_password