This series of articles will be introduced from the usage level.
SwoftThe use of the framework and business development, for junior high school level
The first new era PHP high-performance synergetic full stack componentization framework based on Swoole native synergetic, built-in synergetic network server and common synergetic client, memory resident, does not rely on traditional PHP-FPM, fully asynchronous non-blocking IO implementation, realizes the use of asynchronous client in a manner similar to synchronous client, does not have complicated asynchronous callback, and does not have complicated yield. It has the coordination process similar to Go language, flexible annotation, powerful global dependency injection container, perfect service governance, flexible and powerful AOP, standard PSR specification implementation, etc. It can be used to build high-performance Web systems, API, middleware, basic services, etc.
SwoftIt’s a place where
SwooleA built on top of
High Performance Concorde PHP Full Stack Framework, and
PHPerIt is an advanced skill, so it has also caused a lot of trouble to many people in the installation of related environment.
SwoftEven more so, this article will pass
DockerThe deployment of the operating environment and development environment is solved in an extremely simple way.
As can be seen from the encyclopedia,
Linux机器上，也可以实现虚拟化，容器是完全使用沙箱机制，相互之间不会有任何接口, it can also be understood that we can package our code and running environment into a container, and the packaged container can be published to any popular
LinuxOn the machine, the Linux machine referred to here is actually not accurate, thanks to
Docker for WindowsProjects and
Hyper-VThe development of,
DockerIt can also run on Windows 10 system in a good state, but the author does not recommend to
Docker for WindowsUsed in production environment.
Docker noun concept
DockerSome commonly used nouns are simply explained and explained so that beginners can understand the following
Docker mirrorThe description of the file, through
docker buildCommand to build into
- Mirror image
DockerfileBuilt, including operating system and operating environment
- Container (
Container), the container is a running mirror, can be understood as a mirror is
DockerThe construction and packaging phase in the life cycle, while the container is the startup and execution phase.
- Mirror warehouse (
Repository), used to store the built
Docker mirrorThe warehouse, can be understood as similar to
GitThe warehouse of
DockerThe installation process of is not complicated, which will be described in this section.
Windows 10The installation process under the system, and
MacOn the system and
DockerEnvironment to run or develop
SwoftProject, because in
Mac for DockerThe performance of shared disks on is extremely poor, resulting in
SwoftThe start-up phase takes a long time.
DockerThe process is quite simple
yum install docker -y
apt-get install docker-engine -y
According to the difference of the system, the above command can be executed at the terminal.
DockerAfter the installation of, we need to perform it
service docker startCommand to start it
DockerAfter that, we still need to install it
docker-composeSo as to facilitate the subsequent use of Docker.
yum install python-pip -y && pip install --upgrade pip && pip install -U docker-compose
apt-get install python-pip -y && pip install --upgrade pip && pip install -U docker-compose
According to the difference of the system, the above command can be executed at the terminal.
docker-composeThe installation of.
Windows 10Upper installation
We will go directly toDocker.comDownload the corresponding installation packagehttps://store.docker.com/edit …, non-login users we will see
Please Login to Download, which means that we should log in to the Docker account before downloading. We can click the button directly to the login page to complete the account registration or login and click on the link page above.
Get DockerDownload, note that this account will be used later when we use it.
After downloading the installation package, the installation package can be directly run for installation. The whole process can be said to be silly. The next step can be continued. Note that the system needs to be turned on before installation.
Hyper-V, the opening process is relatively simple, you can refer to other articles.https://segmentfault.com/a/11 …Attention
VMwareIs a conflict, the two cannot coexist, can only choose one, if you have to use virtual machines, such as
VagrantTools like this can also be run in a virtual machine.
Linux systemAnd then according to this article about
Linux systemThe installation process of is processed and runs in the virtual machine.
DockerAs a development environment.
The latest edition
DockerThe installation package already contains
docker-compose, there is no need to do extra operations.
After the installation is completed, restart the computer when you see the
The little whaleShow
Docker is runningThat is to say
DockerThe launch was successful.
We need to right-click
Sign in / Create Docker IDSign in to what we just registered
Docker IDIn order to get the information we can get fromDockerHubGets the permission of the public mirror in the.
Because we are used for development and use, we also need to authorize the permissions of the shared directory, right-click
Settings, set the interface to switch to
Shared Drives, check your project code
Disk drive letter, and click on the lower right corner
ApplyThe authorization can be completed.
Swoft development environment
Modify the official default
We passed the order
git clone https://github.com/swoft-cloud/swoftFrom
Clone SwoftProject, and use the project’s own
docker-compose.ymlFile to implement an environment for development,
docker-composeWe will look at the contents of the official default file:
version: '3' services: swoft: container_name: swoft image: swoft/swoft ports: - "80:80" volumes: - ./:/var/www/swoft stdin_open: true tty: true entrypoint: ["php", "/var/www/swoft/bin/swoft", "start"]
This is a relatively simple layout file, only
swoftA service is not associated with too much content, about
docker-compose.ymlWe don’t give too many explanations on the format of the document, but we can find relevant contents for reading and understanding.
A simple reading of the contents of this document can be understood as using
swoft/swoftThe official image and set the container name as
swoftA that binds the inside of the container
EightyPort and host
./The current directory and the
/var/www/swoftThe directory is a shared directory, and the interactive terminal with the container is opened and started when arranging files is started.
We can notice that the
php /var/www/swoft/bin/swoft start, that is to say
Start Swoft serviceBut if only
CloneProject and execution
docker-compose upTo try to start
containerIf so, we will get a failed result because it has not been implemented yet.
composer installTo load
ComposerThe dependence and lack of
autoloadSuch as related files, resulting in incorrect operation
SwoftExample, let’s look at the default layout file settings
tty: trueTwo parameters, respectively
-tTwo parameters, simple understanding is
-tOpen a connection container inside
Interactive terminal, we can take advantage of these two parameters and arrange the
entrypointThe line should read
entrypoint: ["sh"]To make the container not start directly after starting
SwoftService, but by us manually through
Interactive terminalEnter the container to start. (note that the Swoft official has changed the entry from command to entrypoint, and the corresponding update has been made in this article.)
The following is a modified version
version: '3' services: swoft: container_name: swoft image: swoft/swoft ports: - "80:80" volumes: - ./:/var/www/swoft stdin_open: true tty: true entrypoint: ["sh"]
Start the development environment container
At this point we start a in the directory where the files are arranged.
Terminal (Shell)And then execute
docker-compose up -d,
-dThe meaning is to
Daemon ModeRunning is convenient for us in the same one
Terminal (Shell)Entering the container, we can see after the command is executed.
Starting swoft ... doneThis means that the container was successfully started.
If you get an error when executing the startup command, it indicates that the host’s
EightyPort is already occupied, change
docker-compose.ymlWithin the document
80:80For other unoccupied ports, note the first
EightyRefers to the port of the host, the second
EightyIt refers to the port in the container, which 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'
Container for entering development environment
docker psThe command can view the container information that was started. The following is sample information:
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 swoft
swoft, we can carry out
docker exec -it f22173763374 bashOr
docker exec -it swoft bashvia
Interactive terminalEnter the container.
If an error is reported during execution
the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty', available at
docker execIncrease before command
winptyCommand resolution, i.e.
winpty docker exec -it swoft bash
Operation, development and debugging
ComposerDependency and Generation
docker execAfter the command entered the container, we noticed that the contents to the left of the cursor changed to
root@f22173763374:I.e. has entered the container, wherein
f22173763374For the corresponding
SwoftOfficial mirror image
swoft/swoftThe configured working directory is
Project current directoryContainers are associated
/var/www/swoftDirectory, that is, through
docker execThe directory entered is already
/var/www/swoftDirectory, that is, project directory, so we can directly execute it.
composer installCommand to load
ComposerThe dependency and generation of
Considering the domestic network environment, we are implementing
composer installThe command can be executed first.
composer config -g repo.packagist composer https://packagist.phpcomposer.comCommand configuration
Composer China Mirror SourceAccelerating installation speed (since Packagist China mirror source has been unavailable recently, it is recommended to switch to Laravel-China China source, i.e.
config -g repo.packagist composer https://packagist.laravel-china.org）。
ComposerOnce dependent, it can be executed
php bin/swoft startStart the service when you see
root@f22173763374:/var/www/swoft# php bin/swoft start Server Information ******************************************************************** * HTTP | host: 0.0.0.0, port: 80, type: 1, 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.
That means yours
SwoftAnd the launch was successful, we can open the browser to access it
http://127.0.0.1:80When you see the picture below, you are done!
If the port on which you bind the host is not
Eighty, you can change to the corresponding;
If what the visit saw was
Redis connection failure host=127.0.0.1 port=6379This indicates a lack of
RedisService, the simplest and most direct is directly installed in the current container
Redis ServerDirect execution
apt install -y redis-server && service redis-server startThe installation and start-up operations can be completed.
Modify the code and make it effective
PHP-FPMThere will be a little difference in the development under the mode
PHP-FPMUnder the mode of direct change code content, then access to the corresponding code can get the changed content, because
PHP-FPMEach request in mode reloads PHP code, while
Persistent operation, which means that the code does not need to be reloaded after the service is started, and this mode change can make
SwoftOne of the reasons for greatly improving performance is that a large amount of code of can be reused without reloading and re-instantiating.
Such changes will have a certain impact on development, that is to say, in
SwoftNext, you need to
Restart serviceIn order for the changed code to take effect, but thanks to
Thermal overloadFunction, can automatically check code changes and automatically
Restart Worker, we just need to go through the
trueThat is, if there is no one in the root directory of the project
.envFiles that can be copied directly
.env.exampleThe file is
.envAnd make corresponding changes, it is only necessary to pay attention to the change
appThe code in the directory will only be
Thermal overloadFunction overload, change other code will not be overloaded, this is because different code is in different life cycle, only
WorkerStartOnly then can the loaded code be reloaded, and we will cover the contents of this part later.
Swoft life cycleAt that time, we will give further explanation.