# 搭建环境 >[danger]SD 已经停止维护,推荐使用ESD项目[ESD](https://www.kancloud.cn/tmtbe/goswoole/1067764) ## 自动安装 SD提供提供了运行环境的一键安装部署,可用于全新系统的环境搭建。 环境包括PHP,各类扩展,SD框架。 执行下面命令将安装SD3.X版本,包括所有环境 ``` curl -sS sd.youwoxing.net/install_v3 | bash ``` 运行后,喝杯茶静等安装完成。 如果你已经安装了PHP,有可能会出现安装错误,请移除你安装的PHP版本再执行上面的命令。 该命令已在ubuntu,centos,deepin系统中验证有效。 上面的命令会自动生成SD的项目文件夹,可以修改composer.json文件增加自己的依赖或者修改SD的版本。 composer.json: ```json { "require": { "tmtbe/swooledistributed":">3.0.0" }, "autoload": { "psr-4": { "app\\": "src/app", "test\\": "src/test" } } } ``` 通过composer进行依赖代码更新,运行: ``` composer update ``` ## 手动安装 1. 安装PHP7.1.14版本,包含bcmath,mbstring,pdo_mysql,posix扩展。 2. 安装swoole,inotify,ds,redis,hiredis 使用下面的安装脚步安装,可以通过修改版本号安装不同版本。 ``` #!/bin/sh #sd安装脚本 set -e pwd=`pwd` hiredis_version="0.13.3" phpredis_version="3.1.6" phpds_version="1.2.4" phpinotify_version="2.0.0" swoole_version="2.1.1" sd_version="3.0.6" swoole_configure="--enable-async-redis --enable-openssl --enable-coroutine" do_install_ex() { php_m=`php -m` if ! [[ $php_m =~ "bcmath" ]] ; then echo "缺少必要bcmath扩展,请安装或者卸载php版本后重新运行本命令" exit fi if ! [[ $php_m =~ "mbstring" ]] ; then echo "缺少必要mbstring扩展,请安装或者卸载php版本后重新运行本命令" exit fi if ! [[ $php_m =~ "pdo_mysql" ]] ; then echo "缺少必要pdo_mysql扩展,请安装或者卸载php版本后重新运行本命令" exit fi if ! [[ $php_m =~ "posix" ]] ; then echo "缺少必要posix扩展,请安装或者卸载php版本后重新运行本命令" exit fi do_install_swoole if ! [[ $php_m =~ "redis" ]] ; then do_install_redis fi if ! [[ $php_m =~ "ds" ]] ; then do_install_ds fi if ! [[ $php_m =~ "inotify" ]] ; then do_install_inotify fi } do_install_inotify() { cd ${pwd}; echo "[inotify] 开始下载" if [ -f "${phpinotify_version}.tar.gz" ] ;then rm -f ${phpinotify_version}.tar.gz fi wget https://github.com/arnaud-lb/php-inotify/archive/${phpinotify_version}.tar.gz if [ -d "php-inotify-${phpinotify_version}" ] ;then rm -rf php-inotify-${phpinotify_version} fi tar -xzvf ${phpinotify_version}.tar.gz > /dev/null echo "[inotify] 开始编译" cd ${pwd}/php-inotify-${phpinotify_version} phpize ./configure make clean > /dev/null make -j make install echo "extension=inotify.so" | tee ${init_file}/inotify.ini rm -f ${pwd}/${phpinotify_version}.tar.gz rm -rf ${pwd}/php-inotify-${phpinotify_version} } do_install_ds() { cd ${pwd}; echo "[ds] 开始下载" if [ -f "v${phpds_version}.tar.gz" ] ;then rm -f v${phpds_version}.tar.gz fi wget https://github.com/php-ds/extension/archive/v${phpds_version}.tar.gz if [ -d "extension-${phpredis_version}" ] ;then rm -rf extension-${phpredis_version} fi tar -xzvf v${phpds_version}.tar.gz > /dev/null echo "[ds] 开始编译" cd ${pwd}/extension-${phpds_version} phpize ./configure make clean > /dev/null make -j make install echo "extension=ds.so" | tee ${init_file}/ds.ini rm -f ${pwd}/v${phpds_version}.tar.gz rm -rf ${pwd}/extension-${phpredis_version} } do_install_redis() { cd ${pwd}; echo "[redis] 开始下载" if [ -f "${phpredis_version}.tar.gz" ] ;then rm -f ${phpredis_version}.tar.gz fi wget https://github.com/phpredis/phpredis/archive/${phpredis_version}.tar.gz if [ -d "redis-${phpredis_version}" ] ;then rm -rf redis-${phpredis_version} fi tar -xzvf ${phpredis_version}.tar.gz > /dev/null echo "[redis] 开始编译" cd ${pwd}/phpredis-${phpredis_version} phpize ./configure make clean > /dev/null make -j make install echo "extension=redis.so" | tee ${init_file}/redis.ini rm -f ${pwd}/${phpredis_version}.tar.gz rm -rf ${pwd}/redis-${phpredis_version} } do_install_swoole() { cd ${pwd}; echo "[swoole] 开始下载" if [ -f "v${swoole_version}.tar.gz" ] ;then rm -f v${swoole_version}.tar.gz fi if [ -f "v${hiredis_version}.tar.gz" ] ;then rm -f v${hiredis_version}.tar.gz fi wget https://github.com/swoole/swoole-src/archive/v${swoole_version}.tar.gz https://github.com/redis/hiredis/archive/v${hiredis_version}.tar.gz if [ -d "swoole-src-${swoole_version}" ] ;then rm -rf swoole-src-${swoole_version} fi if [ -d "hiredis-${hiredis_version}" ] ;then rm -rf hiredis-${hiredis_version} fi tar -xzvf v${swoole_version}.tar.gz > /dev/null tar -xzvf v${hiredis_version}.tar.gz > /dev/null echo "[swoole] 开始编译" cd ${pwd}/hiredis-${hiredis_version} make clean > /dev/null make -j make install ldconfig cd ${pwd}/swoole-src-${swoole_version} phpize ./configure ${swoole_configure} make clean > /dev/null make -j make install echo "extension=swoole.so" | tee ${init_file}/swoole.ini rm -f ${pwd}/v${swoole_version}.tar.gz rm -f ${pwd}/v${hiredis_version}.tar.gz rm -rf ${pwd}/swoole-src-${swoole_version} rm -rf ${pwd}/hiredis-${hiredis_version} } do_install_sd(){ echo "[SD] 开始安装" cd ${pwd} mkdir sd cd sd echo "{\"require\": {\"tmtbe/swooledistributed\":\"=${sd_version}\"},\"autoload\": {\"psr-4\": {\"app\\\\\": \"src/app\",\"test\\\\\": \"src/test\"}}}" > composer.json composer update php vendor/tmtbe/swooledistributed/src/Install.php -y echo "框架安装完毕,项目目录:${pwd}/sd" } do_install_composer(){ if ! command_exists composer ; then cd ${pwd} echo "[composer] 开始下载" curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer fi composer config -g repo.packagist composer https://packagist.phpcomposer.com } set_php() { init_file=`echo '<?php $command=$argv[1]??null;ob_start();phpinfo(INFO_GENERAL);$result=ob_get_contents();ob_clean();$ini_files="";$info=explode("\n\n",$result)[1];$info2=explode("\n",$info);foreach($info2 as $value){$info3=explode("=>",$value);if($info3[0]=="Scan this dir for additional .ini files "){$ini_files = trim($info3[1]);break;}}echo $ini_files;' | php` } set_ldconf() { echo "include /etc/ld.so.conf.d/*.conf" | tee /etc/ld.so.conf if ! [ -d "/etc/ld.so.conf.d" ] ;then mkdir /etc/ld.so.conf.d fi cd /etc/ld.so.conf.d echo "/usr/local/lib" | tee /etc/ld.so.conf.d/libc.conf ldconfig -v } do_install(){ set_ldconf php_version=`php -v` if ! [[ $php_version =~ "PHP 7" ]] ; then echo "PHP版本不对,请卸载php后重新运行此命令" fi set_php do_install_ex do_install_composer do_install_sd } do_install ```