The content of this article is about how to build a SWoft development environment through Docker. The content is very detailed, and friends who need it can refer to it, hoping to help you.
Swoft
The first Swoole native coroutine based PHP high-performance full-stack component framework of the new era, built-in coroutine network server and commonly used coroutine client, resident memory, independent of traditional PHP-FPM, full asynchronous non-blocking IO implementation, similar to synchronous client writing to achieve the use of asynchronous client. No complex asynchronous callbacks, no cumbersome yield, go-like coroutines, flexible annotations, powerful global dependency injection containers, perfect service governance, flexible and powerful AOP, standard PSR specification implementation, etc., can be used to build high-performance Web systems, apis, middleware, basic services, and so on.
preface
My official group click here.
Swoft is a high-performance coroutine PHP full-stack framework built on top of Swoole, and Swoole is an advanced skill in PHPer, so it is very difficult for many people to install in the relevant environment, especially Swoft. This article will use Docker to solve the deployment of runtime and development environments in an extremely simple way.
Docker
Can be seen from wikipedia, the Docker is an open source application container, engine allows developers to pack their applications and depend on the package to a portable container, and then release to any popular Linux machine, also can implement virtualization, the container is completely use the sandbox mechanism, there is no interface between each other, We can package our code and runtime environment into a container that can be distributed to any popular Linux machine. The Linux machine is not exactly the Linux machine. Thanks to the Development of the Docker for Windows project and Hyper-V, Docker can also run fine on Windows 10, but I don’t recommend using Docker for Windows in production environments.
Docker noun concept
Here, some nouns commonly used by Docker are simply explained and explained for beginners to understand the following
Dockerfile
.Dockerfile
是Docker mirror
Description of the file, throughdocker build
Command to build intoThe mirror
- Mirror (
Image
),Dockerfile
Built, including the operating system and operating environment - Container (
Container
), the container is a running image, which can be understood as a mirrorDocker
The build and package phases of the life cycle, while containers are the start and execute phases - Mirror warehouse (
Repository
) for storing the builtDocker mirror
The warehouse can be understood as similar toGit
The warehouse
Install the Docker
The installation process of Docker is not complicated. This section will introduce the installation process of Linux and Windows 10 system. However, it is not recommended to use Docker environment to run or develop Swoft project on Mac system. Because the performance of shared disks on Mac for Docker is extremely poor, Swoft takes a long time to start up.
Install Docker and Docker-compose on Linux
CentOS: yum install docker-y Ubuntu: yum install docker-y Ubuntu: Apt-get install docker-engine-y just need to execute the above line command in terminal according to the difference of the system to complete the docker installation. After the installation is complete, we need to execute the service docker start command to start the Docker service.
Docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose Yum install python-pip -y && PIP install –upgrade PIP && PIP install -u docker-compose Ubuntu: Apt-get install python-pip -y && PIP install –upgrade PIP && PIP install -u docker-compose Docker-compose can be installed by executing the above command in the terminal.
Install Docker and Docker-compose on Windows 10
We directly download the corresponding installation package from the official Docker website store.docker.com/edit… Please Login to Download, which means we need to Login to the Docker account before downloading. We can directly click the button to Login to the Login page to complete account registration or Login, and then click Get Docker to Download on the above link page. Note that this account will also be used later when we use it. After downloading the installation package, you can directly run the installation package for installation, the whole process can be said to be a fool, continue the next step, pay attention to the need to start the system hyper-V before installation, start the process is relatively simple can refer to other articles segmentfault.com/a/11… If you must use a virtual machine, such as Vagrant, you can also run a Linux operating system inside the virtual machine. Then follow the Linux installation process described in this article. Run Docker inside the virtual machine as the development environment. Docker-compose is already included in the latest Docker installation package, so there is no need to do any extra operations. After the installation is completed, restart the computer. When you see the Docker Icon in the taskbar showing Docker is running, it means that Docker has been successfully started.
We need to right-click Docker and click Sign in/Create Docker ID to log in to the Docker ID we just registered in order to get the permission to get the public image from DockerHub.
Right click Docker and click Settings to switch to Shared Drives. Check the drive letter where your project code is located and click Apply in the lower right corner to complete authorization.
Swoft development environment
Modify the official default docker-comemage. yml file
Clone Swoft project from Github by using git clone https://github.com/swoft-cloud/swoft Docker-compose. Yml is the docker-compose layout configuration file for docker-compose.
version: '3'
services:
swoft:
container_name: swoft
image: swoft/swoft
ports:
- "80:80"
volumes:
- ./:/var/www/swoft
stdin_open: true
tty: true
command: php /var/www/swoft/bin/swoft startCopy the code
This is a relatively simple layout file, only swoft has one service, and there is not too much content associated with it. As for the file format of docker-comemage. yml, we do not give too much explanation here, and you can find relevant content for reading and understanding.
The swoft/ WWW /swoft directory is shared with the /var/ WWW /swoft directory in the container. The swoft/ WWW /swoft directory is shared with the /var/ WWW /swoft directory in the container. Start the interactive terminal with the container and start the Swoft service when the choreographer file is started.
Note that the command on the default choreographer file configates PHP /var/www/swoft/bin/swoft start, which is the command to start the swoft service. But if we just clone the project and execute docker-compose up to try to start the container, we will get a failure. The Swoft instance cannot run properly because the composer install dependency has not been implemented and the vendor folder and Autoload files are missing. The default choreography file is stdin_open: true and tty: The two parameters true correspond to the two parameters -i and -t respectively on the docker command. Simply understood, -i enables the input function, and -t enables the interactive terminal ina connection container. We can use these two parameters. And change the command line of the choreography file to command: /bin/bash, so that the Swoft service is not directly started after the container is started, but manually started by us through the interactive terminal into the container.
Here is an example of a modified docker-comemage.yml file:
version: '3'
services:
swoft:
container_name: swoft
image: swoft/swoft
ports:
- "80:80"
volumes:
- ./:/var/www/swoft
stdin_open: true
tty: true
command: /bin/bashCopy the code
Start the development environment container
In the layout file directory at this time we launched a terminal (Shell), then execute the docker – compose up – d, 3-d mean in Daemon Mode (Daemon Mode), for we are in the same terminal (Shell) into the container, After executing this command we can see Starting Swoft… Done indicates that the container is successfully started. If the following error occurs when executing the startup command, it indicates that port 80 of the host machine has been occupied. Change the 80:80 in docker-comemage. yml file to another port that is not occupied. Note that the first 80 refers to the port of the host machine and the second 80 refers to the port in the container. That means we only need to change the first one
ERROR: for swoft Cannot start service swoft: b'driver failed programming external connectivity on endpoint Swoft (dab0f4d00620e2f5c07e33084ca5cac6f08cb48018d6b737eadc035e5aa0b597) : Bind for 0.0.0.0:80 failed: port is already allocated'Copy the code
Enter the development environment container
You can view the started container information by executing the docker ps command. The following is an example:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f22173763374 swoft/swoft:latest "docker-php-entrypoin"About a minute ago Up About a minute 0.0.0.0:80->80/ TCP swoftCopy the code
The Container ID is F22173763374 and the Container Name is SWoft. Docker execit f22173763374 bash or Docker execit swoft bash enter the container via interactive terminal (Terminal).
The input device is not a TTY. If you are using mintty, try prefixing the command with ‘winpty’, Winpty exec — it swoft bash — winpty exec — it swoft bash
Run and develop debugging
Install Composer relies on and generates Autoload files
After entering the Container through the Docker exec command, we notice that the content on the left of the cursor changes to root@f22173763374: that is, it has entered the Container, where F22173763374 is the corresponding Container ID(Container ID). Docker-comemage. yml connects the current directory of the project with the container /var/www/swoft. /var/ WWW /swoft = /var/ WWW /swoft = /var/ WWW /swoft = /var/ WWW Considering the Internet environment in China, We perform composer install command can perform before composer config – g repo. Packagist composer https://packagist.phpcomposer.com command configuration Composer China mirror source speeds up installation.
Start the Swoft service
After installing the Composer dependency, you can start the service by executing PHP bin/swoft start as soon as you see
root@f22173763374:/var/www/swoft# php bin/swoft startServer Information * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * HTTP | host: 0.0.0.0, port: In 81,type: 1, the worker: 1, mode: 3 * TCP | host: 0.0.0.0, port: 8099,type: 1, worker: 1 (Enabled)
********************************************************************
Server has been started. (master PID: 15, manager PID: 16)
You can use CTRL + C to stop run. Copy the code
That means your Swoft and launch successfully, we can open the browser to visit http://127.0.0.1:80, when you see the picture below is done!
If the host port you bind to is not 80, change it to the corresponding port. Redis connection failure host=127.0.0.1 port=6379 Apt install -y redis-server && service redis-server start
Modify the code and make it work
Swoft is a bit different from php-FPM. In PHp-FPM, you can change the content of the code directly, and then access the corresponding code to get the changed content. In PHp-FPM, the PHP code is reloaded on every request, while Swoft is persistent. This means that the code does not need to be reloaded once the service is started. This change in pattern allows Swoft’s large amount of code to be reused without reloading and re-instantiation, which is one of the major performance improvements. This change will affect the development to a certain extent. That is to say, under Swoft, you need to restart the Worker or the service to make the changed code take effect. However, thanks to Swoft’s hot reload feature, it can automatically check the code change and restart the Worker. We can just change the AUTO_RELOAD item to true using the.env file in the root directory of the project. If there is no.env file in the root directory of the project, we can just copy the.env.example file to.env and make the corresponding changes. It is important to note that hot reloading will only happen if you change code in the app directory. Changing other code will not be reloaded. This is because different code is in a different life cycle. We’ll talk more about this later when it comes to the Swoft life cycle.
I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc.