Dockerfile创建LNMP平台并上线php项目

实验环境

系统版本:CentOS Linux release 7.6.1810 (Core)x64

Docker版本:18.09.5

关闭防火墙并禁止开机自启

systemctl stop firewalld.service
systemctl disable firewalld

关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

重启 reboot

安装docker

1、安装docker-ce的yum源

//下载yum源到本地

wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

//移动yum源到/etc/yum.repos.d/目录下

mv docker-ce.repo /etc/yum.repos.d/

2、从高到低排列显示docker版本

yum list docker-ce --showduplicates | sort -r

3、安装docker最新版

yum -y install docker-ce .x86_64

注:切记启动docker并设置开机自启!

准备需要的镜像

构建Dockerfile-Mysql镜像

注:因为自己构建的数据库不够简洁所以建议使用官方镜像!

docker run -d --name lnmp_mysql --net lnmp --mount src=mysql-vol,dst=/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=wordpress mysql:5.7 --character-set-server=utf8

注:因为使用的官网镜像简洁包小并且不容易出问题,所以直接下载运行了!

构建Dockerfile-php镜像

1、提前创建好php配置文件

php-fpm.conf php.ini

注:以上两个文件与dockerfile-php文件在同一目录,里边配置信息根据需求改正即可!

2、创建Dockerfile-php文件

cat Dockerfile-php

FROM centos:7
MAINTAINER www.ctnrs.com
RUN yum install epel-release -y && \
yum install -y gcc gcc-c++ make gd-devel libxml2-devel \
libcurl-devel libjpeg-devel libpng-devel openssl-devel \
libmcrypt-devel libxslt-devel libtidy-devel autoconf \
iproute net-tools telnet wget curl && \
yum clean all && \
rm -rf /var/cache/yum/*

RUN wget http://docs.php.net/distributions/php-5.6.36.tar.gz && \
tar zxf php-5.6.36.tar.gz && \
cd php-5.6.36 && \
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm --enable-opcache \
--with-mysql --with-mysqli --with-pdo-mysql \
--with-openssl --with-zlib --with-curl --with-gd \
--with-jpeg-dir --with-png-dir --with-freetype-dir \
--enable-mbstring --with-mcrypt --enable-hash && \
make -j 4 && make install && \
cp php.ini-production /usr/local/php/etc/php.ini && \
cp sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf && \
sed -i "90a \daemonize = no" /usr/local/php/etc/php-fpm.conf && \
mkdir /usr/local/php/log && \
cd / && rm -rf php* && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ENV PATH $PATH:/usr/local/php/sbin
COPY php.ini /usr/local/php/etc/
COPY php-fpm.conf /usr/local/php/etc/
WORKDIR /usr/local/php
EXPOSE 9000
CMD ["php-fpm"]

3、生成php镜像

docker build -t hph:v1 -f Dockerfile-php .



注:dockerfile构建的镜像在运行时需要添加一些参数,例如:内存、cpu、挂载目录、自定义网络卷等等。

构建Dockerfile-Nginx镜像

1、创建dockerfile-nginx配置文件

nginx.conf

注:以上两个文件与dockerfile-nginx文件在同一目录,里边配置信息根据需求改正即可!

2、创建Dockerfile-Nginx文件

cat Dockerfile-nginx

FROM centos:7
MAINTAINER www.ctnrs.com
RUN yum install -y gcc gcc-c++ make \
openssl-devel pcre-devel gd-devel \
iproute net-tools telnet wget curl && \
yum clean all && \
rm -rf /var/cache/yum/
RUN wget http://nginx.org/download/nginx-1.15.5.tar.gz && \
tar zxf nginx-1.15.5.tar.gz && \
cd nginx-1.15.5 && \
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module && \
make -j 4 && make install && \
rm -rf /usr/local/nginx/html/
&& \
echo "ok" >> /usr/local/nginx/html/status.html && \
cd / && rm -rf nginx-1.12.2* && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/nginx/sbin
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
WORKDIR /usr/local/nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

3、生成nginx镜像

docker build -t nginx:v1 -f Dockerfile-nginx .



注:dockerfile构建的镜像在运行时需要添加一些参数,例如:内存、cpu、挂载目录、自定义网络卷等等。

创建docker网络

1、创建lnmp自定义网络

docker network create lnmp

注:创建自定义网络是为了实现项目之间的隔离!

2、创建mysql容器

docker run -d --name lnmp_mysql --net lnmp --mount src=mysql-vol,dst=/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=wordpress mysql:5.7 --character-set-server=utf8

注:将已有镜像mysql:5.7指定以上参数即可!

3、创建PHP容器

docker run -d --name lnmp_php --net lnmp --mount src=wwwroot,dst=/wwwroot php:v1

注:根据已有镜像php:v1指定以上参数运行!

4、创建Nginx容器

docker run -d --name lnmp_nginx --net lnmp -p 88:80 --mount src=wwwroot,dst=/wwwroot nginx:v1

注:根据已有镜像nginx:v1指定以上参数运行!

5、以wordpress博客为例

wordpress官网地址:https://wordpress.org

下载最新版本:https://wordpress.org/latest.zip

LNMP平台上线wordpress项目

1、切换到存放php源码的目录

cd /var/lib/docker/volumes/wwwroot/_data/

注:默认情况下目录为空,所以需要将下载好的wordpress源码放进去!

2、上传wordpress源码包到指定目录

wordpress-5.2.1.zip

3、解压源码包

unzip wordpress-5.2.1.zip

4、将目录里的文件放到当前_data目录

mv wordpress/* ./

注:与压缩包和解压包在同一目录!

访问测试

浏览器访问地址:http://192.168.152.170:88/

点击let's go

输入账号密码

点击Submit

注:按照提示操作在/var/lib/docker/volumes/wwwroot/_data/目录下创建wp-config.php文件,并将以上截图(文件内容如下
cat /var/lib/docker/volumes/wwwroot/_data/wp-config.php)文中代码复制到文件中,然后保存退出

/**

  • The base configuration for WordPress
  • The wp-config.php creation script uses this file during the
  • installation. You don't have to use the web site, you can
  • copy this file to "wp-config.php" and fill in the values.
  • This file contains the following configurations:
    • MySQL settings
    • Secret keys
    • Database table prefix
    • ABSPATH
  • @link https://codex.wordpress.org/Editing_wp-config.php
  • @package WordPress
    */

// MySQL settings - You can get this info from your web host //
/* The name of the database for WordPress /
define( 'DB_NAME', 'wordpress' );

/* MySQL database username /
define( 'DB_USER', 'root' );

/* MySQL database password /
define( 'DB_PASSWORD', '123456' );

/* MySQL hostname /
define( 'DB_HOST', 'lnmp_mysql' );

/* Database Charset to use in creating database tables. /
define( 'DB_CHARSET', 'utf8mb4' );

/* The Database Collate type. Don't change this if in doubt. /
define( 'DB_COLLATE', '' );

/**#@+

  • Authentication Unique Keys and Salts.
  • Change these to different unique phrases!
  • You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
  • You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
  • @since 2.6.0
    /
    define( 'AUTH_KEY', '/<~(pJcLvo;v8_rTpijkKuFt ;I2?[/Pq;%%K3e=DAN' );
    define( 'SECURE_AUTH_KEY', 'wk>0|gw9$A7HIs20Z5$MQ~q +HEs)D0E1/+
    m[]L=pG:#dd_Z,3p!8V?vOc^Bb.Y' );
    define( 'LOGGED_IN_KEY', 'ACxnr^hx wR?:AVqpb^L)7KD]Y0NW;O>S#&vfqUT:(z9UaV$!z0%86b.:hj(]' );
    define( 'NONCE_KEY', 'Fc>EO$mS%}3E@:g6y0TeOGXdefine( 'AUTHSALT', '[?N]%z|M0hc7gMpFfJd7ZiAyExLj[{v!@C536{BOeR+IY?Qk-cOK|/a)=,MhL5,' );
    define( 'SECURE_AUTH_SALT', 'OuM!8Fz2<)Vs(@DI}_IcsFx4m?{!4FsXp6E3TS=&+sR2/2N}`>I%JbsR]|qX5#[I' );
    define( 'LOGGED_IN_SALT', 'v>MBmN%n;%oS?qw#|10vTn]~n1Y0Q28+[e
    TrT~3h|TV5f;Mg]SlB_DVYQuGuaXQ' );
    define( 'NONCESALT', 'd*|^;neW-7DB&XTAc$-f-=b>wL~G,N0HOa}wotO}Sru}41Zb2Cec VJa@X

/*#@-/

/**

  • WordPress Database Table prefix.
  • You can have multiple installations in one database if you give each
  • a unique prefix. Only numbers, letters, and underscores please!
    */
    $tableprefix = 'wp';

/**

  • For developers: WordPress debugging mode.
  • Change this to true to enable the display of notices during development.
  • It is strongly recommended that plugin and theme developers use WP_DEBUG
  • in their development environments.
  • For information on other constants that can be used for debugging,
  • visit the Codex.
  • @link https://codex.wordpress.org/Debugging_in_WordPress
    */
    define( 'WP_DEBUG', false );

/ That's all, stop editing! Happy publishing. /

/* Absolute path to the WordPress directory. /
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', dirname( FILE ) . '/' );
}

/* Sets up WordPress vars and included files. /
require_once( ABSPATH . 'wp-settings.php' );

点击Run the installation

点击Install WordPress

点击Log In


注:输入账号密码登陆网页后台!

继续点击Log In登陆

注:以上通过lnmp平台上线php项目完成!

转载于:https://blog.51cto.com/13043516/2386556

你可能感兴趣的:(php,运维,数据库)