Archivo de la etiqueta: git

Cómo crear un contenedor Docker con PHP y Nginx

La forma más sencilla para crear contenedores de Docker que contengan el servidor web Nginx y PHP instalado junto con las extensiones que necesitemos y otros servicios comunes en el desarrollo de aplicaciones web, es utilizando el generador de contenedores PhpDocker.io.

Al entrar en la página, nos muestra una breve descripción del servicio:

PhpDocker.io es una herramienta que te ayuda a construir el entorno de desarrollo típico de PHP en un contenedor de Docker con unos pocos clicks. Soporta los servicios más comunes (MySQL/MariaDB, Redis, Elasticsearch …), y más que están por llegar. Soporta PHP 7.1, además de la versión 7.0 y la 5.6.

Para generar nuestro contenedor pinchamos en el enlace Generator.

En el primer bloque, seleccionamos la configuración básica de nuestro contenedor:

  • Project name: El nombre de nuestro proyecto.
  • Base port: El puerto donde se ejecutará nuestro contenedor.
  • Application type: El tipo de aplicación de PHP (Genérica, Symfony, Phalcon 3, Silex). En esta opción, si por ejemplo elegimos Symfony, nos configurará Nginx para que funcione con una aplicación de Symfony (vinculando el archivo app.php como controlador frontal, etc).
  • Max upload size (MB): Límite de tamaño por archivo para las subidas al servidor.

En el segundo bloque, debemos especificar la versión que queremos de PHP y las extensiones que necesitemos.

php-config

Más abajo, el generador nos permite añadir otros servicios a nuestro contenedor múltiple. Estos servicios son MySQL, MariaDB, Postgres, Elasticsearch, Memcached, Redis y Mailhog.

Servicios PHP Docker

Y por último, pinchamos en el botón Generate project archive, lo que nos generará un archivo .zip que incluye lo siguiente:

  • docker-compose.yml
  • phpdocker: En esta carpeta se incluyen los servicios que hemos añadido junto con sus archivos de configuración.
  • Readme.htnl: Un archivo HTML donde nos indica cómo ejecutar nuestro contenedor.
  • Readme.md

He creado un repositorio en Github donde podéis ver como tengo configurado un proyecto con Symfony utilizando Nginx y PHP-FPM 7.1.

Además, dentro del contenedor de PHP-FPM he instalado las extensiones MySQL y LDAP de PHP junto con GIT y Composer.

Para cualquier duda o sugerencia podéis dejar un comentario en la entrada.

How to show the current GIT branch name in command prompt

git-branches

We can get the current GIT branch name in command prompt with adding a simple code into the .bashrc file.

Open your terminal and type the following command:

sudo nano ~/.bashrc

Go to the end of the file and add this code:

function parse_git_branch () {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
 
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
NO_COLOR="\[\033[0m\]"
 
PS1="$GREEN\u@\h$NO_COLOR:\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "

Save file changes.

Now, if you open a new terminal and access into a GIT repository, you will see the current GIT branch name.

How to manage a website with GIT

It is not known by many people, but we can use GIT to manage our website or web project in seconds, without the need to upload one by one all the files through a FTP clients.

Maybe, your are asking: Why I must start using it?

  • You can update your website to the last version in seconds.
  • You can go back to the lastest stable version in seconds in case you have any problem on production environment.
  • Prevent human errors when you are uploading many files with a FTP client.
  • Your website update is faster.
  • Prevent reupload unnecesary files.

If you want to try it, continue reading:

First, you need a Linux server with shell access and root permissions.

Obviously, you must have GIT installed in your server.

sudo apt-get install git

Once you have installed GIT, create a folder where you are going to save our GIT repositories.

mkdir /var/www/gitrep/project.git

Access to the just created folder

cd /var/www/gitrep/project.git/

Init a GIT bare repository

git init --bare

A bare repository, is a central repository. This means that is a repository where we only are going to do push from our project.

Now, we create a HOOK which is used for the auto-updating the folder where is located our web project in our web server when we do a push.

sudo vi /var/www/gitrep/project.git/hooks/post-update

Enter the next content:

GIT_WORK_TREE=/var/www/project/ git checkout -f

Replace ‘/var/www/project/’ for the path where is located your project folder in your web server. After this, save the file changes.

Then, you must set necessary permissions to the HOOK you created before.

chmod +x /var/www/gitrep/project.git/hooks/post-update

With the GIT repository configured in your web server, it is time to configure a few things in our local repository (working copy).

First, add remote repository to your local repository:

git remote add remote_repository_name ssh://user@host/var/www/git_rep/project.git

Set write permissions to your GIT repository

sudo chmod -R 0777 /var/www/git_rep/project.git

Push local repository content to remote repository

git push remote_repository_name +master:refs/heads/master

In case that your web server hasn’t updated your project with your local repository content, you must to do a local change in some of your project files, then create a new commit, and push to the remote repository again.

git push remote_repository_name master

And this is all you must to know. Now, when you do a push into your remote (production) server your project will be updated automatically.

If you have any question, tell us as a post comment.