This document contains the content of /resources/other/debian_nginx_server.sh. Download the file by setting the ?act=download parameter, or access the raw file at either srcs.cc or src.cerium.cc.
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 #!/bin/bash sectionPrompt() { printf "\n" IFS= while true; do printf "=> $@ [Y/n]" read -rs -n 1 key case $key in 'Y'|'y'|' '|'' ) printf " starting.\n" return 0;; 'N'|'n'|$'\t' ) printf " skipping.\n" return 1;; $'\e' ) printf " exiting.\n" sleep 0.5 exit 0;; * ) printf " invalid input.\n";; esac done } # Development web server configuration if sectionPrompt "Set up as development web server?"; then release="bookworm" user="web-dev" php="8.2" dev="true" dev_tools="clang clang-19" proxy="false" # Main web server configuration elif sectionPrompt "Set up as production web server?"; then release="bookworm" user="web-main" php="8.2" dev="false" proxy="false" # Proxy configuration elif sectionPrompt "Set up as web proxy?"; then release="bookworm" user="proxy" php="8.2" dev="false" proxy="true" cloudflare_cred="./cloudflare.ini" else printf "\nEnd of choices. Exiting.\n" exit fi ## ## Configure sources and update ## sectionPrompt "Configure sources?" && { cat > /etc/apt/sources.list.d/debian.sources <<- EOF Types: deb URIs: https://deb.debian.org/debian Suites: $release $release-updates Components: main contrib Enabled: yes Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg Types: deb URIs: https://security.debian.org/debian-security Suites: $release-security Components: main contrib Enabled: yes Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg EOF cat > /etc/apt/sources.list <<- EOF # Sources should be defined in "/etc/apt/sources.list.d/debian.sources". # Old one-line-style sources can still be defined here if needed. EOF apt update } sectionPrompt "Upgrade system?" && { apt full-upgrade -y } ## ## Set up user ## sectionPrompt "Create user: $user?" && { deluser $user mkdir -p /home/$user adduser --home /home/$user $user } sectionPrompt "Set up sudo?" && { apt install -y sudo usermod -aG sudo $user } sectionPrompt "Increase user file limits?" && { cat > /etc/security/limits.d/$user.conf <<- EOF $user soft nofile 65536 EOF } ## ## Set up SSH ## sectionPrompt "Install SSH?" && { apt install -y openssh-server if [ "$dev" = "true" ]; then sed -i "s/^#\?AllowTcpForwarding .*/AllowTcpForwarding yes/" /etc/ssh/sshd_config fi systemctl enable ssh systemctl start ssh lan_addr="$(ip route get 255.255.255.255 | grep -oP '(?<=src )[^ ]*')" cat <<- EOS The following commands can be run on a device to be used as an SSH client. Create a new SSH key pair for web servers (or use an existing key): ssh-keygen -f ~/.ssh/id_ed25519_web -t ed25519 The copy the public key to this server (from local network): ssh-copy-id -i ~/.ssh/id_ed25519_web $user@$lan_addr Then add the following entry to ~/.ssh/config: Host $lan_addr HostName $lan_addr User $user IdentityFile ~/.ssh/id_ed25519_web EOS } ## ## Install development tools ## if [ "$dev" = "true" ]; then sectionPrompt "Install development tools?" && { apt install -y build-essential $dev_tools } fi ## ## Install Nginx ## sectionPrompt "Install Nginx?" && { apt install -y curl gnupg2 ca-certificates debian-archive-keyring curl https://nginx.org/keys/nginx_signing.key \ | gpg --dearmor \ | tee /usr/share/keyrings/nginx-archive-keyring.gpg > /dev/null cat > /etc/apt/sources.list.d/nginx.sources <<- EOF Types: deb URIs: http://nginx.org/packages/mainline/debian Suites: $release Components: nginx Enabled: yes Signed-By: /usr/share/keyrings/nginx-archive-keyring.gpg EOF echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \ | tee /etc/apt/preferences.d/99nginx apt update apt install -y nginx systemctl enable nginx } ## ## Configure Nginx ## sectionPrompt "Configure Nginx?" && { cp -r /etc/nginx /tmp/nginx-conf rm -rf /etc/nginx/* cp /tmp/nginx-conf/nginx.conf /etc/nginx/nginx.conf.bak cp /tmp/nginx-conf/mime.types /etc/nginx/mime.types.bak if [ -f /tmp/nginx-conf/fastcgi.conf ]; then cp /tmp/nginx-conf/fastcgi.conf /etc/nginx/fastcgi.conf.bak fi mkdir /etc/nginx/conf.d /etc/nginx/sites-enabled if [ -f /tmp/nginx-conf/conf.d/default.conf ]; then mv /tmp/nginx-conf/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak fi cp -r /tmp/nginx-conf/modules /etc/nginx/modules rm -rf /tmp/nginx-conf mkdir -p /etc/nginx/log # This directory must exist to prevent an Nginx alert on startup (not necessary) mkdir -p /var/log/nginx # Create nginx.conf if [ "$proxy" = "true" ]; then cat > /etc/nginx/nginx.conf <<- EOF user $user; worker_processes auto; worker_rlimit_nofile 65536; error_log /etc/nginx/log/error.log; include /etc/nginx/modules/*.conf; include /etc/nginx/conf.d/*.conf; pcre_jit on; events { worker_connections 65536; } http { # Server and header optimizations sendfile on; tcp_nopush on; tcp_nodelay on; server_tokens off; etag off; # Prevents using temporary files and reduces latency proxy_buffering off; #proxy_temp_path /tmp; # Consistent redirects server_name_in_redirect off; port_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; # Logging access_log /etc/nginx/log/access.log; log_not_found off; http2 on; # SSL ssl_protocols TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on; ssl_session_tickets off; # Gzip gzip on; gzip_vary on; gzip_proxied any; gzip_comp_level 2; gzip_http_version 1.1; gzip_types text/css application/javascript text/plain text/xml application/xml; # Hosts, redirects, and error configurations include /etc/nginx/sites-enabled/*.conf; include /etc/nginx/sites-enabled/redirects/*.conf; include /etc/nginx/sites-enabled/errors/*.conf; } EOF else cat > /etc/nginx/nginx.conf <<- EOF user $user; worker_processes auto; worker_rlimit_nofile 65536; error_log /etc/nginx/log/error.log; include /etc/nginx/modules/*.conf; include /etc/nginx/conf.d/*.conf; pcre_jit on; events { worker_connections 65536; } http { # Server and header optimizations sendfile on; tcp_nopush on; tcp_nodelay on; server_tokens off; etag off; # Consistent redirects server_name_in_redirect off; port_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; # Logging access_log /etc/nginx/log/access.log; log_not_found off; http2 on; # Gzip gzip off; # Host configurations include /etc/nginx/sites-enabled/*.conf; } EOF fi cat > /etc/nginx/fastcgi.conf <<- EOF fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 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 SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REQUEST_SCHEME $scheme; fastcgi_param HTTPS $https if_not_empty; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param REMOTE_USER $remote_user; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200; EOF cat > /etc/nginx/mime.types <<- EOF types { text/html html htm shtml; text/css css; text/xml xml; image/gif gif; image/jpeg jpeg jpg; application/javascript js; application/atom+xml atom; application/rss+xml rss; text/mathml mml; # Added sh and bash text/plain txt sh bash; text/vnd.sun.j2me.app-descriptor jad; text/vnd.wap.wml wml; text/x-component htc; image/avif avif; image/png png; image/svg+xml svg svgz; image/tiff tif tiff; image/vnd.wap.wbmp wbmp; image/webp webp; image/x-icon ico; image/x-jng jng; image/x-ms-bmp bmp; # Added font/ttf font/ttf ttf; font/woff woff; font/woff2 woff2; application/java-archive jar war ear; application/json json; application/mac-binhex40 hqx; application/msword doc; application/pdf pdf; application/postscript ps eps ai; application/rtf rtf; application/vnd.apple.mpegurl m3u8; application/vnd.google-earth.kml+xml kml; application/vnd.google-earth.kmz kmz; application/vnd.ms-excel xls; application/vnd.ms-fontobject eot; application/vnd.ms-powerpoint ppt; application/vnd.oasis.opendocument.graphics odg; application/vnd.oasis.opendocument.presentation odp; application/vnd.oasis.opendocument.spreadsheet ods; application/vnd.oasis.opendocument.text odt; application/vnd.openxmlformats-officedocument.presentationml.presentation pptx; application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx; application/vnd.openxmlformats-officedocument.wordprocessingml.document docx; application/vnd.wap.wmlc wmlc; application/wasm wasm; application/x-7z-compressed 7z; application/x-cocoa cco; application/x-java-archive-diff jardiff; application/x-java-jnlp-file jnlp; application/x-makeself run; application/x-perl pl pm; application/x-pilot prc pdb; application/x-rar-compressed rar; application/x-redhat-package-manager rpm; application/x-sea sea; application/x-shockwave-flash swf; application/x-stuffit sit; application/x-tcl tcl tk; application/x-x509-ca-cert der pem crt; application/x-xpinstall xpi; application/xhtml+xml xhtml; application/xspf+xml xspf; application/zip zip; application/octet-stream bin exe dll; application/octet-stream deb; application/octet-stream dmg; application/octet-stream iso img; application/octet-stream msi msp msm; audio/midi mid midi kar; audio/mpeg mp3; audio/ogg ogg; audio/x-m4a m4a; audio/x-realaudio ra; video/3gpp 3gpp 3gp; video/mp2t ts; video/mp4 mp4; video/mpeg mpeg mpg; video/quicktime mov; video/webm webm; video/x-flv flv; video/x-m4v m4v; video/x-mng mng; video/x-ms-asf asx asf; video/x-ms-wmv wmv; video/x-msvideo avi; } EOF } ## ## Proxy DNS and SSL ## if [ "$proxy" = "true" ]; then sectionPrompt "Create self-signed certificate?" && { apt install -y openssl # Create a self-signed certificate mkdir -p /etc/nginx/self-signed openssl req -x509 -newkey rsa:4096 \ -keyout /etc/nginx/self-signed/private.key \ -out /etc/nginx/self-signed/cert.pem \ -sha256 -days 36500 -nodes -subj "/CN=localhost" } sectionPrompt "Set up Certbot?" && { apt install -y certbot python3-certbot-dns-cloudflare # Example cloudflare.ini file # ## Cloudflare API token used by Certbot #dns_cloudflare_api_token = <api_token> # Prompts to create certificates printf "\n=> Enter domains to create certificates (space separated):\n" while true; do printf " -> Enter domains (leave blank to stop): " read -r -a domains if [ ${#domains[@]} -eq 0 ]; then break fi args="" for domain in "${domains[@]}"; do args+=("-d" "$domain") done certbot certonly --dns-cloudflare \ --dns-cloudflare-credentials "$cloudflare_cred" \ -d ${args[@]} done } fi ## ## Install and configure PHP ## sectionPrompt "Install PHP?" && { apt install -y php$php-fpm sectionPrompt "Configure PHP?" && { # Set user as PHP user sed -i "s/^;\?\(user\|group\|listen\.owner\|listen\.group\) = .*/\1 = $user/" /etc/php/$php/fpm/pool.d/www.conf sed -i "s/^;\?listen = .*/listen = \/run\/php\/php$php-fpm.sock/" /etc/php/$php/fpm/pool.d/www.conf # Set PHP error log to Nginx directory for convenience sed -i "s/^;\?error_log = .*/error_log = \/etc\/nginx\/log\/php-error.log/" /etc/php/$php/fpm/php-fpm.conf # Disable "X-Powered-By" PHP header sed -i "s/^;\?expose_php = On/expose_php = Off/" /etc/php/$php/fpm/php.ini } systemctl enable php$php-fpm } ## ## Set permissions ## sectionPrompt "Set user home and Nginx config directory owner to $user?" && { chown -R $user:$user /etc/nginx chown -R $user:$user /home/$user } ## ## Start services ## sectionPrompt "Start services?" && { systemctl restart nginx systemctl start php$php-fpm } More resources