您好,登錄后才能下訂單哦!
這篇文章主要講解了“Docker容器如何編譯LNMP”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Docker容器如何編譯LNMP”吧!
使用Docker容器基于centos鏡像分別制作nginx鏡像,mysql鏡像和php鏡像使用編譯安裝的方式,最后通過鏡像啟動成容器時使用container模式網絡模式并訪問到php測試頁面
//拉取centos鏡像 [root@Docker ~]# docker pull centos Using default tag: latest latest: Pulling from library/centos a1d0c7532777: Pull complete Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 Status: Downloaded newer image for centos:latest docker.io/library/centos:latest [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 5d0da3dc9764 2 months ago 231MB // 運行centos鏡像 [root@localhost ~]# docker run -it --name nginx 5d0da3dc9764 /bin/bash [root@03ca6bdc0374 /]# // 傳nginx安裝包到容器中 [root@localhost ~]# docker cp /usr/src/nginx-1.20.1.tar.gz 03ca6bdc0374:/usr/src/ // 創建nginx賬戶 [root@03ca6bdc0374 /]# useradd -r -M -s /sbin/nologin nginx // 安裝依賴包 [root@03ca6bdc0374 /]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make // 創建nginx日志存放目錄 [root@03ca6bdc0374 /]# mkdir -p /var/log/nginx [root@03ca6bdc0374 /]# chown -R nginx.nginx /var/log/nginx/ // 解壓nginx包進行編譯安裝 [root@03ca6bdc0374 /]# cd /usr/src/ [root@03ca6bdc0374 src]# ls debug kernels nginx-1.20.1.tar.gz [root@03ca6bdc0374 src]# tar xf nginx-1.20.1.tar.gz [root@03ca6bdc0374 src]# cd nginx-1.20.1 [root@03ca6bdc0374 nginx-1.20.1]# ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-debug \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_image_filter_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --http-log-path=/var/log/nginx/access.log \ --error-log-path=/var/log/nginx/error.log [root@03ca6bdc0374 nginx-1.20.1]# make && make install // 設置環境變量 [root@03ca6bdc0374 nginx-1.20.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh [root@03ca6bdc0374 nginx-1.20.1]# source /etc/profile.d/nginx.sh // 查看監聽端口 [root@03ca6bdc0374 nginx-1.20.1]# nginx [root@03ca6bdc0374 nginx-1.20.1]# ss -antl 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@localhost ~]# curl 172.17.0.2 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h2>Welcome to nginx!</h2> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/" rel="external nofollow" >nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/" rel="external nofollow" >nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> // 修改配置文件 [root@03ca6bdc0374 nginx-1.20.1]# vim /usr/local/nginx/conf/nginx.conf ...... http { include 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.php index.html index.htm; // 添加index.php } ...... location ~ \.php$ { root /var/www/html; // php測試頁面目錄 fastcgi_pass 127.0.0.1:9000; // 在工作中這里要改為php服務器的地址 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $Document_Root$fastcgi_script_name; include fastcgi_params; } ...... daemon off; // 寫最后面 // 重新加載配置文件 [root@03ca6bdc0374 nginx-1.20.1]# nginx -s reload [root@03ca6bdc0374 nginx-1.20.1]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:80 0.0.0.0:* // 創建nginx鏡像 [root@localhost ~]# docker commit -a '1826597954@qq.com' -c 'CMD ["/usr/local/nginx/sbin/nginx"]' -p 03ca6bdc0374 gaofan1225/nginx:v0.1 sha256:453bfb1a13ae0aeba38e2e26ebe03e09544aa2ea8b477e45e4fb8aa51fec3e92 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE gaofan1225/nginx v0.1 453bfb1a13ae 16 seconds ago 575MB centos latest 5d0da3dc9764 2 months ago 231MB
// 運行centos鏡像 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE gaofan1225/nginx v0.1 453bfb1a13ae 16 seconds ago 575MB centos latest 5d0da3dc9764 2 months ago 231MB [root@localhost ~]# docker run -it --name mysql 5d0da3dc9764 /bin/bash [root@3ea39d4dfa8f /]# // 傳mysql安裝包到容器 [root@localhost ~]# docker cp /usr/src/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz 9b6741a9ef22:/usr/src/ // 安裝依賴包 [root@9b6741a9ef22 /]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs [root@9b6741a9ef22 /]# yum -y install libaio* [root@9b6741a9ef22 /]# yum -y install numactl.x86_64 // 創建mysql用戶 [root@9b6741a9ef22 /]# useradd -r -M -s /sbin/nologin mysql // 解壓安裝包 [root@9b6741a9ef22 /]# cd /usr/src/ [root@9b6741a9ef22 src]# ls debug kernels mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz [root@9b6741a9ef22 src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ // 制作軟鏈接,設置全校性 [root@9b6741a9ef22 src]# cd /usr/local/ [root@9b6741a9ef22 local]# ls bin include libexec share etc lib mysql-5.7.34-linux-glibc2.12-x86_64 src games lib64 sbin [root@9b6741a9ef22 local]# ln -sv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql 'mysql' -> 'mysql-5.7.34-linux-glibc2.12-x86_64/' [root@9b6741a9ef22 local]# chown -R mysql.mysql /usr/local/mysql* // 設置環境變量 [root@9b6741a9ef22 local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh [root@9b6741a9ef22 local]# source /etc/profile.d/mysql.sh [root@9b6741a9ef22 local]# echo $PATH /usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin // 制作頭文件軟鏈接 [root@9b6741a9ef22 local]# ln -s /usr/local/mysql/include /usr/include/mysql // 創建幫助文檔 [root@9b6741a9ef22 local]# cat /etc/man_db.conf MANDATORY_MANPATH /usr/local/mysql/man // 創建庫文件 [root@9b6741a9ef22 local]# cat /etc/ld.so.conf.d/mysql.conf /usr/local/mysql/lib [root@9b6741a9ef22 local]# ldconfig // 創建數據存放目錄 [root@9b6741a9ef22 local]# mkdir -p /opt/data [root@9b6741a9ef22 local]# chown -R mysql.mysql /opt/data [root@9b6741a9ef22 local]# ls -l /opt/ total 0 drwxr-xr-x. 2 mysql mysql 6 Dec 4 01:31 data // 初始化數據庫 [root@9b6741a9ef22 local]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/ // 生成配置文件 [root@9b6741a9ef22 local]# cat > /etc/my.cnf <<EOF [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 pid-file = /opt/data/mysql.pid user = mysql skip-name-resolve EOF // 配置mysql啟動服務 [root@9b6741a9ef22 local]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /usr/local/mysql/support-files/mysql.server [root@9b6741a9ef22 local]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /usr/local/mysql/support-files/mysql.server // 啟動mysql服務 [root@9b6741a9ef22 local]# /usr/local/mysql/support-files/mysql.server start Starting MySQL.Logging to '/opt/data/9b6741a9ef22.err'. SUCCESS! [root@9b6741a9ef22 local]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 80 *:3306 *:* // 編寫mysql啟動腳本 [root@9b6741a9ef22 local]# cd / [root@9b6741a9ef22 /]# cat /start.sh #!/bin/sh /usr/local/mysql/support-files/mysql.server start /bin/bash [root@9b6741a9ef22 /]# chmod +x /start.sh // 創建mysql鏡像 [root@localhost ~]# docker commit -a '1826597954@qq.com' -c 'CMD ["/bin/bash","/start.sh"]' -p 9b6741a9ef22 gaofan1225/mysql:v0.1 sha256:7abe6fc819127b8ef3d9ac0ea3d24aadda1b189d739e4b53416530fc79db795f [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE gaofan1225/mysql v0.1 7abe6fc81912 10 seconds ago 3.81GB gaofan1225/nginx v0.1 453bfb1a13ae 17 minutes ago 575MB centos latest 5d0da3dc9764 2 months ago 231MB
// 運行centos鏡像 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE gaofan1225/mysql v0.1 7abe6fc81912 10 seconds ago 3.81GB gaofan1225/nginx v0.1 453bfb1a13ae 17 minutes ago 575MB centos latest 5d0da3dc9764 2 months ago 231MB [root@localhost ~]# docker run -it --name php 5d0da3dc9764 [root@c6882394804e /]# // 把php安裝包和依賴包傳到容器中 [root@localhost ~]# docker cp /usr/src/php-8.0.12.tar.gz c6882394804e:/usr/src/ [root@localhost ~]# docker cp /usr/src/oniguruma-devel-6.8.2-2.el8.x86_64.rpm c6882394804e:/usr/src/ // 下載epel源和依賴包 [root@c6882394804e /]# yum -y install epel-release [root@c6882394804e /]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd libzip-devel libsqlite3x libsqlite3x-devel oniguruma libzip-devel gcc gcc-c++ make [root@c6882394804e /]# yum -y install libcurl-devel // 解壓php安裝包進行編譯安裝 [root@c6882394804e /]# cd /usr/src/ [root@c6882394804e src]# ls debug oniguruma-devel-6.8.2-2.el8.x86_64.rpm kernels php-8.0.12.tar.gz [root@c6882394804e src]# yum -y install oniguruma-devel-6.8.2-2.el8.x86_64.rpm [root@c6882394804e src]# tar xf php-8.0.12.tar.gz [root@c6882394804e src]# cd php-8.0.12 [root@c6882394804e php-8.0.12]# ./configure --prefix=/usr/local/php8 \ --with-config-file-path=/etc \ --enable-fpm \ --disable-debug \ --disable-rpath \ --enable-shared \ --enable-soap \ --with-openssl \ --enable-bcmath \ --with-iconv \ --with-bz2 \ --enable-calendar \ --with-curl \ --enable-exif \ --enable-ftp \ --enable-gd \ --with-jpeg \ --with-zlib-dir \ --with-freetype \ --with-gettext \ --enable-mbstring \ --enable-pdo \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-readline \ --enable-shmop \ --enable-simplexml \ --enable-sockets \ --with-zip \ --enable-mysqlnd-compression-support \ --with-pear \ --enable-pcntl \ --enable-posix [root@c6882394804e php-8.0.12]# make && make install // 設置環境變量 [root@c6882394804e php-8.0.12]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh [root@c6882394804e php-8.0.12]# source /etc/profile.d/php.sh // 配置php-fpm [root@c6882394804e php-8.0.12]# cp php.ini-production /etc/php.ini cp: overwrite '/etc/php.ini'? y [root@c6882394804e php-8.0.12]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@c6882394804e php-8.0.12]# chmod +x /etc/rc.d/init.d/php-fpm [root@c6882394804e php-8.0.12]# cd /usr/local/php8/etc/ [root@c6882394804e etc]# cp php-fpm.conf.default php-fpm.conf [root@c6882394804e etc]# cd php-fpm.d/ [root@c6882394804e php-fpm.d]# cp www.conf.default www.conf // 查看監聽端口 [root@c6882394804e php-fpm.d]# /usr/local/php8/sbin/php-fpm [root@c6882394804e php-fpm.d]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 127.0.0.1:9000 0.0.0.0:* // 編寫啟動腳本 [root@c6882394804e php-fpm.d]# cd / [root@c6882394804e /]# cat /start.sh #!/bin/sh /usr/local/php8/sbin/php-fpm /bin/bash [root@c6882394804e /]# chmod +x /start.sh // 創建測試頁面 [root@c6882394804e /]# mkdir -p /var/www/html [root@c6882394804e /]# cd /var/www/html/ [root@c6882394804e html]# vi index.php [root@c6882394804e html]# cat index.php <?php phpinfo(); ?> // 制作php鏡像 [root@localhost ~]# docker commit -a '1826597954@qq.com' -c 'CMD ["/bin/bash","/start.sh"]' -p c6882394804e gaofan1225/php:v0.1 sha256:9bb6f6ec5b7cff9b3e92bc3b2f8eb2542c963643e74642be7eace465bc2225f9 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE gaofan1225/php v0.1 9bb6f6ec5b7c 15 seconds ago 1.53GB gaofan1225/mysql v0.1 7abe6fc81912 2 hours ago 3.81GB gaofan1225/nginx v0.1 453bfb1a13ae 2 hours ago 575MB centos latest 5d0da3dc9764 2 months ago 231MB
使用container模式網絡模式
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES // 啟動nginx容器 [root@localhost ~]# docker run -dit --name nginx -p 80:80 453bfb1a13ae a8ff680fc2bb61118d10ab1926fffed9c4975f72834d1628bf0cfff851bd7935 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8ff680fc2bb 453bfb1a13ae "/usr/local/nginx/sb…" 16 seconds ago Up 14 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx [root@localhost ~]# docker exec -it a8ff680fc2bb /bin/bash [root@a8ff680fc2bb /]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:80 0.0.0.0:* // 啟動mysql容器 [root@localhost ~]# docker run -dit --name mysql --network container:a8ff680fc2bb 7abe6fc81912 e776f9e93c6ca0d8fba53957cfa9e85105913fcbe53a9400c2657127eb049c2d [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e776f9e93c6c 7abe6fc81912 "/bin/bash /start.sh" 9 seconds ago Up 8 seconds mysql a8ff680fc2bb 453bfb1a13ae "/usr/local/nginx/sb…" 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx [root@localhost ~]# docker exec -it e776f9e93c6c /bin/bash [root@a8ff680fc2bb /]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:80 0.0.0.0:* LISTEN 0 80 *:3306 *:* // 啟動php容器 [root@localhost ~]# docker run -dit --name php --network container:a8ff680fc2bb 9bb6f6ec5b7c e80155914f858910ffb678a7d294e68804f735bf9a52efd21a036f7abee23bbe [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e80155914f85 9bb6f6ec5b7c "/bin/bash /start.sh" 4 seconds ago Up 3 seconds php e776f9e93c6c 7abe6fc81912 "/bin/bash /start.sh" About a minute ago Up About a minute mysql a8ff680fc2bb 453bfb1a13ae "/usr/local/nginx/sb…" 4 minutes ago Up 4 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx [root@localhost ~]# docker exec -it e80155914f85 /bin/bash [root@a8ff680fc2bb /]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 127.0.0.1:9000 0.0.0.0:* LISTEN 0 128 0.0.0.0:80 0.0.0.0:* LISTEN 0 80 *:3306 *:*
感謝各位的閱讀,以上就是“Docker容器如何編譯LNMP”的內容了,經過本文的學習后,相信大家對Docker容器如何編譯LNMP這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。