search
HomeBackend DevelopmentPHP Tutorial在Mac系统下用Docker搭建Swoole开发环境

作为一款重新定义PHP的开源框架,Swoole让PHP可以应用于更多的场景。

对于一位PHP程序员来说,通过Swoole可以了解以往不曾接触过的编程方法。

众所周知,搭建开发环境其实是很麻烦的一件事,经常会遇到各种各样的问题。所以我用Docker搭建了一套Swoole环境,按照这篇教程,就可以非常简单得开始Swoole之旅。

首先是安装Docker,官网有详细的安装步骤: https://docs.docker.com/mac/step_one/

Mac用户参考上述网址即可。对于其他系统的用户来说,官网都有相应的方法。这里不再细说。

以下的步骤也都是基于Mac系统来进行。

安装完成后,在Launchpad里会看到Docker Quickstart Terminal这个名字的应用,点击后,会打开系统默认的终端,然后等一会儿,就可以看到鲸鱼的画像出现了。

要注意终端里的一条提示信息:

docker is configured to use the default machine with IP 192.168.99.100

记住这个IP地址,我们访问容器时会用到。

为了验证Docker是否运行起来,在打开的终端里输入下面的命令:

docker images

如果Docker没有正确运行,会看到下面的错误提示:

Cannot connect to the Docker daemon. Is the docker daemon running on this host?

确保Docker正确运行后,输入下面的命令,获取swoole镜像:

docker pull koolob/swoole-docker

这个镜像是基于php:5.6-cli的镜像构建,包括swoole的最新版本1.8.4,默认使用了下面的编译参数进行编译:

--enable-async-redis --enable-async-httpclient --enable-openssl --enable-jemalloc

swoole的编译参数说明:http://wiki.swoole.com/wiki/page/437.html

这个镜像的Dockerfile是开源的,有需要搭建自定义环境的朋友可以参考我的Dockerfile文件进行修改:Github

下载镜像后,就可以运行一个容器,进入Swoole环境了。

输入命令:

docker run -t -i koolob/swoole-docker /bin/bash

之后会进入到容器内。

再次输入:

php -r 'echo swoole_version()."\n";'

可以看到输出了1.8.4,也就是当前swoole的版本号。

这样我们就有了一个通过Docker构建的Swoole环境。

接下来,我们可以把代码和环境构建成一个镜像来运行。依然有个示例可以参考:

git clone https://github.com/koolob/swoole-docker-example.git

下载下来后,切换到目录中,直接执行

./build.sh

然后浏览器访问地址:http://192.168.99.100:8080/ 就可以看到结果了,同时在终端中也能看到程序输出的内容。在终端里使用Ctrl+C就可以退出容器。

192.168.99.100这个IP就是上文提到的终端提示的IP

我们运行的是一个非常简单的HTTP服务,代码位于src文件夹下:

$serv = new swoole_http_server("0.0.0.0", 8080);$serv->on('Request', function($request, $response) {    var_dump($request->get);    var_dump($request->post);    var_dump($request->cookie);    var_dump($request->files);    var_dump($request->header);    var_dump($request->server);    $response->cookie("User", "Swoole");    $response->header("X-Server", "Swoole");    $response->end("<h1 id="Hello-Swoole">Hello Swoole!</h1>");});$serv->start();

build.sh脚本做的事就是构建镜像,并启动一个容器。

新构建的镜像也很简单,基于koolob/swoole-docker,开放8080端口,将src目录中的内容复制到镜像内,并在容器运行时,执行php代码。

之后要做的事就是修改src中的代码,执行build.sh脚本,然后测试。

至此,我们的Swoole开发环境搭建完毕。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft