其他分享
首页 > 其他分享> > 基于容器部署一个web站点

基于容器部署一个web站点

作者:互联网

基于容器部署一个web站点

目录

编译安装apache

拉取centos镜像

[root@localhost ~]# docker pull centos:8
8: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:8
docker.io/library/centos:8
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       8         5d0da3dc9764   10 months ago   231MB

用centos生成一个容器进去

[root@localhost ~]# docker run -it --name httpd centos:8 /bin/bash
[root@6cd57ac5f6e7 /]# 

在容器中阿里云的yum源和epel源

//yum源配置
[root@44293a537755 /]# cd /etc/yum.repos.d/
[root@44293a537755 yum.repos.d]# rm -rf *
[root@44293a537755 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@44293a537755 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
//配置epel源
[root@44293a537755 yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@44293a537755 yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@44293a537755 yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@44293a537755 yum.repos.d]# ls
CentOS-Base.repo  epel-modular.repo  epel-testing-modular.repo  epel-testing.repo  epel.repo

下载apache的包并复制到容器中

//新打开一个终端
[root@localhost ~]# wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz http://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz http://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg  apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.54.tar.gz
[root@localhost ~]# docker cp httpd-2.4.54.tar.gz httpd:/usr/src/
[root@localhost ~]# docker cp apr-1.7.0.tar.gz httpd:/usr/src/
[root@localhost ~]# docker cp apr-util-1.6.1.tar.gz httpd://usr/src/

//在容器中查看是否复制成功
[root@44293a537755 ~]# ls /usr/src/
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  debug  httpd-2.4.54.tar.gz  kernels

//下载相关依赖包
[root@44293a537755 ~]# dnf -y install wget vim openssl-devel pcre-devel expat-devel libtool libxml2-devel gcc gcc-c++ make

//创建一个用户
[root@44293a537755 ~]# useradd -r -M -s /sbin/nologin apache

编译安装apache

//解压
[root@44293a537755 ~]# cd /usr/src/
[root@44293a537755 src]# tar xf apr-1.7.0.tar.gz 
[root@44293a537755 src]# tar xf apr-util-1.6.1.tar.gz 
[root@44293a537755 src]# tar xf httpd-2.4.54.tar.gz 

//编译apr
[root@44293a537755 src]# cd apr-1.7.0/
[root@44293a537755 apr-1.7.0]# vim configure
cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    #$RM "$cfgfile"			//注释这一行
[root@44293a537755 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@44293a537755 apr-1.7.0]# make && make install

//解压编译apr-util
[root@44293a537755 apr-1.7.0]# cd ..
[root@44293a537755 src]# cd apr-util-1.6.1/
[root@44293a537755 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
[root@44293a537755 apr-util-1.6.1]# make && make install

//解压编译httpd
[root@44293a537755 apr-util-1.6.1]# cd ..
[root@44293a537755 src]# cd httpd-2.4.54/
[root@44293a537755 httpd-2.4.54]# ./configure --prefix=/usr/local/apache --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-rpm=prefork
[root@44293a537755 httpd-2.4.54]# make && make install

配置环境变量

/配置环境变量
[root@44293a537755 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
[root@44293a537755 ~]# source /etc/profile.d/apache.sh  

//创建头文件
[root@44293a537755 ~]# ln -s /usr/local/apache/include/ /usr/include/apache

//取消启动警告
[root@6cd57ac5f6e7 ~]# vim /usr/local/apache/conf/httpd.conf 
ServerName www.example.com:80				//取消注释

//启动apache服务
[root@6cd57ac5f6e7 ~]# apachectl start
[root@6cd57ac5f6e7 ~]# ss -anlt
State       Recv-Q      Send-Q           Local Address:Port             Peer Address:Port      Process      
LISTEN      0           128                    0.0.0.0:80                    0.0.0.0:*                      

//验证服务是否能访问
[root@6cd57ac5f6e7 ~]# curl 127.0.0.1
<html><body><h1>It works!</h1></body></html>

制作镜像

在开启一个终端进行操作

[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE      COMMAND       CREATED        STATUS        PORTS     NAMES
6cd57ac5f6e7   centos:8   "/bin/bash"   13 hours ago   Up 12 hours             httpd
//-p参数是让正在运作中的容器先暂停等制作完镜像。-c后面跟上的命令是让该镜像作为容器启动时把httpd服务也启用
[root@localhost ~]# docker commit -p -c 'CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]' 6cd57ac5f6e7 xinruizhong/httpd:v1.1
sha256:65836c5507843e93afef9baf8e5dbf8c61b7166a560ff28e8f2fa9a94d4a7fbe
[root@localhost ~]# docker images
REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
xinruizhong/httpd   v1.1      65836c550784   14 seconds ago   789MB
centos              8         5d0da3dc9764   10 months ago    231MB

//首次登录需要提供用户名和密码
[root@localhost ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: xinruizhong
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@localhost ~]# docker push xinruizhong/httpd:v1.1		//上传镜像
The push refers to repository [docker.io/xinruizhong/httpd]
a3705dc88dcd: Pushed 
74ddd0ec08fa: Layer already exists 
v1.1: digest: sha256:a5b133a68e0bb18860b7c9361e32ae93943f95c188bf0d126d1995d4f16d02b7 size: 742

测试镜像,并部署网页

//使用创建的镜像创建容器,并挂载存储卷
[root@localhost ~]# docker run -d --name web --restart always -p 80:80 -v /root/httpd_html:/usr/local/apache/htdocs xinruizhong/httpd:v1.1
13a1d5674f411590169702d564522f95f6371e1c702749fc76e17dfa20a2860e
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS         PORTS                               NAMES
13a1d5674f41   xinruizhong/httpd:v1.1   "/usr/local/apache/b…"   4 minutes ago   Up 4 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   web
6cd57ac5f6e7   centos:8                 "/bin/bash"              14 hours ago    Up 13 hours                                        httpd

//导入下载的压缩包,配置网站
[root@localhost ~]# cd /root/httpd_html/
[root@localhost httpd_html]# ls
ali.zip
[root@localhost httpd_html]# unzip ali.zip 
[root@localhost httpd_html]# ls
ali.zip  all.min.css  all.min.js  fonts  iali53.jpg  img  index.html  jquery.min.js  love.min.js
[root@localhost httpd_html]# rm -f ali.zip 

浏览器访问

image

标签:容器,web,httpd,apr,站点,apache,44293a537755,root,localhost
来源: https://www.cnblogs.com/Their-own/p/16575076.html