• notice
  • Congratulations on the launch of the Sought Tech site

How to quickly deploy PHP Web environment (nginx php mysql redis)

First look at the final effect, as follows:
insert image description here

What is it?

It is docker container virtualization technology . There are only a few KB description files in total, which define what to install and what to configure, and it will be automatically processed once it is executed.

what's it for?

  1. Solve the problem of newcomers setting up the environment for half a day

  2. Solve the problem of wasting time setting up the environment after reinstalling the system

  3. Consistent environment, rapid deployment

Enter the detailed explanation (this script is for debian operating system, such as deepin, ubuntu)

As shown in the figure, four files:
insert image description here

  1. Prepare the file Dockerfile-php7.4, the content is as follows. This is based on docker's official 7.4 version of PHP, and defines the installation of MySQL extensions, GD extensions, and Redis extensions. If necessary, you can increase or decrease by yourself.

FROM php:7.4-fpm

# system update
RUN apt-get update \
# Install the sql extension
&& docker-php-ext-install pdo_mysql\
# Install the graphics processing extension
&& apt install libjpeg62-turbo-dev libfreetype6-dev -y \
&& docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg \
&& docker-php-ext-install gd \
# Install the redis extension
&& pecl install -o -f redis \
# delete temporary files
&& rm -rf /tmp/pear \
# enable redis extension
&& docker-php-ext-enable redis


  1. Prepare the file docker-compose.yml with the following content. This is a project description file, which describes what to install and what to configure.

version: '3'
services:
 php:
   build:
     context: ./
     dockerfile: Dockerfile-php7.4 #Build php from this file name
   ports:
     - "9100:9000"
   container_name: common_1.0_php #container name
   volumes:
     - "/opt/wwwroot/www:/www" #Mount the host's /opt/wwwroot/www directory to the container's /www directory
   restart: always #automatic start


 nginx:
   image: nginx:1.23 #Based on the official image nginx:1.23
   ports:
     - "80:80"
   container_name: common_1.0_nginx
   restart: always
   volumes:
     - "/opt/wwwroot/www:/www"
     - "/opt/wwwroot/nginx/conf.d:/etc/nginx/conf.d"
     - "/opt/wwwroot/nginx/logs:/etc/nginx/logs"


 redis:
   image:redis:7.0
   ports:
     - "6179:6379"
   container_name: common_1.0_redis
   restart: always


 mysql:
   image: mysql:8.0
   ports:
     - "3106:3306"
   container_name: common_1.0_mysql
   restart: always
   volumes:
     - "/opt/wwwroot/mysql:/var/lib/mysql"
   environment:
     MYSQL_ROOT_PASSWORD: 123qwe.

  1. Prepare the file start.sh, which is an automated processing script with the following content:

#!/bin/bash
echo "Excuse me, what do you want to do? This is the PHP general development environment. Enter exit to exit."
echo "Install docker: install docker"
echo "Uninstall docker: remove docker"
echo "Install docker-compose: install compose"
echo "Uninstall docker-compose: remove compose"
echo "Generate PHP environment: build php env"
echo "View container list: docker ps"
echo "View image list: docker images"

echo ""


function go(){
if [ "$install_type" ]
then
if [ "$install_type" == "install docker" ]
then
sudo curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
elif [ "$install_type" == "remove docker" ]
then
sudo apt-get purge docker-ce
sudo rm -rf /var/lib/docker
elif [ "$install_type" == "install compose" ]
then
sudo curl -L "https://github.com/docker/compose/releases/download/v2.14.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/ bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose version
elif [ "$install_type" == "remove compose" ]
then
sudo /usr/local/bin/docker-compose
elif [ "$install_type" == "build php env" ]
then
sudo docker-compose up -d

elif [ "$install_type" == "docker ps" ]
then
sudo docker ps

elif [ "$install_type" == "docker images" ]
then
sudo docker images
else
echo "Please enter the correct value"
the fi
else
echo "Please enter a value"
the fi
}

# Infinite loop, so it can be processed multiple times
while [true]
do
read -p "Please enter a value:" install_type
if [ "$install_type" ] && [ "$install_type" == "exit" ]
then
break
the fi
go $install_type
done

Build a PHP Web environment

  1. Enter the current directory, execute ./start.shit, and you will see the picture effect at the beginning of the article. For this tutorial, you need to install docker and
    docker-compose first , you can install it yourself, or you can install it here, just enter the corresponding command.

  2. Enter: build php env , you may be prompted for a password, which is the current linux system administrator's password.
    insert image description here
    It's that simple, done. Seeing the above started indicates that the PHP Web environment has been successfully built and started, mysql has started, php has started, redis has started, and nginx has started. When connecting, you can use the host IP or the container name.

  3. Input: docker psCheck whether the four containers are started, and the names are common_1.0_xx
    insert image description here

Add a new website

  1. Set the host domain name, or go to the domain name provider to resolve a domain name. Please do it yourself. I have configured a sdt.com here

  2. To configure nginx, in the /opt/wwwroot/nginx/conf.d directory, create a new sdt.com.conf file with the following content:

# server configuration node
server {
    # Listening port. This port cannot be used
    listen 80;
    # The domain name of this site. Configure a host domain name directly on the host machine, or resolve it from cloud service providers such as Alibaba Cloud.
    server_name sdt.com;
    # The entry directory of this site, which is the directory of the php container. The host machine mounts the directory inside the container.
    root /www/shangdiantuanfenxiao/public;
    # Recognizable entry files in the entry directory
    index index.html index.htm index.php;
   
# Configure urls. Thinkphp's pseudo-static settings. This is fastadmin, using thinkphp5.0
    location / {
        #If the file in the access path does not exist, rewrite the URL and hand it over to ThinkPHP for processing
        if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php?s=/$1 last;
            break;
        }
    }
   
     # Configure url, process and forward PHP requests
    location ~ \.php(/|$) {
    # You can directly write the container name, or you can directly write the IP of the host machine
fastcgi_pass common_1.0_php:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
    }
    # The directory will be automatically generated.
    access_log /etc/nginx/logs/sdt.com.log;
    # error log
    error_log /etc/nginx/logs/sdt.com.error.log;
}

Note that to add a website, just add a configuration file in /opt/wwwroot/nginx/conf.d, and nginx will automatically read all configuration files in this directory.

  1. Open the website to see
    insert image description here

Attach the download address: https://gitee.com/gogls/php-web-environment.git


Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+