-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebtec_lamp
More file actions
173 lines (132 loc) · 6.34 KB
/
Copy pathwebtec_lamp
File metadata and controls
173 lines (132 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash
# Antes de ejecutar este script, asegúrate de editar las siguientes variables de configuración:
# Variables de configuración
domain_name="your_domain.com"
php_timezone="America/New_York" # Ajusta la zona horaria de PHP
email="your_email@example.com" # Tu dirección de correo electrónico
# Pregunta al usuario si los parámetros son correctos
echo "Recuerda editar este script antes de ejecutarlo, verifica los siguientes parámetros de configuración:"
echo "Dominio: $domain_name"
echo "Zona horaria de PHP: $php_timezone"
echo "Correo electrónico: $email"
read -p "¿Son correctos? (Sí/No): " answer
if [[ $answer =~ ^[Ss][Ii]$ ]]; then
# Los parámetros son correctos, continúa con el script
echo "Continuando con la ejecución del script..."
# Verifica si el dominio ya existe en la configuración de Apache
if [ -f "/etc/httpd/conf.d/$domain_name.conf" ]; then
echo "El dominio $domain_name ya existe en la configuración de Apache. Saliendo."
exit 1
fi
# Directorio base para el sitio web del dominio
webroot="/var/www/$domain_name"
# Verifica si el directorio del dominio ya existe
if [ -d "$webroot" ]; then
echo "El directorio $webroot ya existe. Saliendo."
exit 1
fi
# Actualiza el sistema
sudo yum update -y
# Instala Apache y GIT
sudo yum install httpd git -y
# Inicia Apache y habilita su inicio en el arranque
sudo systemctl start httpd
sudo systemctl enable httpd
# Abre el puerto 80 en el firewall para HTTP
service firewalld restart
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
service firewalld restart
# Instala Certbot y el complemento de Apache para SSL
sudo yum install certbot python3-certbot-apache -y
# Crea el directorio del dominio
sudo mkdir -p $webroot
# Crea un archivo de configuración de VirtualHost para Apache
sudo tee /etc/httpd/conf.d/$domain_name.conf > /dev/null <<EOF
<VirtualHost *:80>
ServerAdmin webmaster@$domain_name
ServerName $domain_name
DocumentRoot $webroot
# Mejoras de seguridad en la configuración de Apache
# ServerTokens Prod
# ServerSignature Off
# TraceEnable Off
# FileETag None
Header set X-Content-Type-Options "nosniff"
Header set X-Frame-Options "SAMEORIGIN"
# Habilitación de HTTP/2
Protocols h2 http/1.1
# Ajustes de rendimiento
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
Timeout 60
ErrorLog /var/log/httpd/$domain_name-error.log
CustomLog /var/log/httpd/$domain_name-access.log combined
</VirtualHost>
EOF
# Las líneas de texto que deseas agregar al final del archivo
nuevas_lineas="ServerTokens Prod\n ServerSignature Off\n TraceEnable Off\n KeepAlive On\n MaxKeepAliveRequests 100\n KeepAliveTimeout 5\n Timeout 60"
# Ruta al archivo httpd.conf
archivo_httpd="/etc/httpd/conf/httpd.conf"
# Verifica si el archivo existe
if [ -e "$archivo_httpd" ]; then
# Agrega las nuevas líneas al final del archivo
echo -e "$nuevas_lineas" >> "$archivo_httpd"
echo "Líneas agregadas exitosamente."
else
echo "El archivo $archivo_httpd no existe."
fi
# Configura permisos para el directorio del dominio
sudo chown -R apache:apache $webroot
sudo chmod -R 755 $webroot
# Recarga la configuración de Apache
sudo systemctl reload httpd
# Ejecuta Certbot para obtener y configurar el certificado SSL
sudo certbot --apache -d $domain_name -m $email --agree-tos --no-eff-email -n
# Instala MariaDB y asegura la instalación
sudo yum install mariadb-server -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
# Instala PHP y módulos comunes
sudo dnf install php php-common php-mysqlnd php-gd php-xml php-mbstring -y
# Configura PHP con la zona horaria seleccionada y ajustes de rendimiento y seguridad
sudo sed -i "s/;date.timezone =/date.timezone = $php_timezone/" /etc/php.ini
sudo sed -i "s/expose_php = On/expose_php = Off/" /etc/php.ini
sudo sed -i "s/memory_limit = 128M/memory_limit = 256M/" /etc/php.ini
sudo sed -i "s/post_max_size = 8M/post_max_size = 32M/" /etc/php.ini
sudo sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 32M/" /etc/php.ini
sudo sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /etc/php.ini
sudo sed -i "s/max_execution_time = 30/max_execution_time = 60/" /etc/php.ini
sudo sed -i "s/max_input_time = 60/max_input_time = 120/" /etc/php.ini
sudo sed -i "s/;realpath_cache_size = 4096k/realpath_cache_size = 4096k/" /etc/php.ini
sudo sed -i "s/;realpath_cache_ttl = 120/realpath_cache_ttl = 600/" /etc/php.ini
sudo sed -i "s/disable_functions =/disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source/" /etc/php.ini
# Configura PHP-FPM para un mejor rendimiento
sudo sed -i "s/;pm.max_children = 5/pm.max_children = 20/" /etc/php-fpm.d/www.conf
sudo sed -i "s/;pm.start_servers = 2/pm.start_servers = 4/" /etc/php-fpm.d/www.conf
sudo sed -i "s/;pm.min_spare_servers = 1/pm.min_spare_servers = 2/" /etc/php-fpm.d/www.conf
sudo sed -i "s/;pm.max_spare_servers = 3/pm.max_spare_servers = 6/" /etc/php-fpm.d/www.conf
sudo sed -i "s/;pm.max_requests = 500/pm.max_requests = 1000/" /etc/php-fpm.d/www.conf
# Reinicia PHP-FPM
sudo systemctl restart php-fpm
# Instala y habilita mod_security
sudo yum install mod_security -y
sudo systemctl restart httpd
# Copia el archivo de configuración de ejemplo y ajusta las reglas
sudo sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /etc/httpd/modsecurity.d/modsecurity.conf
# Descarga las reglas OWASP ModSecurity Core Rule Set (CRS)
sudo git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git /etc/httpd/modsecurity.d/owasp-crs
sudo mv /etc/httpd/modsecurity.d/owasp-crs/crs-setup.conf.example /etc/httpd/modsecurity.d/owasp-crs/crs-setup.conf
# Incluye las reglas OWASP en el archivo de configuración principal de mod_security
echo "IncludeOptional /etc/httpd/modsecurity.d/owasp-crs/*.conf" | sudo tee -a /etc/httpd/modsecurity.d/modsecurity.conf
# Reinicia Apache para aplicar las configuraciones
sudo systemctl restart httpd
echo "El servidor LAMP con el VirtualHost para $domain_name y SSL habilitado se ha instalado correctamente. Puede acceder a su sitio web en https://$domain_name"
exit 0
else
# Los parámetros no son correctos, muestra un mensaje de error y sale del script
echo "Por favor, edita los parámetros de configuración antes de ejecutar el script."
exit 1
fi