系统相关
首页 > 系统相关> > 在CentOS上用docker搭建LNMP环境

在CentOS上用docker搭建LNMP环境

作者:互联网

1.cat /etc/redhat-release 查看centos版本,7.8

2.yum updated 更新yum

3.安装docker

====================================

  1. 卸载老版本
    $ yum -y remove docker docker-common docker-selinux docker-engine

  2. 安装需要的软件包
    $ yum install -y yum-utils device-mapper-persistent-data lvm2

  3. 设置国内yum源
    $ yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

  4. 查看docker版本
    $ yum list docker-ce --showduplicates|sort -r

  5. 安装
    $ yum install docker-ce-18.03.1.ce -y
    // (yum install docker-ce -y) 如果上面那个安装不了,于是用这个(可能是因为centos8)

  6. 配置docker镜像源

$ mkdir -p /etc/docker/   # 先创建目录,再创建文件 
$ vi /etc/docker/daemon.json 
{ 
"registry-mirrors": ["http://hub-mirror.c.163.com"] 
}
{
    "registry-mirrors" : [
    "https://registry.docker-cn.com",
    "https://docker.mirrors.ustc.edu.cn",
    "http://hub-mirror.c.163.com",
    "https://cr.console.aliyun.com/"
  ]
}
  1. 启动
    $ systemctl start docker

  2. 加入开机自启
    systemctl enable docker
    ====================================

4.1 安装nginx (注意,这里先不用管nginx配置文件的php部分先) 【172.17.0.2】

docker pull nginx
创建nginx配置

mkdir -p /docker/nginx && cd /docker/nginx
vim default.conf
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    root /docker/www/lmrs-2008/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /docker/www/lmrs-2008/public;
    }

    location ~ \.php$ {
        root /docker/www/lmrs-2008/public;
        fastcgi_pass   172.17.0.3:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

使用nginx镜像创建nginx应用容器(注意命令报错的话是 --privileged前面空格的原因)


创建www目录: mkdir -p /docker/www 
docker run -p 80:80 -d --name nginx -v /docker/nginx/default.conf:/etc/nginx/conf.d/default.conf -v /docker/www:/docker/www  --privileged=true nginx

4.2 安装php 【172.17.0.3】

docker pull php:7.4-fpm

A.创建容器

docker run -p 9000:9000 -d --name php -v /docker/www:/docker/www --privileged=true php:7.4-fpm

B.查看IP, nginx的是02,看来是按照容器创建的顺序来的

docker inspect php | grep "IPAddress"
172.17.0.3

4.3 安装mysql 【172.17.0.4】

docker pull mysql

编辑 /etc/my.cnf (这个文件好像是已存在的,现在编辑,注意如果是复制ONT会启动不了容器)

[client]
port=3306
socket=/tmp/mysql.sock

[mysqld]
secure_file_priv=/var/lib/mysql
port=3306
socket=/tmp/mysql.sock
datadir=/usr/local/mysql/data
default_storage_engine = InnoDB
performance_schema_max_table_instances = 400
table_definition_cache = 400
skip-external-locking
key_buffer_size = 32M
max_allowed_packet = 100G
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 4K
read_buffer_size = 768K
read_rnd_buffer_size = 256K
myisam_sort_buffer_size = 8M
thread_cache_size = 16
tmp_table_size = 32M
default_authentication_plugin = mysql_native_password
lower_case_table_names = 1
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

explicit_defaults_for_timestamp = true
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535

log-bin=mysql-bin
binlog_format=mixed
server-id = 1
binlog_expire_logs_seconds = 600000
slow_query_log=1
slow-query-log-file=/usr/local/mysql/data/mysql-slow.log
long_query_time=3
early-plugin-load = ""

innodb_data_home_dir = /usr/local/mysql/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/data
innodb_buffer_pool_size = 128M
innodb_log_file_size = 64M
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 1
innodb_write_io_threads = 1

[mysqldump]
quick
max_allowed_packet = 500M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 768K
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

创建容器(密码是root)

docker run -p 3306:3306 -d --name mysql -v /etc/my.cnf:/etc/mysql/my.cnf --privileged=true -e MYSQL_ROOT_PASSWORD=root mysql

进入容器并新建一个用户

docker exec -it mysql bash
mysql -uroot -proot
create user `starsky`@`%` identified by "root";
grant all on *.* to `starsky`@`%` with grant option;

5. 安装redis 【172.17.0.5】

docker pull redis
创建容器

docker run -p 6379:6379 -d --name redis --privileged=true redis

6. 安装Swoole

1.首先到swoole官网下载压缩包,版本选择4.6.6 [https://github.com/swoole/swoole-src/releases]
暂时放在/docker/www/bak

tar –zxvf swoole-4.6.6.tar
mv swoole-src-4.6.6 swoole

2.先进入php容器创建目录:

mkdir -p /usr/src/php/ext/

3.复制到php容器中:

docker cp /docker/www/bak/swoole php:/usr/src/php/ext/swoole

4.在容器中安装php扩展

cd /usr/src/php/ext/
docker-php-ext-install swoole
php -m, 可以看到已经安装了swoole

7. 在php容器中,安装composer,以及配置composer所需要的环境

1.下载安装脚本,执行安装过程,删除安装脚本。移动到bin目录,完成全局安装

php -r "copy('https://install.phpcomposer.com/installer','composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
mv composer.phar /usr/local/bin/composer

2.设置阿里云镜像

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

3.安装pcntl扩展

cd /usr/src/php/ext
docker-php-ext-install pcntl

4.安装git和zip

sudo apt-get update
sudo apt-get install git
apt-get install zip unzip

5.测试安装个项目(过程有可能有点久,那么可能是少了某个环境所需)

composer create-project --prefer-dist laravel/laravel iproject 7.*

5.1 安装phpredis扩展

curl -L -o /docker/www/bak/redis.tar.gz  https://github.com/phpredis/phpredis/archive/5.3.3.tar.gz
tar -zxvf redis.tar.gz
mv phpredis-5.3.3 /usr/src/php/ext/phpredis
cd /usr/src/php/ext
docker-php-ext-install phpredis
php -m, 可以看到已经安装了redis扩展了

6.安装mysql扩展

docker-php-ext-install pdo pdo_mysql
docker-php-ext-install mysqli
# 安装后记得重启php容器

7.1 在测试项目中安装laravelS扩展

composer require hhxsv5/laravel-s
php artisan laravels publish

也可以在.env 修改工作进程数并重启服务

LARAVELS_LISTEN_IP=0.0.0.0   # 监听的ip
LARAVELS_LISTEN_PORT=5200    # 端口
LARAVELS_WORKER_NUM=4        # 工作进程数

7.1 启动laravelS

php bin/laravels start

到这里基础环境已经安装好了,接下来是lmrs项目的

1.宿主机安装SuperVisor(super歪色),暂时跳过这步,因为比较麻烦,而且不方便调试

2.安装openresty 【172.17.0.6】

用来做,一个基于Nginx与Lua的高性能web平台(感觉当成另一个nginx就行)

1.拉取镜像

docker pull openresty/openresty

2.创建配置文件
宿主机中创建用来映射的目录和配置文件

mkdir -p /docker/openresty/conf
vim /docker/openresty/conf/default.conf

server {
    listen       90;
    listen       [::]:90; #监听所有的ipv6地址
    server_name  localhost;

    root   /docker/www/webserver/;
    index  index.html login.html;
}

3.创建容器,监听90端口,映射了两个目录,其中一个目录和nginx容器的一样

docker run -p 90:90 -d --name openresty -v /docker/openresty/conf/default.conf:/etc/nginx/conf.d/default.conf -v /docker/www:/docker/www --privileged=true openresty/openresty 

标签:www,CentOS,LNMP,nginx,上用,mysql,docker,php,安装
来源: https://www.cnblogs.com/wchqyy/p/16079609.html