This series of articles will be introduced from the usage level.Swoft
The use of the framework and business development, for junior high school levelPHPer
Swoft
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.
Preface
Swoft
It’s a place whereSwoole
A built on top ofHigh Performance Concorde PHP Full Stack Framework
, andSwoole
InPHPer
It is an advanced skill, so it has also caused a lot of trouble to many people in the installation of related environment.Swoft
Even more so, this article will passDocker
The deployment of the operating environment and development environment is solved in an extremely simple way.
Docker
As can be seen from the encyclopedia,Docker
是一个开源的的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的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 popularLinux
On the machine, the Linux machine referred to here is actually not accurate, thanks toDocker for Windows
Projects andHyper-V
The development of,Docker
It can also run on Windows 10 system in a good state, but the author does not recommend toDocker for Windows
Used in production environment.
Docker noun concept
Right hereDocker
Some commonly used nouns are simply explained and explained so that beginners can understand the following
-
Dockerfile
,Dockerfile
YesDocker mirror
The description of the file, throughdocker build
Command to build intomirror image
- Mirror image
Image
), throughDockerfile
Built, including operating system and operating environment - Container (
Container
), the container is a running mirror, can be understood as a mirror isDocker
The construction and packaging phase in the life cycle, while the container is the startup and execution phase. - Mirror warehouse (
Repository
), used to store the builtDocker mirror
The warehouse, can be understood as similar toGit
The warehouse of
Install doc
Docker
The installation process of is not complicated, which will be described in this section.Linux
AndWindows 10
The installation process under the system, andMac
On the system andNot recommended
UseDocker
Environment to run or developSwoft
Project, because inMac for Docker
The performance of shared disks on is extremely poor, resulting inSwoft
The start-up phase takes a long time.
InLinux
Upper installationDocker
Anddocker-compose
InLinux
Pass onyum
Andapt-get
To installDocker
The process is quite simple
CentOS:yum install docker -y
Ubuntu:apt-get install docker-engine -y
According to the difference of the system, the above command can be executed at the terminal.Docker
After the installation of, we need to perform itservice docker start
Command to start itDocker
Services.
After installationDocker
After that, we still need to install itdocker-compose
So as to facilitate the subsequent use of Docker.
CentOS: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
According to the difference of the system, the above command can be executed at the terminal.docker-compose
The installation of.
InWindows 10
Upper installationDocker
Anddocker-compose
We will go directly toDocker.comDownload the corresponding installation packagehttps://store.docker.com/edit …, non-login users we will seePlease 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 Docker
Download, 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 …AttentionHyper-V
Yes andVMware
Is a conflict, the two cannot coexist, can only choose one, if you have to use virtual machines, such asVagrant
Tools like this can also be run in a virtual machine.Linux system
And then according to this article aboutLinux system
The installation process of is processed and runs in the virtual machine.Docker
As a development environment.
The latest editionDocker
The installation package already containsdocker-compose
, there is no need to do extra operations.
After the installation is completed, restart the computer when you see theThe little whale
ShowDocker is running
That is to sayDocker
The launch was successful.
We need to right-clickDocker
, clickSign in / Create Docker ID
Sign in to what we just registeredDocker ID
In 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-clickDocker
And clickSettings
, set the interface to switch toShared Drives
, check your project codeDisk drive letter
, and click on the lower right cornerApply
The authorization can be completed.
Swoft development environment
Modify the official defaultdocker-compose.yml
File
We passed the ordergit clone https://github.com/swoft-cloud/swoft
FromGithub
onClone
SwoftProject, and use the project’s owndocker-compose.yml
File to implement an environment for development,docker-compose.yml
Yesdocker-compose
We 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, onlyswoft
A service is not associated with too much content, aboutdocker-compose.yml
We 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 usingswoft/swoft
The official image and set the container name asswoft
A that binds the inside of the containerEighty
Port and hostEighty
Ports, settings./
The current directory and the/var/www/swoft
The directory is a shared directory, and the interactive terminal with the container is opened and started when arranging files is started.Swoft
Services.
We can notice that theentrypoint
Configuredphp /var/www/swoft/bin/swoft start
, that is to sayStart Swoft service
But if onlyClone
Project and executiondocker-compose up
To try to startcontainer
If so, we will get a failed result because it has not been implemented yet.composer install
To loadComposer
The dependence and lack ofvendor
Folders andautoload
Such as related files, resulting in incorrect operationSwoft
Example, let’s look at the default layout file settingsstdin_open: true
Andtty: true
Two parameters, respectivelydocker
On command-i
And-t
Two parameters, simple understanding is-i
OpenInput
Functions,-t
Open a connection container insideInteractive terminal
, we can take advantage of these two parameters and arrange theentrypoint
The line should readentrypoint: ["sh"]
To make the container not start directly after startingSwoft
Service, but by us manually throughInteractive terminal
Enter 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 versiondocker-compose.yml
File instance:
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 executedocker-compose up -d
,-d
The meaning is toDaemon Mode
Running is convenient for us in the same oneTerminal (Shell)
Entering the container, we can see after the command is executed.Starting swoft ... done
This means that the container was successfully started.
If you get an error when executing the startup command, it indicates that the host’sEighty
Port is already occupied, changedocker-compose.yml
Within the document80:80
For other unoccupied ports, note the firstEighty
Refers to the port of the host, the secondEighty
It 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
Through implementationdocker ps
The 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
LearnContainer id
Forf22173763374
,Container Name
Forswoft
, we can carry outdocker exec -it f22173763374 bash
Ordocker exec -it swoft bash
viaInteractive terminal
Enter the container.
If an error is reported during executionthe input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'
, available atdocker exec
Increase before commandwinpty
Command resolution, i.e.winpty docker exec -it swoft bash
Operation, development and debugging
InstallationComposer
Dependency and GenerationAutoload
File
viadocker exec
After the command entered the container, we noticed that the contents to the left of the cursor changed toroot@f22173763374:
I.e. has entered the container, whereinf22173763374
For the correspondingContainer id
.
Due toSwoft
Official mirror imageswoft/swoft
The configured working directory is/var/www/swoft
, anddocker-compose.yml
Will againProject current directory
Containers are associated/var/www/swoft
Directory, that is, throughdocker exec
The directory entered is already/var/www/swoft
Directory, that is, project directory, so we can directly execute it.composer install
Command to loadComposer
The dependency and generation ofAutoload
Documents.
Considering the domestic network environment, we are implementingcomposer install
The command can be executed first.composer config -g repo.packagist composer https://packagist.phpcomposer.com
Command configurationComposer China Mirror Source
Accelerating 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
)。
Start upSwoft
Service
Installation completedComposer
Once dependent, it can be executedphp bin/swoft start
Start 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 yoursSwoft
And the launch was successful, we can open the browser to access ithttp://127.0.0.1:80
When you see the picture below, you are done!
If the port on which you bind the host is notEighty
, you can change to the corresponding;
If what the visit saw wasRedis connection failure host=127.0.0.1 port=6379
This indicates a lack ofRedis
Service, the simplest and most direct is directly installed in the current containerRedis Server
Direct executionapt install -y redis-server && service redis-server start
The installation and start-up operations can be completed.
Modify the code and make it effective
Swoft
withPHP-FPM
There will be a little difference in the development under the modePHP-FPM
Under the mode of direct change code content, then access to the corresponding code can get the changed content, becausePHP-FPM
Each request in mode reloads PHP code, whileSwoft
YesPersistent operation
, which means that the code does not need to be reloaded after the service is started, and this mode change can makeSwoft
One 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, inSwoft
Next, you need toRestart Worker
OrRestart service
In order for the changed code to take effect, but thanks toSwoft
TheThermal overload
Function, can automatically check code changes and automaticallyRestart Worker
, we just need to go through the.env
File changesAUTO_RELOAD
Items aretrue
That is, if there is no one in the root directory of the project.env
Files that can be copied directly.env.example
The file is.env
And make corresponding changes, it is only necessary to pay attention to the changeapp
The code in the directory will only beThermal overload
Function overload, change other code will not be overloaded, this is because different code is in different life cycle, onlyWorkerStart
Only then can the loaded code be reloaded, and we will cover the contents of this part later.Swoft life cycle
At that time, we will give further explanation.