Skip to content

Instantly share code, notes, and snippets.

@eduardoarandah
Created June 3, 2020 20:46
Show Gist options
  • Save eduardoarandah/332220462117822e6dc3983e7e07ce16 to your computer and use it in GitHub Desktop.
Save eduardoarandah/332220462117822e6dc3983e7e07ce16 to your computer and use it in GitHub Desktop.
Comandos para agregar un vhost con nginx y php
# Variables necesarias en tu entorno
W='/home/lalo/www'
VHOSTS_NGINX='/etc/nginx/sites-enabled'
USER_LOCAL='lalo'
#!/bin/bash
# u checar todas las variables
# e salir al primer error
# x mostrar todos los comandos
set -ue
if [ -z $1 ] ; then echo "Falta el parámetro 1 (alias)" && exit 1;fi
if [ ! -f ~/.localrc ]; then echo "ERROR: No existe .localrc"; exit 1; else source ~/.localrc; fi
if [ ! -d $W ]; then echo "dir $W no existe"; exit 1; fi
if [ ! -d $VHOSTS_NGINX ]; then echo "dir $VHOSTS_NGINX no existe"; exit 1; fi
CONFFILE=$VHOSTS_NGINX/${1}
##############
# Crear BD #
##############
sudo mysql -e "create database if not exists $1;"
#####################################
# AGREGAR EL VHOST
#####################################
cat > $CONFFILE << EOL
server {
listen 80;
listen [::]:80;
root $W/$1/public;
index index.html index.htm index.php;
server_name $1.local www.$1.local;
# favicon
location = /favicon.ico {
log_not_found off;
access_log off;
}
# robots
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny access to any files with a .php extension in the uploads directory
location ~* /(?:uploads|files)/.*\\.php$ {
deny all;
}
# Deny dotfiles
location ~ /\\. { deny all; }
# This is cool because no php is touched for static content.
# include the "?\$args" part so non-default permalinks doesn't break when using query string
location / {
try_files \$uri \$uri/ /index.php?\$args;
}
# Serve PHP
location ~ \\.php\$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
# Cache for images
location ~* \\.(js|css|png|jpg|jpeg|gif|ico)\$ {
expires max;
log_not_found off;
}
}
EOL
echo "SITIOS ACTIVOS:"
ls $VHOSTS_NGINX
##############
# CREAR DIR
##############
mkdir -p $W/$1/public
echo "cambiar permisos de $W/$1 a $USER_LOCAL:$USER_LOCAL"
sudo chown -R $USER_LOCAL:$USER_LOCAL $W/$1
# reiniciar nginx
echo "reiniciar nginx..."
sudo service nginx restart
echo "OK"
echo "iniciar php7.3 en caso de no estar activo"
sudo service php7.3-fpm start
#!/bin/bash
# u checar todas las variables
# e salir al primer error
# x mostrar todos los comandos
set -ue
if [ ! -f ~/.localrc ]; then echo "ERROR: No existe .localrc"; exit 1; else source ~/.localrc; fi
if [ ! -d $W ]; then echo "dir $W no existe"; exit 1; fi
if [ ! -d $VHOSTS_NGINX ]; then echo "dir $VHOSTS_NGINX no existe"; exit 1; fi
echo "sites enabled:"
ls -al $VHOSTS_NGINX
echo "Configuraciones en:"
echo "/etc/nginx/nginx.conf"
echo "/etc/nginx/sites-enabled"
#!/bin/bash
# u checar todas las variables
# e salir al primer error
# x mostrar todos los comandos
set -ue
if [ -z $1 ] ; then echo "Falta el parámetro 1 (alias)" && exit 1;fi
if [ ! -f ~/.localrc ]; then echo "ERROR: No existe .localrc"; exit 1; else source ~/.localrc; fi
if [ ! -d $W ]; then echo "dir $W no existe"; exit 1; fi
if [ ! -d $VHOSTS_NGINX ]; then echo "dir $VHOSTS_NGINX no existe"; exit 1; fi
CONFFILE=$VHOSTS_NGINX/${1}
echo "Borrar base de datos $1"
sudo mysql -e "drop database if exists $1;"
echo "Borrar VHOST $CONFFILE"
rm -f $CONFFILE
echo "Borrar carpeta $W/$1"
rm -rf $W/$1
echo "Reiniciar nginx"
sudo service nginx restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment