Last active
December 17, 2015 11:19
-
-
Save unRob/5601644 to your computer and use it in GitHub Desktop.
nginx.conf en ubuntu me gusta así
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
TEST= | |
while getopts “t” OPTION | |
do | |
case $OPTION in | |
t) | |
TEST=1 | |
;; | |
esac | |
done | |
CPU_COUNT=$(cat /proc/cpuinfo | grep processor | wc -l) | |
PASSENGER='' | |
if which passenger-config &> /dev/null | |
then | |
echo "Configurando con Passenger $(passenger-config --version)" | |
PASSENGER=" | |
passenger_root $(passenger-config --root); | |
passenger_ruby $(which ruby); | |
" | |
fi | |
PHP='' | |
if which php &> /dev/null | |
then | |
echo "Configurando con PHP $(php -v | head -n 1)" | |
#socket= | |
PHP=" | |
upstream backend { | |
server unix:$(cat /etc/php5/fpm/pool.d/www.conf | grep ^listen | head -n 1 | cut -d ' ' -f3); | |
} | |
" | |
fi | |
if [ -z $TEST ] | |
then | |
echo "Creando directorios..." | |
mkdir -p /etc/nginx/conf.d | |
mkdir -p /var/log/nginx/ | |
mkdir -p /etc/nginx/sites-enabled | |
mkdir -p /etc/nginx/sites-available | |
fi | |
CONFIG=$(cat <<_CONF_ | |
user www-data; | |
worker_processes ${CPU_COUNT}; | |
pid /var/run/nginx.pid; | |
events { | |
worker_connections 768; | |
# multi_accept on; | |
} | |
http { | |
## | |
# Basic Settings | |
## | |
sendfile on; | |
tcp_nopush on; | |
tcp_nodelay on; | |
keepalive_timeout 65; | |
types_hash_max_size 2048; | |
# server_tokens off; | |
charset utf-8; | |
# server_names_hash_bucket_size 64; | |
# server_name_in_redirect off; | |
include /etc/nginx/mime.types; | |
default_type application/octet-stream; | |
## | |
# Logging Settings | |
## | |
access_log /var/log/nginx/access.log; | |
error_log /var/log/nginx/error.log; | |
## | |
# Gzip Settings | |
## | |
gzip on; | |
gzip_vary on; | |
gzip_static on; | |
# gzip_proxied any; | |
# gzip_comp_level 6; | |
# gzip_buffers 16 8k; | |
# gzip_http_version 1.1; | |
gzip_min_length 1100; | |
gzip_comp_level 1; | |
gzip_http_version 1.0; | |
gzip_proxied any; | |
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; | |
gzip_disable "msie6"; | |
## | |
# nginx-naxsi config | |
## | |
# Uncomment it if you installed nginx-naxsi | |
## | |
#include /etc/nginx/naxsi_core.rules; | |
## | |
# nginx-passenger config | |
## | |
# Uncomment it if you installed nginx-passenger | |
## | |
$PASSENGER | |
## | |
# PHP Configuration | |
## | |
$PHP | |
## | |
# Virtual Host Configs | |
## | |
include /etc/nginx/conf.d/*.conf; | |
include /etc/nginx/sites-enabled/*; | |
} | |
_CONF_ | |
) | |
if [ $TEST ] | |
then | |
echo "--- Config: ----" | |
echo "$CONFIG" | |
echo "----------------" | |
exit 0 | |
else | |
echo "Escribiendo a /etc/nginx/nginx.conf" | |
if [ -a /etc/nginx/nginx.conf ] | |
then | |
echo "Creando respaldo en /etc/nginx/nginx.conf.original" | |
mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.original | |
fi | |
echo "$CONFIG" >> /etc/nginx/nginx.conf | |
mkdir -p /domains/default | |
INDEX='index index.html;' | |
INCLUDE_PHP='' | |
if [[ ! -z "$PHP" ]]; then | |
PHPCONF=$( cat <<PHPC | |
location ~ ^.+\.php { | |
fastcgi_split_path_info ^(.+\.php)(.*)$; | |
fastcgi_pass backend; | |
fastcgi_index index.php; | |
fastcgi_param SCRIPT_FILENAME \$document_root/\$fastcgi_script_name; | |
include fastcgi_params; | |
fastcgi_param PATH_INFO \$fastcgi_path_info; | |
fastcgi_param PATH_TRANSLATED \$document_root/\$fastcgi_path_info; | |
fastcgi_param QUERY_STRING \$query_string; | |
fastcgi_param REQUEST_METHOD \$request_method; | |
fastcgi_param CONTENT_TYPE \$content_type; | |
fastcgi_param CONTENT_LENGTH \$content_length; | |
fastcgi_param DOCUMENT_ROOT \$document_root; | |
fastcgi_param SCRIPT_NAME \$fastcgi_script_name; | |
fastcgi_intercept_errors off; | |
fastcgi_ignore_client_abort off; | |
fastcgi_connect_timeout 60; | |
fastcgi_send_timeout 180; | |
fastcgi_read_timeout 180; | |
fastcgi_buffer_size 128k; | |
fastcgi_buffers 4 256k; | |
fastcgi_busy_buffers_size 256k; | |
fastcgi_temp_file_write_size 256k; | |
} | |
PHPC | |
) | |
echo "$PHPCONF" > /etc/nginx/conf.d/php.run | |
INDEX='index index.php index.html;' | |
INCLUDE_PHP='include /etc/nginx/conf.d/php.run;' | |
echo "<?php phpinfo();" > /domains/default/test.php; | |
fi | |
DEFAULT_SERVER=$( cat <<EOL | |
server { | |
listen 80; | |
server_name localhost; | |
root /domains/default; | |
$INDEX | |
$INCLUDE_PHP | |
} | |
EOL | |
) | |
echo "$DEFAULT_SERVER" > /etc/nginx/sites-available/default.conf | |
ln -sf /etc/nginx/sites-available/default.conf /etc/nginx/sites-enabled/default.conf | |
echo "<!DOCTYPE html><html><head><meta charset=\"utf-8\" /><title>Enginecs</title></head><body><h1>It Güorcs!</h1></body></html>" >> /domains/default/index.html | |
LOGROTATE=$(cat <<LR | |
/var/log/nginx/*.log { | |
weekly | |
rotate 4 | |
compress | |
delaycompress | |
create 0640 www-data adm | |
sharedscripts | |
postrotate | |
[ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid` | |
endscript | |
} | |
LR | |
) | |
echo "Configurando logrotate" | |
echo "$LOGROTATE" > /etc/logrotate.d/nginx | |
echo "Probando config" | |
nginx -t -c /etc/nginx/nginx.conf | |
if [ $? -ne 0 ] | |
then | |
echo "El test de nginx se cagó!" | |
echo "Ve que trae en ./nginx.conf" | |
mv /etc/nginx/nginx.conf . | |
mv /etc/nginx/nginx.conf.original /etc/nginx/nginx.conf | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment