前提:
安装好docker,并且修改了镜像加速,启动docker
一、安装lnmp
1. 拉取镜像,haveyb/lnmp 集成 Linux-centos7.6、Nginx、MySQL5.7、PHP7.3、Redis 的镜像
docker pull haveyb/lnmp
2. 创建容器
小插曲:
因为自己一开始安装途中会报错:
Failed to get D-Bus connection: No such file or directory
原因是开启privileged,但是设置这个参数并没有生效,所以可以先解决这个问题:
1). 前往访达资源库,我的地址为/Users/xxx/Library/Group Containers/group.com.docker
2). 找到settings.json
3). 把deprecatedCgroupv1的false改成true:
"deprecatedCgroupv1": true,
4). 重启docker
开始创建容器,将宿主机的 /Users/xxx/Desktop/docker 与 容器内的 /data 目录同步,并且将 nginx 的80端口,redis 的 6379 端口、mysql 的3306 端口都与 mac 系统的端口做了映射:
docker run -itd --name lnmp7.4 --privileged -p 6379:6379 -p 80:80 -p 3307:3306 -v /Users/xxx/Desktop/docker:/data haveyb/lnmp /usr/sbin/init
参数说明:
-p:指定 mac 和容器内的端口映射,比如,-p 3307:3306,表示连接mac 的3307 端口,实际上就会请求到容器内的3306端口
-v:指定目录共享
--privileged:赋予特殊权限
/usr/sbin/init:可以使用 systemctl 等命令
3. 进入容器
docker exec -it lnmp7.4 bash
4. 查看php版本
php -v
PHP 7.3.16
二、php7.3升级为php7.4
lnmp已经搭建好了,不过我项目要用的是php7.4,所以接下来是升级,不需要升级的可以跳过这块
编译安装libzip包(centos7自带的是0.10版本,编译环境需要0.10版本以上但不包含1.3和1.7版本的包)
yum remove libzip libzip-devel -y
wget https://libzip.org/download/libzip-1.2.0.tar.gz
tar -zxvf libzip-1.2.0.tar.gz
cd libzip-1.2.0
./configure
make -j4 && make install
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"
先安装需要的依赖包
yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses curl gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel expat-devel xmlrpc-c xmlrpc-c-devel libicu-devel libmcrypt-devel libmemcached-devel libsqlite3x-devel oniguruma-devel
先备份
mv /usr/local/php /usr/local/php-7.3.16
下载新的版本https://www.php.net/distributions/php-7.4.33.tar.bz2,我放在共享目录下
cp /data/php-7.4.33.tar.bz2 /usr/local/src/php-7.4.33.tar.bz2
cd /usr/local/src
解压
tar -xjvf php-7.4.33.tar.bz2
cd php-7.4.33
停止php-fpm
systemctl stop php-fpm
编译并且安装,这个是简易版的,少了很多扩展,先别急着安装,请接着往下看
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm
编译并且安装,开启相应扩展,需要的扩展都装上,后续不用追加,省心省力(强烈推荐这种)
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --enable-opcache --enable-inline-optimization --disable-debug --enable-shared -enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mysqlnd-compression-support --with-zlib --enable-xml --enable-bcmath --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbstring --enable-intl --enable-ftp --enable-gd --enable-gd-jis-conv --with-jpeg --with-freetype --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-zip --enable-soap --with-gettext --enable-fileinfo --with-pear
PASS:
1)make编译过程出现(在make之前还是先进行这步操作吧,不然等make很长一段时间再报错,会很奔溃)
undefined reference to `libiconv' 错误的解决方法
在执行完 ./configure … 之后,修改下 Makefile,找到其中的EXTRA_LIBS,在最后面添加 -liconv,修改后如下:
EXTRA_LIBS = ......这是本来就有的一大段,需要保留..... -liconv
2) 如果编译出现
configure: error: Cannot find ldap.h
yum install openldap openldap-devel -y
编译好长一段时间
make && make install
复制默认配置文件,从备份的php-7.3.16里面复制就行
cp /usr/local/php-7.3.16/etc/php.ini /usr/local/php/etc/php.ini
cp /usr/local/php-7.3.16/etc/php-fpm.conf /usr/local/php/etc/php-fpm.conf
cp /usr/local/php-7.3.16/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
启动php-fpm
systemctl start php-fpm
查看版本
php -v
PHP 7.4.33
显示加载yar.so、amqp.so扩展失败
vi /usr/local/php/etc/php.ini ,两个扩展用不上,extension前面加上分号注释掉
;extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/yar.so
;extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/amqp.so
三、安装php扩展(安装自己需要的扩展即可)
我的扩展都是本地下载好,放到共享目录/Users/xxx/Desktop/docker里面,拷贝到/usr/local/src目录进行源码编译安装的,源码安装一般步骤都是phpize、./configure、make、make install,编译安装php7.4的时候,最好带上自己需要的扩展,就没后续这么多麻烦事了,如果后续发现少了什么扩展的话,也可以补充上去
3.1安装swoole扩展
cp -r /data/swoole-4.8.12 /usr/local/src
cd /usr/local/src/swoole-4.8.12
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
vi /usr/local/php/etc/php.ini
加上扩展
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/swoole.so
查看安装成功的扩展
php -m | grep swoole
返回
swoole
3.2 安装swoole-loader扩展
cp /data/slyp_shop/install/swoole-loader/swoole_loader74.so /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/swoole_loader74.so
vi /usr/local/php/etc/php.ini
加上扩展
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/swoole_loader74.so
查看安装成功的扩展
php -m | grep swoole
返回
swoole
swoole_loader
3.3 安装redis扩展
cp /data/redis-5.3.7.tgz /usr/local/src/redis-5.3.7.tgz
cd /usr/local/src
tar zxvf redis-5.3.7.tgz
cd redis-5.3.7
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
vi /usr/local/php/etc/php.ini
加上:
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/redis.so
php -m | grep redis
返回:redis
最后,重启一下php
systemctl restart php-fpm
四、配置虚拟域名
编辑mac的hosts文件
sudo vim /etc/hosts
127.0.0.1 dev_docker.test.com
回到容器内,切换到nginx配置目录(conf.d 目录下有配置例子,可以复制修改)
cd /usr/local/nginx/conf.d
vi dev_docker.test.com.conf
server {
listen 80;
server_name dev_docker.test.com;
client_max_body_size 128M;
charset utf-8;
error_log /usr/local/nginx/logs/error/dev_docker.test.com.log error;
access_log /usr/local/nginx/logs/access/dev_docker.test.com.log main;
root /data/test;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ ^/assets/.*\.php$ {
deny all;
}
location /favicon.ico {
log_not_found off;
access_log off;
}
error_page 404 403 500 502 503 504 /404.html;
location = /404.html {
root /data/errorPage;
}
location ~ \.php$ {
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
cd /data
mkdir test
vi index.php
加入:
<?php
phpinfo();
重启nginx
systemctl restart nginx
浏览器访问:
http://dev_docker.test.com/index.php
![image.png](https://upload-images.jianshu.io/upload_images/1638771-6117d29148c1bd5f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
五、跑crmeb项目(本人项目需要,可以跳过)
到这里已经基本完成了,因为我的项目是crmeb二开,需要往下继续操作,我配置的项目域名是dev_docker.slyp_shop.com,在mac系统hosts配置域名,同上面第四点,这里不赘述
crmeb需要配置反向代理,这里贴一下代码
cd /usr/local/nginx/conf.d
vi dev_docker.slyp_shop.com.conf
server {
listen 80;
server_name dev_docker.slyp_shop.com;
client_max_body_size 128M;
charset utf-8;
error_log /usr/local/nginx/logs/error/dev_docker.slyp_shop.com.log error;
access_log /usr/local/nginx/logs/access/dev_docker.slyp_shop.com.log main;
root /data/slyp_shop;
index index.html index.htm index.php;
location ^~ / {
proxy_pass http://127.0.0.1:8326;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
# proxy_hide_header Upgrade;
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
set $static_filebUUocfuv 0;
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
{
set $static_filebUUocfuv 1;
expires 1m;
}
if ( $static_filebUUocfuv = 0 )
{
add_header Cache-Control no-cache;
}
}
}
重启nginx
systemctl restart nginx
启动项目
systemctl start redis
cd /data/slyp_shop
php think swoole start
浏览器访问:
http://dev_docker.slyp_shop.com/
开启proc_open
vi /usr/local/php/etc/php.ini
在php.ini中,找到disable_functions选项,看看是否有proc_open函数被禁用了,如果有的话,去掉即可
systemctl restart php-fpm
mysql设置sql_mode
vi /etc/my.cnf
[mysqld]
sql_mode=NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
systemctl restart mysql
到这里就搭建成功了!
六、一些说明
php管理
systemctl start/stop/restart/reload php-fpm
配置文件:/usr/local/php/etc/php.ini
nginx管理
systemctl start/stop/restart/reload nginx
站点配置文件:/usr/local/nginx/conf.d
mysql管理
systemctl status/start/stop/restart/reload mysql
配置文件: /etc/my.cnf
初始密码:123456
redis管理
systemctl start/stop/restart/reload redis
配置文件:/etc/redis.conf
密码:haveyb
navicat连接mysql,上面配置的端口是3307,注意端口别搞错
点击测试连接返回:is not allowed to connect to this mysql server
出现这种原因是mysql设置了不允许远程连接,进入容器
docker exec -it lnmp7.4 bash
mysql -u root -p 密码(默认123456)
use mysql;
select host from user where user='root';
update user set host = '%' where user ='root';
flush privileges;
exit; 退出mysql