搭建Wordpress
下载项目包
#下载worldpress官网最新安装包
[root@docker1 opt]# wget https://wordpress.org/latest.zip
#解压压缩包
[root@docker1 opt]# unzip latest.zip
docker虚拟机上
nginx配置
#建立一个网桥
[root@docker1 ~]# docker network create lnmp
#创建三个文件夹
[root@docker1 ~]# mkdir {nginx,php,mysql}
#切换目录
[root@docker1 ~]# cd nginx/
#编辑nginx配置文件
[root@docker1 nginx]# vim nginx.conf
user www;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
include /etc/nginx/conf.d/*.conf;
}
#编辑worldpress站点目录文件
[root@docker1 nginx]# vim default.conf
server {
listen 80;
root /usr/share/nginx/html/;
index index.html index.htm index.php;
location / {
if ( -f $request_filename) {
break;
}
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
try_files $uri $uri/ /index.php$uri?$query_string;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php($|/) {
set $script $uri;
set $path_info "";
if ($uri ~ "^(.+\.php)(/.+)") {
set $script $1;
set $path_info $2;
}
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_NAME $script;
try_files $uri =404;
}
}
[root@docker1 nginx]# vim nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
module_hotfixes=true
#编辑nginx Dockerfile
[root@docker1 nginx]# vim Dockerfile
FROM centos:7
ADD default.conf /etc/nginx/conf.d/
ADD nginx.conf /etc/nginx
ADD nginx.repo /etc/yum.repos.d/nginx.repo
RUN useradd www
RUN yum install -y nginx
EXPOSE 80
WORKDIR /root
CMD nginx -g "daemon off;"
#利用Dockerfile 构建镜像
[root@docker1 nginx]# docker build -t nginx .
#开启ipv4地址转发
[root@docker1 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
#重启网络
[root@docker1 ~]# systemctl restart network
#查看ip转发是否配置成功
[root@docker1 ~]# sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1
#启动容器(容器名为nginx,绑定端口映射,指定网桥,指定挂载目录,开辟一个窗口)
[root@docker1 opt]# docker run -it --name nginx -p 80:80 --network=lnmp -v /opt/wordpress/:/usr/share/nginx/html nginx:latest bash
PHP配置
#切换目录
[root@docker1 nginx]# cd …/php/
#编辑php配置文件
[root@docker1 php]# vim www.conf
[www]
user = www
group = www
listen = 9000
request_terminate_timeout = 0
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache
#创建PHP.repo
[root@docker1 php]# vim php.repo
[php-webtatic]
name = PHP Repo
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0
enable = 1
#编辑php Dockerfile
[root@docker1 php]# vim Dockerfile
FROM centos:7
ADD www.conf /etc/php-fpm.d/www.conf
ADD php.repo /etc/yum.repos.d/php.repo
RUN useradd www
RUN yum install -y php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb php71w-bcmath --skip-broken
EXPOSE 9000
CMD ["php-fpm "-F""]
#构建php镜像
[root@docker1 php]# docker build -t php:v1 .
#启动容器(容器名为php,指定网桥,指定挂载目录,开辟一个窗口)
[root@docker1 php]# docker run -it --name php --network=lnmp -v /opt/wordpress/:/usr/share/nginx/html php:v1 bash
mysql配置
#启动容器(容器名为mysql,指定网桥,创建密码))
[root@docker1 opt]# docker run -d -e MYSQL_ROOT_PASSWORD=123 --network=lnmp --name mysql mysql:5.7
#进入mysql容器
[root@docker1 ~]# docker exec -it mysql bash
#进入数据库
root@9669bf1d0d99:/# mysql -uroot -p
Enter password: 123
#创建数据库
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
nginx容器内
#创建应用所需文件
[root@cdbddab2ad90 html]# cd/usr/share/nginx/html
[root@cdbddab2ad90 html]# vi wp-config.php
<?php
/**
* The base configuration for WordPress
*
* The wp-config.php creation script uses this file during the
*
* This file contains the following configurations:
*
* * MySQL settings
* * Secret keys
* * Database table prefix
* * ABSPATH
*
* @link https://wordpress.org/support/article/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', '123' );
/** MySQL hostname */
define( 'DB_HOST', '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}
*
* @since 2.6.0
*/
define( 'AUTH_KEY', '|D-gm:iMGF5(HcH6:#N;nl<8n!lE.``P${q1p|/TM<6z~GK6*n9UyAPzu~a8, U}' );
define( 'SECURE_AUTH_KEY', '/+y}hVin#l0B;F+-juHh-E=5{$9ItrqVzqp!hI8X_2+-rI.[nZNcrfH!qZ*.<!Bf' );
define( 'LOGGED_IN_KEY', 'd@G|3N)R]c5TaeK,{9A?NI=AA]ps1HYhkBAFKJk9:Zn%c~$PQtki]]!}89a|^P6L' );
define( 'NONCE_KEY', 'giY3odJ_qn6^L4 T-xx@JISdy1l;eZn?}ZA~a+TE^gvc]0iU4s!Z-|Mk?n3iBKKu' );
define( 'AUTH_SALT', '=(2f8x;^-6fTEpg,X0_Wm16n@b$Zu]-Q$G7YtYj1.uD>+L S:8:mv [}1&7:Tqc)' );
define( 'SECURE_AUTH_SALT', 'as|%edpHdS5/ik/TV|Qoo$=Da/2fl{:r.`;dhAX-KL!*=%@W6{EW]M.oWO,E:9?X' );
define( 'LOGGED_IN_SALT', '#vBSzvD9JZ~4?UDhW.RVOl9}n1`Sm?ob~:j[&Hl@.]o)_M/TfE6.d2<Z3V_>tS2b' );
define( 'NONCE_SALT', 'f~MgE_(^s}rhjnGN1}XBP(4h0MS!EAR|*Z&OXk>#t@7=KKm4.PcY6~?J$Dq6_:ZE' );
/**#@-*/
/**
* 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!
*/
$table_prefix = '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 documentation.
*
* @link https://wordpress.org/support/article/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', __DIR__ . '/' );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
问题一:当网站出现502时(配置文件,网络都可以运行的情况下)
注释 /etc/php-fpm.d/www.conf中的listen allow client=0.0.0.0
拓展内容
使用dockerfile安装django框架
1、创建django目录
[root@docter django]# mkdir -p /root/django/
[root@docter django]# cd /root/django/
# 2、编写Dockerfile文件
[root@docter django]# vim Dockerfile
FROM python:3.6
RUN cd /opt/ &&\
pip install django &&\
django-admin startproject docker &&\
cd docker/ &&\
django-admin startapp aplication
WORKDIR /opt/docker
EXPOSE 8080
CMD python manage.py runserver 0.0.0.0:8080
# 3、构建镜像
[root@docter django]# docker build -t blog-django:v1 . # 构建镜像
Sending build context to Docker daemon 2.048kB
...
Successfully built 75ea06c8060c
Successfully tagged blog-django:v1
# 4、启动容器
[root@docter django]# docker run -d -p 8080:8080 blog-django:v1
fc9b4d2b4109b5d1c012d98fc9540ca781396992ad0a3691dc86fe8c0848ee1a
# 5、查看容器是否启动
[root@docter django]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc9b4d2b4109 blog-django:v1 "/bin/sh -c 'python …" 2 seconds ago Up 1 second 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sweet_allen
# 6、修改django配置文件
6.1、从容器内复制配置文件
[root@docter django]# docker cp fc9b4d2b4109:/opt/docker/docker/settings.py .
[root@docter django]# ll
total 8
-rw-r--r-- 1 root root 240 Jul 30 16:52 Dockerfile
-rw-r--r-- 1 root root 3242 Jul 30 16:04 settings.py
6.2、修改配置文件
[root@docter django]# egrep 'ALLOWED_HOSTS' settings.py
ALLOWED_HOSTS = ['*'] # 修改这个配置文件
# 7、重新启动容器
[root@docter django]# docker restart fc9b4d2b4109
fc9b4d2b4109
[root@docter django]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc9b4d2b4109 blog-django:v1 "/bin/sh -c 'python …" About an hour ago Up 56 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sweet_allen
# 8、IP访问
192.168.1.50:8080
Dockerfile搭建java
1、创建java目录
[root@docter ~]# mkdir java
[root@docter ~]# cd java/
2、上传java安装包
# 1、上传安装包
[root@docter java]# lz springboot-master.zip
# 2、解压并查看
[root@docter java]# unzip springboot-master.zip
[root@docter java]# ll
total 60
drwxr-xr-x 4 root root 108 Jul 24 21:14 springboot-master
-rw-r--r-- 1 root root 58361 Jul 30 15:49 springboot-master.zip
3、编译java
# 1、下载镜像
[root@docter springboot-master]# pwd
/root/java/springboot-master
[root@docter springboot-master]# docker run -it -v /root/jave/:/opt/ maven:3.6-openjdk-8 bash
3.6-openjdk-8: Pulling from library/maven
004f1eed87df: Pull complete
5d6f1e8117db: Pull complete
48c2faf66abe: Pull complete
234b70d0479d: Pull complete
d7eb6c022a4e: Pull complete
347c03cf94f8: Pull complete
914dd506c750: Pull complete
d7d7fe135d55: Pull complete
71a3a5508994: Pull complete
73dccb1622e9: Pull complete
Digest: sha256:ac624d39dd0b7bed827d4b0d1bbaeb457012e0cba0fb6c53ce34b00bed16ec08
Status: Downloaded newer image for maven:3.6-openjdk-8
# 2、容器里面编译安装
root@5d6ce165b40f:/opt/springboot-master# ls
Dockerfile mvnw mvnw.cmd pom.xml src
root@5d6ce165b40f:/opt/springboot-master# mvn clean package # 预计时间5分钟
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/2.4.4/spring-boot-starter-parent-2.4.4.pom
...
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS #编译成功
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:41 min
[INFO] Finished at: 2021-07-30T11:16:39Z
[INFO] ------------------------------------------------------------------------
# 3、构建镜像
[root@docter springboot-master]# docker build -t blog-jave-v1 . #构建镜像
Sending build context to Docker daemon 41.79MB
Step 1/5 : FROM openjdk:18-jdk
18-jdk: Pulling from library/openjdk
18-jdk: Pulling from library/openjdk
1da50e1664e1: Pull complete
e4c8e5a84542: Pull complete
170c51e5216c: Pull complete
Digest: sha256:a115ac999336b4bc851abd2ec2eebf8feb01860972e121acd9a74438a5371957
Status: Downloaded newer image for openjdk:18-jdk
---> 0072c1906e21
Step 2/5 : ADD target/*.war /usr/local/webapp/
---> 1b889632132b
Step 3/5 : WORKDIR /usr/local/webapp
---> Running in cb431d072089
Removing intermediate container cb431d072089
---> 6935bc6e4bf9
Step 4/5 : EXPOSE 8080
---> Running in 939e6a3fc2bd
Removing intermediate container 939e6a3fc2bd
---> 64933fc6f4e6
Step 5/5 : CMD java -jar GitLab-0.0.1-SNAPSHOT.war
---> Running in 7ff37eb57a00
Removing intermediate container 7ff37eb57a00
---> 9658342afa70
Successfully built 9658342afa70
Successfully tagged blog-jave-v1:latest
# 4、启动容器
[root@docter springboot-master]# docker run -d -p 8888:8080 blog-java-v1
9929fdafeff8e9d7c842adf7484e0978fabf53713871f2760971efaca5e25ca5
# 5、查看容器是否启动
[root@docter springboot-master]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9929fdafeff8 blog-java-v1 "/bin/sh -c 'java -j…" 15 seconds ago Up 15 seconds 0.0.0.0:8888->8080/tcp, :::8888->8080/tcp eloquent_wilson
# 5、访问测试
192.168.1.50:8888
查看java页面