LEMP ( Linux, Nginx, MySQL, PHP7 ) stack Docker image deployment

About

The automated build docker LEMP image linuxconfig/lemp-php7 can be used as a testing and also as a production environment for a dynamic PHP applications. It comprises of Debian GNU/Linux, lightweight and yet powerful Nginx webserver, MySQL relational database management system and PHP scripting language.

At the time of writing, Debian Linux does not come with PHP 7 as a part of a its standard repository. From this reason PHP 7 was compiled from the source code during docker image build using compile-php-debian script located at https://github.com/linuxconfig/compile-php-debian.

Furthermore, not all 3rd-party PHP modules are currently supported on PHP 7.

Deployment

The following section describes the deployment of linuxconfig/lemp-php7 docker on your docker host. Let’ start by creating a sample PHP website with a MySQL connection handle. Alternatively add phpinfo(); into the mix to display current PHP information page:

<?php
$link = mysqli_connect("127.0.0.1", "admin", "pass");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made!" . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);
?>

Save the above code into index.php file and within a new html directory. Alternatively,html directory may contain your desired PHP application:

$ mkdir html
$ vi html/index.php
$ ls html/
index.php

At this stage we are ready to deploy linuxconfig/lemp-php7 docker image:

# docker run --name=lemp -dP -v $PWD/html:/var/www/html linuxconfig/lemp-php7
8306ab0e404e5e5ce4f17731f88a610f7f8bc939289c0746b875716355df0b1a

By executing the above command we have created and started a new docker container lemp. At the same time we have also mounted our website development directory html as nginx’s root directory. Both, MySQL and Nginx ports are now bound to a host system’s random port.

The both local ports can now be used to access both MySQL and Nginx services :

# docker port lemp
3306/tcp -> 0.0.0.0:32772
80/tcp -> 0.0.0.0:32773

Nginx Access

At this stage Nginx webserver and our website can be accessed via ports given by $ sudo docker port lemp command. Use command line or your browser to navigate to http://localhost:32773 URL:

# curl -i http://localhost:32773
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Sun, 01 Jan 2017 22:51:58 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/7.1.0

Success: A proper connection to MySQL was made!
Host information: 127.0.0.1 via TCP/IP

lemp nginx docker php info page

MySQL Access

The MySQL database can be accessed via admin user and default password pass. As previously, first we need to obtain the host system port number linked to MySQL database within a docker image by using $ sudo docker port lemp command. Next, we can connect to database:

$ mysql -uadmin -ppass -h 127.0.0.1 -P32772
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.5.53-0+deb8u1 (Debian)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

PHP 7

As already mentioned above, PHP 7 was compiled from a bleeding edge PHP 7 branch. The entire installation was performed into /usr/local/php-VERSION/ directory. From this reason to access the command line php binary you either run it with a full path:

# docker exec -it lemp /usr/local/php-7.1.0/bin/php -v
PHP 7.1.0 (cli) (built: Jan  1 2017 21:33:40) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.1.0, Copyright (c) 1999-2016, by Zend Technologies

or include /usr/local/php-7.1.0/bin/ as part of your PATH environmental variable.

Additional Information

Obtain a list of enabled PHP 7 modules

# docker exec -it lemp /usr/local/php-7.1.0/bin/php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dba
dom
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
intl
json
libxml
mbstring
mcrypt
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
pspell
readline
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlwriter
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache

Container Access

While your lemp container is running it can be access by:

$ sudo docker exec -it lemp /bin/bash
root@8306ab0e404e:/#

Reset MySQL user password

The below command will set a new password 123for theadmin user :

# mysql
mysql> SET PASSWORD FOR 'admin'@'%' = PASSWORD('123');

Restart lemp stack

$ sudo docker exec lemp service supervisor restart


Comments and Discussions
Linux Forum