慕容的工作随笔

慕容的工作随笔

记录学习和工作

【转载】利用docker搭建LAMP(php5+mysql+apache2)集成环境

转自:https://blog.csdn.net/a33130317/article/details/81304048

https://www.cnblogs.com/saysmy/p/10389902.html

  1. 搜索并下载镜像
    我们先搜索lamp.

docker search lamp

选择星级最高的linode/lamp这个镜像进行下载

docker pull linode/lamp

  1. 查看镜像的使用说明(重点)
    到docker hub的官网去搜索linode/lamp这个镜像,可以查看到他的具体使用说明及tag

https://hub.docker.com/

搜索的结果如下: (左侧框是镜像的名称,右侧框是镜像的详细信息)

点击进入详情页后,可以看到具体的使用说明.

每个使用说明都推荐大家认真观看,看过后会少走好些弯路.

这里我们可以看出vhost的目录结构 , mysql的root密码是Admin2015 , 以及其他的配置信息.

  1. 建立docker卷
    docker volume create lamp_www # apache的web根目录
    docker volume create lamp_apache_conf # apache的配置目录
    docker volume create lamp_mysql_conf # MySQL的配置目录
    docker volume create lamp_mysql_data # MySQL的数据目录
    已经建立好的docker卷,在宿主机的目录为:

/var/lib/docker/volumes/

  1. 启动镜像
    docker run -it -p 80:80 -p 3306: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:运行一个container,如果后面要绑定宿主主机的0-1024端口需要使用sudo

-p port1:port2: 将宿主机的端口port1映射到容器中的port2

-v volume:path: 将宿主机的docker卷映射到容器中的指定的路径

-t -i linode/lamp /bin/bash:使用linode/lamp生成容器,并打开shell

按照官方镜像的说明,我们使用了-i和-t两个参数,直接进入到了容器内.

如果想退出容器,请不要使用exit或ctrl+d.这样会将容器直接关闭.

退出请使用ctrl + p , ctrl + q

上面的启动命令中一共映射了四个路径,分别是apache的web根目录,apache和MySQL的两个主配置文件,及MySQL的数据目录.这些都是为了方便我们后续直接在宿主机上对配置文件和web文件的修改进行的操作,利用docker卷映射了数据库的目录也能保证镜像再次运行的时候数据不会丢失.

  1. 查看docker卷里是否已经有映射的目录
    先使用ctrl + p , ctrl + q退出主机

进入宿主机的/var/lib/docker/volumes/目录,可以看到docker卷对应的目录

进入lamp_mysql_data这个卷的目录,可以看到镜像中的数据库目录已经映射到宿主机里了.

这个卷只要不删除,其数据就一直存在其中.这样避免了镜像重启后数据丢失的情况.

  1. 在终端中对lamp环境作个性化配置
    (1) 再次进入终端
    docker exec -it lamp bash
    (2) 启动apache2和mysql
    service apache2 start
    service mysql start

可以通过netstat -pant 命令查看80和3306端口有没有开启成功

(3) 设置mysql允许root远程登录,及设置root的密码
mysql -u root -p
#密码:Admin2015

#修改root可远程登录:
mysql>use mysql;
mysql>update user set host = ‘%’ where user = ‘root’ and host=‘127.0.0.1’;

#修改密码:
mysql>update user set password=password(“your_password”) where user=‘root’;

#刷新权限
FLUSH PRIVILEGES;
(4) 安装php扩展
apt-get update
apt-get install -y php5-mysql
apt-get install -y php5-gd

…

#然后重启apache2:
service apache2 restart

#再次确认mysql和apache2有没有安装成功:
netstat -pant
可以通过下面的命令查看服务器上有哪些php的扩展:

apt-cache search php5-*

  1. 将程序和数据库分别上传,测试是否可用

三. 结语
到此,LAMP集成环境已经搭建成功了,但还不是很理想.一旦容器停止,这个环境还得重新配置.

接下来我会探索通过dockerfiles,或通过docker-composer,或通过sh脚本实现便捷的一键部署环境的方法.

希望大家持续的关注我接下来的文章.

感谢https://blog.csdn.net/MasonQAQ/article/details/78048112这篇文章,正是站在了他的肩膀上,我才能将这篇经验整理出来.感谢! 感谢! 感谢! (重要的事情说三遍)

四. 配置文件
Mysql: my.cnf

The MySQL database server configuration file.

You can copy this to one of:

- “/etc/mysql/my.cnf” to set global options,

- “~/.my.cnf” to set user-specific options.

One can use all long options that the program supports.

Run program with –help to get a list of available options and with

–print-defaults to see which it would actually understand and use.

For explanations see

http://dev.mysql.com/doc/mysql/en/server-system-variables.html

This will be passed to all mysql clients

It has been reported that passwords should be enclosed with ticks/quotes

escpecially if they contain “#” chars…

Remember to edit /etc/mysql/debian.cnf when changing the socket location.

[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock

Here is entries for some specific programs

The following values assume you have at least 32M ram

This was formally known as [safe_mysqld]. Both versions are currently parsed.

[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0

[mysqld]

* Basic Settings

user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking

Instead of skip-networking the default is now to listen only on

localhost which is more compatible and is not less secure.

bind-address = 0.0.0.0

* Fine Tuning

key_buffer = 32M
max_allowed_packet = 1M
thread_stack = 128K
thread_cache_size = 8

This replaces the startup script and checks MyISAM tables if needed

the first time they are touched

myisam-recover = BACKUP
max_connections = 75
table_cache = 32
#thread_concurrency = 10

* Query Cache Configuration

query_cache_limit = 1M
query_cache_size = 16M

* Logging and Replication

Both location gets rotated by the cronjob.

Be aware that this log type is a performance killer.

As of 5.1 you can enable the log at runtime!

#general_log_file = /var/log/mysql/mysql.log
#general_log = 1

Error log - should be very few entries.

log_error = /var/log/mysql/error.log

Here you can see queries with especially long duration

#log_slow_queries = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes

The following can be used as easy to replay backup logs or for replication.

note: if you are setting up a replication slave, see README.Debian about

other settings you may need to change.

#server-id = 1
#log_bin = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size = 100M
#binlog_do_db = include_database_name
#binlog_ignore_db = include_database_name

* InnoDB

InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.

Read the manual for more InnoDB related options. There are many!

* Security Features

Read the manual, too, if you want chroot!

chroot = /var/lib/mysql/

For generating SSL certificates I recommend the OpenSSL GUI “tinyca”.

ssl-ca=/etc/mysql/cacert.pem

ssl-cert=/etc/mysql/server-cert.pem

ssl-key=/etc/mysql/server-key.pem

[mysqldump]
quick
quote-names
max_allowed_packet = 16M

[mysql]
#no-auto-rehash # faster start of mysql but no tab completition

[isamchk]
key_buffer = 16M

* IMPORTANT: Additional settings that can override those from this file!

The files must end with ‘.cnf’, otherwise they’ll be ignored.

!includedir /etc/mysql/conf.d/
Apache: apache2.conf

This is the main Apache server configuration file. It contains the

configuration directives that give the server its instructions.

See http://httpd.apache.org/docs/2.4/ for detailed information about

the directives and /usr/share/doc/apache2/README.Debian about Debian specific

hints.

Summary of how the Apache 2 configuration works in Debian:

The Apache 2 web server configuration in Debian is quite different to

upstream’s suggested way to configure the web server. This is because Debian’s

default Apache2 installation attempts to make adding and removing modules,

virtual hosts, and extra configuration directives as flexible as possible, in

order to make automating the changes and administering the server as easy as

possible.

It is split into several files forming the configuration hierarchy outlined

below, all located in the /etc/apache2/ directory:

/etc/apache2/

|– apache2.conf

| `– ports.conf

|– mods-enabled

| |– *.load

| `– *.conf

|– conf-enabled

| `– *.conf

`– sites-enabled

`– *.conf

* apache2.conf is the main configuration file (this file). It puts the pieces

together by including all remaining configuration files when starting up the

web server.

* ports.conf is always included from the main configuration file. It is

supposed to determine listening ports for incoming connections which can be

customized anytime.

* Configuration files in the mods-enabled/, conf-enabled/ and sites-enabled/

directories contain particular configuration snippets which manage modules,

global configuration fragments, or virtual host configurations,

respectively.

They are activated by symlinking available configuration files from their

respective *-available/ counterparts. These should be managed by using our

helpers a2enmod/a2dismod, a2ensite/a2dissite and a2enconf/a2disconf. See

their respective man pages for detailed information.

* The binary is called apache2. Due to the use of environment variables, in

the default configuration, apache2 needs to be started/stopped with

/etc/init.d/apache2 or apache2ctl. Calling /usr/bin/apache2 directly will not

work with the default configuration.

Global configuration

ServerRoot: The top of the directory tree under which the server’s

configuration, error, and log files are kept.

NOTE! If you intend to place this on an NFS (or otherwise network)

mounted filesystem then please read the Mutex documentation (available

at <URL:http://httpd.apache.org/docs/2.4/mod/core.html#mutex>);

you will save yourself a lot of trouble.

Do NOT add a slash at the end of the directory path.

#ServerRoot “/etc/apache2”

The accept serialization lock file MUST BE STORED ON A LOCAL DISK.

Mutex file:${APACHE_LOCK_DIR} default

PidFile: The file in which the server should record its process

identification number when it starts.

This needs to be set in /etc/apache2/envvars

PidFile ${APACHE_PID_FILE}

Timeout: The number of seconds before receives and sends time out.

Timeout 300

KeepAlive: Whether or not to allow persistent connections (more than

one request per connection). Set to “Off” to deactivate.

KeepAlive Off

MaxKeepAliveRequests: The maximum number of requests to allow

during a persistent connection. Set to 0 to allow an unlimited amount.

We recommend you leave this number high, for maximum performance.

MaxKeepAliveRequests 100

KeepAliveTimeout: Number of seconds to wait for the next request from the

same client on the same connection.

KeepAliveTimeout 5

These need to be set in /etc/apache2/envvars

User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

HostnameLookups: Log the names of clients or just their IP addresses

e.g., www.apache.org (on) or 204.62.129.132 (off).

The default is off because it’d be overall better for the net if people

had to knowingly turn this feature on, since enabling it means that

each client request will result in AT LEAST one lookup request to the

nameserver.

HostnameLookups Off

ErrorLog: The location of the error log file.

If you do not specify an ErrorLog directive within a <VirtualHost>

container, error messages relating to that virtual host will be

logged here. If you do define an error logfile for a <VirtualHost>

container, that host’s errors will be logged there and not here.

ErrorLog ${APACHE_LOG_DIR}/error.log

LogLevel: Control the severity of messages logged to the error_log.

Available values: trace8, …, trace1, debug, info, notice, warn,

error, crit, alert, emerg.

It is also possible to configure the log level for particular modules, e.g.

“LogLevel info ssl:warn”

LogLevel warn

Include module configuration:

IncludeOptional mods-enabled/.load
IncludeOptional mods-enabled/
.conf

Include list of ports to listen on

Include ports.conf

Sets the default security model of the Apache2 HTTPD server. It does

not allow access to the root filesystem outside of /usr/share and /var/www.

The former is used by web applications packaged in Debian,

the latter may be used for local directories served by the web server. If

your system is serving content from a sub-directory in /srv you must allow

access here, or in any related virtual host.

<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>

<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>

<Directory /var/www/>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>

#<Directory /srv/>

Options Indexes FollowSymLinks

AllowOverride None

Require all granted

#</Directory>

AccessFileName: The name of the file to look for in each directory

for additional configuration directives. See also the AllowOverride

directive.

AccessFileName .htaccess

The following lines prevent .htaccess and .htpasswd files from being

viewed by Web clients.

<FilesMatch “^.ht”>
Require all denied
</FilesMatch>

The following directives define some format nicknames for use with

a CustomLog directive.

These deviate from the Common Log Format definitions in that they use %O

(the actual bytes sent including headers) instead of %b (the size of the

requested file), because the latter makes it impossible to detect partial

requests.

Note that the use of %{X-Forwarded-For}i instead of %h is not recommended.

Use mod_remoteip instead.

LogFormat “%v:%p %h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"” vhost_combined
LogFormat “%h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"” combined
LogFormat “%h %l %u %t "%r" %>s %O” common
LogFormat “%{Referer}i -> %U” referer
LogFormat “%{User-agent}i” agent

Include of directories ignores editors’ and dpkg’s backup files,

see README.Debian for details.

Include generic snippets of statements

IncludeOptional conf-enabled/*.conf

Include the virtual host configurations:

IncludeOptional sites-enabled/*.conf

vim: syntax=apache ts=4 sw=4 sts=4 sr noet

<IfModule mpm_prefork_module>
StartServers 2
MinSpareServers 6
MaxSpareServers 12
MaxClients 30
MaxRequestsPerChild 3000
</IfModule>

ServerName localhost

评论 (0)

还没有评论,来做第一个吧。

发表评论

提交后需管理员审核通过后显示