Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Using docker to build LAMP environment

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Environment: Centos7.4

[root@oracle ~] # cat / etc/redhat-release

CentOS Linux release 7.4.1708 (Core)

Get the most up-to-date version of Docker

Https://get.docker.com

(This script is meant for quick & easy install via:)

[root@oracle] # curl-fsSL https://get.docker.com-o get-docker.sh

[root@oracle ~] # sh get-docker.sh

[root@oracle ~] # service docker start

Redirecting to / bin/systemctl start docker.service

[root@oracle ~] # docker version

Client:

Version: 18.09.0

API version: 1.39

Go version: go1.10.4

Git commit: 4d60db4

Built: Wed Nov 7 00:48:22 2018

OS/Arch: linux/amd64

Experimental: false

Server: Docker Engine-Community

Engine:

Version: 18.09.0

API version: 1.39 (minimum version 1.12)

Go version: go1.10.4

Git commit: 4d60db4

Built: Wed Nov 7 00:19:08 2018

OS/Arch: linux/amd64

Experimental: false

[root@oracle] # docker search-s 10 lamp

Flag-stars has been deprecated, use-filter=stars=3 instead

NAME DESCRIPTION STARS OFFICIAL AUTOMATED

Linode/lamp LAMP on Ubuntu 14.04.1 LTS Container 157

Tutum/lamp Out-of-the-box LAMP image (PHP+MySQL) 108

Greyltc/lamp a super secure, up-to-date and lightweight L OK 92 [OK]

Mattrayner/lamp A simple LAMP docker image running the prere OK 55 [OK]

Fauria/lamp Modern, developer friendly LAMP stack. Inclu Inclu 46 [OK]

Janes/alpine-lamp lamp base on alpine linux 36 [OK]

Nickistre/ubuntu-lamp LAMP server on Ubuntu 26 [OK]

Nickistre/centos-lamp LAMP on centos setup 24 [OK]

Dgraziotin/lamp 11 [OK]

Lioshi/lamp Docker image for LAMP + MySql under debian 10 [OK]

Go to the official website of docker hub to search for the image of linode/lamp, and you can see its specific instructions (see specific instructions to avoid detours)

Https://hub.docker.com/r/linode/lamp

[root@oracle ~] # docker pull linode/lamp (download image)

Using default tag: latest

Latest: Pulling from linode/lamp

A3ed95caeb02: Pull complete

76a4cab4eb20: Pull complete

D2ff49536f4d: Pull complete

F94adccdbb9c: Pull complete

808b5278afbb: Pull complete

Ab367f87d978: Pull complete

Digest: sha256:4b7af614899c795aa30cb60b62c28b978201c2dca3fb1352a60fec206edb92de

Status: Downloaded newer image for linode/lamp:latest

[root@oracle ~] # docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

Linode/lamp latest 2359fa12fded 3 years ago 372MB

Create docker Volum

[root@oracle ~] # docker volume create lamp_www

[root@oracle ~] # docker volume create lamp_apache_conf

[root@oracle ~] # docker volume create lamp_mysql_conf

[root@oracle ~] # docker volume create lamp_mysql_data

Create a / var/lib/docker/volumes directory on the host

[root@oracle ~] # mkdir / var/lib/docker/volumes

Start mirroring

[root@oracle] # docker run-it-p 80:80-p 3306 it 3306-v lamp_www:/var/www-v lamp_apache_conf:/etc/apache2-v lamp_mysql_conf:/etc/mysql-v lamp_mysql_data:/var/lib/mysql-- name lamp linode/lamp / bin/bash

(docker run: run a container. If you want to bind the 0-1024 port of the host host later, you need to use sudo because it is Ubuntu)

(- p port1:port2: map the host's port port1 to the port2 in the container)

(- v volume:path: maps the docker volume of the host to the specified path in the container)

(- t-I linode/lamp / bin/bash: use linode/lamp to generate the container and open shell)

It has now entered the container. If you want to exit the container, do not use exit or ctrl+d. This will close the container directly.

To exit, use ctrl + p, and then ctrl + Q

The above startup command maps a total of four paths, namely, the web root of apache, the two main configuration files of apache and MySQL, and the data directory of MySQL. All these are for the convenience of our subsequent operations to modify the configuration file and web file directly on the host computer. Mapping the database directory with docker volumes can also ensure that the data will not be lost when the mirror runs again.

[root@oracle ~] # ls-la / var/lib/docker/volumes

Total 24

Drwx-. 8 root root 255 Dec 1 13:41.

Drwx--x--x. 15 root root 200 Dec 1 10:56..

Drwxr-xr-x. 3 root root 19 Dec 1 13:41 lamp_apache_conf

Drwxr-xr-x. 3 root root 19 Dec 1 13:41 lamp_mysql_conf

Drwxr-xr-x. 3 root root 19 Dec 1 13:41 lamp_mysql_data

Drwxr-xr-x. 3 root root 19 Dec 1 13:41 lamp_www

Enter the directory of the lamp_mysql_data volume and you can see that the database directory in the mirror has been mapped to the host. As long as the volume is not deleted, its data will always exist in it. This avoids data loss after mirror restart.

[root@oracle ~] # ls-la / var/lib/docker/volumes/lamp_mysql_data

Total 0

Drwxr-xr-x. 3 root root 19 Dec 1 13:41.

Drwx-. 8 root root 255 Dec 1 13:41..

Drwx-. 5 102 106 164 Dec 1 13:43 _ data

[root@oracle ~] # ls-la / var/lib/docker/volumes/lamp_mysql_data/_data

Total 28684

Drwx-. 5 102 106 164 Dec 1 13:43.

Drwxr-xr-x. 3 root root 19 Dec 1 13:41..

-rw-r--r--. 1 root root 0 Feb 20 2015 debian-5.5.flag

Drwx-. 2 102 106 20 Dec 1 13:43 exampleDB

-rw-rw----. 1 102 106 18874368 Feb 20 2015 ibdata1

-rw-rw----. 1 102 106 5242880 Feb 20 2015 ib_logfile0

-rw-rw----. 1 102 106 5242880 Feb 20 2015 ib_logfile1

Drwx-. 2 102 root 4096 Dec 1 13:43 mysql

-rw-. 1 root root 6 Feb 20 2015 mysql_upgrade_info

Drwx-. 2 102 106 4096 Dec 1 13:43 performance_schema

Personalized configuration of the lamp environment in the terminal

[root@oracle] # docker exec-I-t lamp bash

Start apache2 and mysql

Root@9a44df59980e:/#service apache2 start

Root@9a44df59980e:/#service mysql start

(you can check whether ports 80 and 3306 have been opened successfully by netstat-naltp command.)

Root@9a44df59980e:/# netstat-naltp

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

Tcp 0 0 127.0.0.1 3306 0.0.0.0: LISTEN-

Tcp 00 0.0.0.0:80 0.0.0.0: LISTEN 100/apache2

Set mysql to allow root to log in remotely, and set the password for root

Root@9a44df59980e:/#smysql-u root-p

# initial password: Admin2015

# modify root to log in remotely:

Mysql > use mysql

Mysql > update user set host='% 'where user =' root' and host='127.0.0.1'

# modify password:

Mysql > update user set password=password ("123456") where user='root'

# Refresh permissions

FLUSH PRIVILEGES

Mysql > quit

Bye

Install the php extension

Root@9a44df59980e:/# apt-get update

Root@9a44df59980e:/# apt-get install-y php5-mysql

Root@9a44df59980e:/# apt-get install-y php5-gd

# then restart apache2:

Root@9a44df59980e:/# service apache2 restart

# confirm whether mysql and apache2 have been installed successfully:

Root@9a44df59980e:/# netstat-naltp

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

Tcp 0 0 127.0.0.1 3306 0.0.0.0: LISTEN-

Tcp 0 0 0.0.0.0:80 0.0.0.0: LISTEN 2899/apache2

You can see what php extensions are on the server with the following command:

Root@9a44df59980e:/# apt-cache search php5-*

Test results:

[root@oracle ~] # ip add

Ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000

Link/ether 00:0c:29:6b:22:a9 brd ff:ff:ff:ff:ff:ff

Inet 192.168.1.203/24 brd 192.168.1.255 scope global noprefixroute dynamic ens33

Valid_lft 2619sec preferred_lft 2619sec

Inet6 fe80::9375:cddb:5162:50f/64 scope link noprefixroute

Valid_lft forever preferred_lft forever

[root@oracle ~] # curl http://192.168.1.203

The Docker LAMP stack is working.

The configuration information can be found here or here

This index.html file is located in the "/ var/www/example.com/public_html" directory.

Visit http://192.168.1.203/

Test example

[root@oracle] # docker exec-I-t lamp bash

Create database data

Root@9a44df59980e:/# mysql-uroot-p

Mysql > show databases

Mysql > create database example default charset=utf8

Mysql > show create database example\ G

Mysql > use example

Mysql > create table tbl (idx integer (3), UserName varchar (30), LastName varchar (50), FreeText varchar (100) ENGINE=InnoDB AUTO_INCREMENT=1 default charset=utf8

Mysql > show create table tbl\ G

Mysql > show columns from tbl

Mysql > select from tbl

Mysql > insert into tbl values (1) just a test')

Mysql > update tbl set UserName='Berber' where UserName='Rafi'

Mysql > select from tbl

Mysql > delete from tbl where idx=1 limit 1

Mysql > insert into tbl values (1) just a test')

Mysql > select * from tbl

When the database is created and viewed on the host machine, you can see the database example created in the container above.

[root@oracle _ data] # cd / var/lib/docker/volumes/lamp_mysql_data/_data

[root@oracle _ data] # ls

Debian-5.5.flag example exampleDB ibdata1 ib_logfile0 ib_logfile1 mysql mysql_upgrade_info performance_schema

Create a PHP file

Root@9a44df59980e:/# cd / var/www/example.com/public_html

Root@9a44df59980e:/var/www/example.com/public_html# vi links.x

Home

Add a new entry to the DataBase

Edit an entry

Delete an entry from the DataBase

Root@9a44df59980e:/var/www/example.com/public_html# vi index.php

Web Database Sample Index

Data from tbl

Open http://192.168.1.203/index.php and you can already see the data.

Root@9a44df59980e:/var/www/example.com/public_html# vi add.php

Add an entry to the database

Add an entry

Index:

UserName:

LastName:

FreeText:

Root@9a44df59980e:/var/www/example.com/public_html# vi add2tbl.php

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report