PHP / MYSQL – Installer son environnement de développement web

Karudev Informatique > Blog > Web > PHP / MYSQL – Installer son environnement de développement web
Spread the love

Installer son environnement de  développement web

Docker et Vagrant
Il est bien loin le temps du bon vieux WAMP utilisé pour développer des petites applications PHP.

Le monde du WEB se professionnalise de plus en plus et des outils de virtualisation comme Vagrant et Docker sont de plus en plus utilisés en entreprise.

Je vais vous décrire rapidement comment mettre en place un environnement de développement en quelques minutes.

1 – Installer Virtualbox

Cette outil de virtualisation va vous permettre de virtualiser des OS linux sur Ubuntu ou Debian depuis votre machine physique Windows par exemple.

Télécharger Virtualbox

2 – Installer Vagrant

Vagrant va vous permettre de gérer votre machine virtuelle.
En effet, en vous basant sur un fichier de configuration VagrantFile, vous pourrez lancer votre machine virtuelle en effectuant un “vagrant up”.

Pour ce faire, cloner le repository suivant : Karudev – Vagrant

git clone https://github.com/karudev/vagrant.git

Puis placez vous dans le répertoire et changez juste les configurations git.

cd vagrant && vim VagrantFile
# -*- mode: ruby -*-
# vi: set ft=ruby :
 
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.
 
  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "ubuntu/trusty64"
  config.ssh.keys_only = false
  config.ssh.username = "vagrant"
  config.ssh.password = "vagrant"
  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false
 
  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # config.vm.network "forwarded_port", guest: 80, host: 8080
 
  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.10"
 
  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"
 
  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  #config.vm.synced_folder "./data", "/vagrant_data"
 
  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #    vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
      vb.memory = '6000'
	  vb.cpus = '2'
  end
  #
  # View the documentation for the provider you are using for more
  # information on available options.
 
  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end
 
  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  config.vm.provision "shell", inline: <<-SHELL
     apt-get update
     apt-get install -y git \
	 htop \
     apt-transport-https \
        ca-certificates \
	curl \
	software-properties-common
 
     curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
     apt-key fingerprint 0EBFCD88
     add-apt-repository \
        "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
	$(lsb_release -cs) \
	stable"
 
     apt-get update
     apt-get install -y docker-ce \
	 	npm
     chown -R vagrant:vagrant /usr/bin/docker
     chown -R vagrant:vagrant /var/run/docker.sock
 
	curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
	chmod +x /usr/local/bin/docker-compose
 
	cd /tmp \
	&& wget https://nodejs.org/dist/v7.10.1/node-v7.10.1.tar.gz \
	&& tar xvf node-v7.10.1.tar.gz \
	&& cd node-v7.10.1 \
	&& ./configure \
	&& make \
	&& make install \
	&& rm -rf node-v7.10.1
 
	git config --global user.email "renault@karudev.fr"	git config --global user.name "Dolyveen Renault"    git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
	git config --global alias.st "status"
	git config --global alias.co "checkout"	 
  SHELL
end

Puis lancer votre machine virtuelle comme ceci

vagrant up

La marchine est prête ! Parfait 🙂
Connectez vous de ce pas..

vagrant ssh

It’s works !

Karudev@MacBook-Pro-de-Dolyveen:~/Documents/web/vagrant$ vagrant ssh
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-117-generic x86_64)
 
 * Documentation:  https://help.ubuntu.com/
 
  System information as of Tue Feb  6 21:40:16 UTC 2018
 
  System load:                    0.0
  Usage of /:                     10.6% of 39.34GB
  Memory usage:                   9%
  Swap usage:                     0%
  Processes:                      117
  Users logged in:                1
  IP address for eth0:            10.0.2.15
  IP address for eth1:            192.168.33.10
  IP address for br-5888814addfa: 172.18.0.1
  IP address for docker0:         172.17.0.1
 
  Graph this data and manage this system at:
    https://landscape.canonical.com/
 
  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud
 
128 packages can be updated.
97 updates are security updates.
 
New release '16.04.3 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
 
 
Last login: Tue Feb  6 21:32:02 2018 from 10.0.2.2
vagrant@vagrant-ubuntu-trusty-64:~$

3 – Lancer la stack Docker

Positionner vous dans un répertoire “projects” par exemple et cloner le repository Karudev – Docker

vagrant@vagrant-ubuntu-trusty-64:~$ cd /home/vagrant/projects/
vagrant@vagrant-ubuntu-trusty-64:~/projects$ git clone https://github.com/karudev/docker-lamp-debian.git

Super !
Mais avant de lancer la stack PHP 7 / MYSQL, il est interessant d’analyser le fichier de configuration docker-compose.yml qui la génère.

vagrant@vagrant-ubuntu-trusty-64:~/projects$ cd docker-lamp-debian/
vagrant@vagrant-ubuntu-trusty-64:~/projects/docker-lamp-debian$ cat docker-compose.yml

On constate que :
– 1 container web est chargé. Il attend des sources php dans le dossier ./src/php et éventuellement un virtualhost dans ce fichier ./php-apache/vhosts/vhosts.conf
– 1 container db est chargé. Il met a disposition le serveur mysql avec les paramètres suivants :
* host : db
* user : root
* password : XXXX
* port : 3306
– 1 container phpmyadmin, afin d’avoir rapidement accès au client phpMyAdmin depuis 192.168.33.10:8080.

A noter qu’en commentaire, il y a la possibilité d’avoir rapidement un serveur mongodb et memcached pour les mordus de performances 🙂

version: '2'
 
services:
  web:
    container_name: php-apache
    build: ./php-apache
    ports:
      - 80:80
    links:
      - db
#      - memcached
    depends_on:
      - db
#      - memcached
#      - mongodb
    volumes:
      - ./src/php:/var/www/html
#      - ./php-apache/logs:/var/log/apache2
#      - ./php-apache/vhosts/vhosts.conf:/etc/apache2/sites-enabled/000-default.conf
#    - ./lib/:/usr/share/java/
 
  db:
    build: ./mysql
    container_name: mysql
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: XXXX
    volumes:
      - ./mysql/db:/var/lib/mysql
    command: mysqld --sql_mode=""
 
 # mongodb:
 #   image: mongo:3.2.10
 #   container_name: mongodb
 #   ports:
 #     - "27017:27017"
 #   volumes:
 #     - ./mongodb:/var/lib/mongodb
 #   restart: always 
 
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    environment:
     - PMA_ARBITRARY=1
    restart: always
    ports:
     - 8080:80
 
 # memcached:
 #   image: memcached:1.4.33
 #   container_name: memcached
 #   restart: always
 #   ports:
 #    - 11211:11211
 
volumes:
  src:
    driver: local
#  lib:
#    driver: local
  db:
    driver: local
 # mongodb:
 #   driver: local

Vous pouvez maintenant lancer votre stack comme ceci

docker-compose up -d

Testez la page http://192.168.33.10.
Have fun…

Vous avez des questions ou avez besoin d’un coup de main pour mettre en place vos environnements de développement, recette, preprod et prod ?

N’hésitez pas à me contactez : renault@karudev.fr

Dolyveen Renault
Consultant Informatique

Leave a Reply

Your email address will not be published. Required fields are marked *