Dockerfile建立LNMP平臺並上線php項目

實驗環境

系統版本:CentOS Linux release 7.6.1810 (Core)x64php

Docker版本:18.09.5html

關閉防火牆並禁止開機自啓mysql

systemctl stop firewalld.service
systemctl disable firewalldlinux

關閉selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinuxnginx

重啓 rebootc++

安裝docker

一、安裝docker-ce的yum源

//下載yum源到本地web

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

//移動yum源到/etc/yum.repos.d/目錄下docker

mv docker-ce.repo /etc/yum.repos.d/數據庫

二、從高到低排列顯示docker版本

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

三、安裝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建立LNMP平臺並上線php項目
注:由於使用的官網鏡像簡潔包小而且不容易出問題,因此直接下載運行了!

構建Dockerfile-php鏡像

一、提早建立好php配置文件

php-fpm.conf php.ini

注:以上兩個文件與dockerfile-php文件在同一目錄,裏邊配置信息根據需求改正便可!

二、建立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"]

三、生成php鏡像

docker build -t hph:v1 -f Dockerfile-php .
Dockerfile建立LNMP平臺並上線php項目
Dockerfile建立LNMP平臺並上線php項目
Dockerfile建立LNMP平臺並上線php項目
注:dockerfile構建的鏡像在運行時須要添加一些參數,例如:內存、cpu、掛載目錄、自定義網絡卷等等。

構建Dockerfile-Nginx鏡像

一、建立dockerfile-nginx配置文件

nginx.conf

注:以上兩個文件與dockerfile-nginx文件在同一目錄,裏邊配置信息根據需求改正便可!

二、建立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;"]

三、生成nginx鏡像

docker build -t nginx:v1 -f Dockerfile-nginx .
Dockerfile建立LNMP平臺並上線php項目
Dockerfile建立LNMP平臺並上線php項目
Dockerfile建立LNMP平臺並上線php項目
注:dockerfile構建的鏡像在運行時須要添加一些參數,例如:內存、cpu、掛載目錄、自定義網絡卷等等。

建立docker網絡

一、建立lnmp自定義網絡

docker network create lnmp

注:建立自定義網絡是爲了實現項目之間的隔離!

二、建立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指定以上參數便可!

三、建立PHP容器

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

注:根據已有鏡像php:v1指定以上參數運行!

四、建立Nginx容器

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

注:根據已有鏡像nginx:v1指定以上參數運行!

五、以wordpress博客爲例

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

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

LNMP平臺上線wordpress項目

一、切換到存放php源碼的目錄

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

注:默認狀況下目錄爲空,因此須要將下載好的wordpress源碼放進去!

二、上傳wordpress源碼包到指定目錄

wordpress-5.2.1.zip

三、解壓源碼包

unzip wordpress-5.2.1.zip

四、將目錄裏的文件放到當前_data目錄

mv wordpress/* ./
Dockerfile建立LNMP平臺並上線php項目

注:與壓縮包和解壓包在同一目錄!

訪問測試

瀏覽器訪問地址:http://192.168.152.170:88/

Dockerfile建立LNMP平臺並上線php項目

點擊let's go

Dockerfile建立LNMP平臺並上線php項目

輸入帳號密碼

Dockerfile建立LNMP平臺並上線php項目

點擊Submit

Dockerfile建立LNMP平臺並上線php項目

注:按照提示操做在/var/lib/docker/volumes/wwwroot/_data/目錄下建立wp-config.php文件,並將以上截圖(文件內容以下
cat /var/lib/docker/volumes/wwwroot/_data/wp-config.php)文中代碼複製到文件中,而後保存退出

<?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', '/<~(pJc<D}ini_cV)[1|S&~5:E >Lvo;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@:g6y0TeOGX<m6S3gtS<]v;k}uV40z)X^WN~ixQWTghPZOsFW?lz' );
    define( '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<S{P' );

/*#@-/

/**

  • 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

Dockerfile建立LNMP平臺並上線php項目

點擊Install WordPress

Dockerfile建立LNMP平臺並上線php項目

點擊Log In

Dockerfile建立LNMP平臺並上線php項目
注:輸入帳號密碼登錄網頁後臺!

繼續點擊Log In登錄

Dockerfile建立LNMP平臺並上線php項目

注:以上經過lnmp平臺上線php項目完成!

相關文章
相關標籤/搜索