# ----------------------------------------------------------------------------- # WEB SUNUCU NOTLARI # ----------------------------------------------------------------------------- Bu dökümanda, web sunucu kurarken gerekli olabilecek notlar yer alır. # ----------------------------------------------------------------------------- # KAYNAKLAR # ----------------------------------------------------------------------------- # How To Use Nginx as a Global Traffic Director on Debian or Ubuntu https://www.digitalocean.com/community/tutorials/how-to-use-nginx-as-a-global-traffic-director-on-debian-or-ubuntu # ----------------------------------------------------------------------------- # GENEL SUNUCU AYARLARI # ----------------------------------------------------------------------------- # /etc/sysctl.conf Debian Jessie için bu değerleri set etmeye gerek yok, default değerler uygun. # SYN cookie aktif net.ipv4.tcp_syncookies=1 # SYN-ACK gondermek icin kac kere deneme yapilacagi (default 5) net.ipv4.tcp_synack_retries=2 # TIME_WAIT durumundaki kaynaklar tekrar kullanilmaya calisilacak mi (default 0) net.ipv4.tcp_tw_reuse=1 net.ipv4.tcp_tw_recycle=1 # TIME_WAIT durumunda ne kadar bekletilecegi (default 60) net.ipv4.tcp_fin_timeout=15 # /etc/security/limits.conf Debian Jessie-systemd bu dosyadaki değerleri dikkate almıyor. Aynı iş için '/etc/systemd/system.conf' ve '/etc/systemd/user.conf' dosyalarının düzenlenmesi gerekiyor. Yalnız Jessie'deki default değerler iyi, düzenlemeye gerek yok. www-data hard nofile 65536 www-data soft nofile 65536 # ----------------------------------------------------------------------------- # APACHE2 # ----------------------------------------------------------------------------- # Kurulum # PHP ile kullanilacaksa aptitude install apache2-mpm-prefork libapache2-mod-php5 php5-cli \ php5-curl php5-gd php5-mcrypt php5-mysql # Thread sorunu olmayan bir dil ile kullanılacaksa aptitude install apache2-mpm-worker # Ayarlar # Ayarların doğruluğunun test edilmesi restart veya reload yapmadan önce ayarların doğruluğu test edilsin. apachectl configtest /etc/init.d/apache2 reload # Yoğun sunucularda apache2-mpm-prefork için curl site.com | grep "src=" | wc -l ile bir bağlantıda yaklaşık kaç adet istek olabileceğini tahmin et. Buna göre MaxKeepAliveRequests değerini belirle. ps aux | grep apache2 ile RSS satırına bakıp her bağlantı için fiziksel hafızadan yaklaşık kaç KB kullanıldığını kontrol et. [Apacheye_ayrilan_RAM] / [RSS_degeri] formülü ile MaxClients değerinin kaç olabileceğini tahmin et. MaxClients 256'dan büyük olacaksa ServerLimit değerini ona eşitle. /etc/apache2/apache2.conf Timeout 45 KeepAlive On MaxKeepAliveRequests 256 StartServers 5 MinSpareServers 10 MaxSpareServers 40 ServerLimit 256 MaxClients 256 MaxRequestsPerChild 2000 # Güvenlik /etc/apache2/conf.d/security ServerTokens Prod ServerSignature Off TraceEnable Off # Enable edilecek modüller a2enmod rewrite # Sitelerin kendi kurallarını yazabilmesi için /etc/apache2/sites-enabled/xxx AllowOverride All # Apache'yi yeniden başlat. /etc/init.d/apache2 restart # Could not reliably determine the server's fully qualified domain name Bu uyarı çıkıyorsa iptal etmek için /etc/apache2/conf.d/servername ServerName localhost # ----------------------------------------------------------------------------- # APACHE2 SELF-SIGNED CERTIFICATE # ----------------------------------------------------------------------------- # SSL desteğini aktif hale getirme. a2enmod ssl a2ensite default-ssl /etc/init.d/apache2 restart # Custom sertifika mkdir /etc/apache2/ssl cd /etc/apache2/ssl openssl req -x509 -nodes -sha256 -days 1826 -newkey rsa:2048 \ -keyout hostkey.key -out hostcert.pem - "Organization Name" bölümün uniq bir firma adı girilecek. Böylece, web tarayıcıda bu sertifika için ayrı bir authority tanımlamak mümkün olacak. - "Common Name" bölümüne, siteye girerken kullanılan alan adı veya IP adresi girilecek. Yoksa web tarayıcıda, otomatik tanınan self-signed sertifika kaydı oluşturulamıyor. chmod 600 * # Oluşan sertifikayı incelemek için openssl x509 -in hostcert.pem -text -noout # /etc/apache2/sites-enabled/default-ssl #SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem #SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key SSLCertificateFile /etc/apache2/ssl/hostcert.pem SSLCertificateKeyFile /etc/apache2/ssl/hostkey.key # restart /etc/init.d/apache2 restart # Apache Server Status # mod_status aktif hale getirilecek. Default aktif. a2enmod status # İzin veriler IP adresleri /etc/apache2/mods-enabled/status.conf Deny from all Allow from 127.0.0.1 ::1 Allow from 10.20.30.0/24 # İzleme http://ip_adresi/server-status # ----------------------------------------------------------------------------- # FIREFOX'DA SELF-SIGNED SERTİFİKAYA OTOMATİK ONAY # ----------------------------------------------------------------------------- # Siteye girilip sertifikaya onay verilir, web sayfası açılır. # Preferences -> View Certificates -> Servers bölümünden sertifika seçilip "export" edilir. Dosya uzantısı olarak ".pem" kullanılsın. # Seçili sertifika, Servers bölümünden "Delete" düğmesine tıklanarak silinir. # Preferences -> View Certificates -> Authorities bölümüne geçilir. "Import" ile bir önceki adımda oluşturulan ".pem" uzantılı dosya alınır. # Açılan pencerede "Trust this CA to identify websites" seçeneği işaretlenir. # ----------------------------------------------------------------------------- # APACHE SUBSTITUTE # ----------------------------------------------------------------------------- Regex kullanılabilir. http://httpd.apache.org/docs/current/mod/mod_substitute.html ... ... AddOutputFilterByType SUBSTITUTE text/html Substitute s/foo/bar/ # ----------------------------------------------------------------------------- # APACHETOP # ----------------------------------------------------------------------------- # Kurulum aptitude install apachetop # Izleme apachetop -f /var/log/apache2/access.log # ----------------------------------------------------------------------------- # NGINX AS REVERSE PROXY # ----------------------------------------------------------------------------- Yoğun sitelerde Apache önüne Nginx, reverse proxy olarak konulabilir. # Kurulum aptitude install nginx-extras libapache2-mod-rpaf libapache2-mod-rpaf, Nginx'ten gelen taleplerde Apache'nin gerçek IP adresini yakalayabilmesi için kuruluyor. # Değişecek Apache2 ayarları. /etc/apache2/ports.conf NameVirtualHost *:8080 Listen 8080 /etc/apache2/sites-available/* # Nginx ayarlarının doğruluğunun test edilmesi Servis restart edilmeden önce mutlaka yapılsın. /etc/init.d/nginx configtest # Ana ayar dosyasında yapılabilecek bazı ayarlar /etc/nginx/nginx.conf worker_processes # CPU sayisi kadar olacak. # Tek islemcili makinede latency'yi azaltmak icin 2 veya 4 denenebilir. worker_rlimit_nofile 32768; # limits.conf'daki nofile degerini asmasin. events { worker_connections 8192; # process basina maksimum baglanti sayisi. # reverse proxy oldugunda x/4 eszamanli kisi baglanabilir. # Genelde kisi basina 2 connection ve 2 de backend web sunucu icin. http { keepalive_timeout 2; client_max_body_size 16M; # maksimum upload boyu icin gerekli. large_client_header_buffers 8 64k; # client headers'i icin buffer boyutu. server_tokens off; # Sunucu hakkinda bilgi vermesin proxy_hide_header X-Powered-By; proxy_hide_header X-AspNet-Version; gzip on; gzip_disable "msie6"; gzip_min_length 1400; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; # Cache alanı ayarları /etc/nginx/conf.d/cache.conf proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=cache1:64m max_size=8192m inactive=600m; proxy_temp_path /var/cache/nginx/tmp; # Site ayarlarında yapılabilecek bazı ayarlar /etc/nginx/sites-enabled/default # Yonlendirme: domain.com -> www.domain.com server { listen 80; server_name domain.com; rewrite ^/(.*) http://www.domain.com/$1 permanent; } server { listen 80; listen [::]:80 default ipv6only=on; server_name www.domain.com www2.domain.com; root /var/www; access_log /var/log/nginx/access.log; # /istatistik icin alias location ~ ^/istatistik($|/.*) { proxy_pass http://localhost:8080; alias /var/www/istatistik$1; } # Tasarimla ilgili statik dosyalarini dogrudan gonder, expire suresi kisa location ~* ^.+\.(css|js|xml|rss)$ { access_log off; expires 60s; } # Resim dosyalarini dogrudan gonder location ~* ^.+\.(jpg|jpeg|gif|png|ico|bmp)$ { gzip off; access_log off; expires 30d; } # Uygulama dosyalarini dogrudan gonder location ~* ^.+\.(zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|rtf)$ { access_log off; expires 30d; } # Video/ses dosyalarini dogrudan gonder location ~* ^.+\.(mp3|mp4|avi|flv|swf|mid|midi|wav|bmp)$ { gzip off; access_log off; expires 30d; } # Diger dosyalari backend sunucudan (Apache'den) al location / { proxy_pass http://localhost:8080; proxy_http_version 1.1; proxy_connect_timeout 3; proxy_read_timeout 180; proxy_set_header Connection ""; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; add_header X-Node "root"; } # Backend sunucudan alinip cachede bir sure tutulacak dosyalar location ~* ^\/haberler\/.+\.(htm|html|php)$ { proxy_pass http://localhost:8080; proxy_http_version 1.1; proxy_connect_timeout 3; proxy_read_timeout 180; proxy_set_header Connection ""; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_ignore_headers Expires Cache-Control Set-Cookie; proxy_cache cache1 proxy_cache_valid 200 302 300s; proxy_cache_valid 301 120s; proxy_cache_valid 404 120s; proxy_cache_valid 500 120s; proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_504; add_header Cache-Control "must-revalidate, proxy-revalidate"; add_header X-Cached $upstream_cache_status; add_header X-Node "haberler"; more_clear_headers Pragma; expires 1200s; } } server { listen 80; listen [::]:80; server_name static.domain.com; root /var/www/static; access_log /var/log/nginx/access.log; # Diger dosyalari backend sunucudan (Apache'den) al location / { access_log off; expires 30d; } } # Location direktifi # Literal string match. Host adindan sonra gelen kismin # bas tarafi ile eslestirir. En uygun locationi bulana # kadar aramaya devam eder. location / { } location /media/ { } location /media/video/ { } location /media/voice/ { } # Literal string match. Uyan location bulundugunda # aramayi birakir. location ^~ /media/ { } # Exact string match. Location taniminin tam uymasi gerekir. # Uygun location bulundugunda aramayi birakir. location = / { } # Case-sensitive regex location ~ ^/Media(/|/index\.html)$ { } location ~ ^/media(/|/index\.html)$ { } # Case-insensitive regex location ~* ^/media(/|/index\.html)$ { } # location oncelikleri . Öncelikle "exact string match" var mi diye bakılır. Varsa arama bitirilir. . Literal string match için bakılır. ^~ kullanıldıysa eşleşen bir location bulundu ise arama bırakılır. Kullanılmadıysa locationlara bakılmaya devam edilir. . Regex araması yapılır. Uyan bir regex varsa arama bırakılır. . Uyan regex çıkmadıysa literal string match ile bulunan en uygun location kullanılır. # Cookie'ye göre proxy_cache aktif hale getirilecekse: server { ... set $is_authenticated 0; if ($http_cookie ~* "mycookie=([0123456789ABCDEF]+)") { set $is_authenticated 1; } location ... { proxy_pass localhost:8080; proxy_no_cache $is_authenticated; proxy_cache_bypass $is_authenticated; proxy_cache one; proxy_cache_valid 200 302 120s; proxy_cache_valid 301 10s; proxy_cache_valid 404 10s; proxy_cache_use_stale error timeout updating; proxy_ignore_headers Expires Cache-Control Set-Cookie; #access_log off; expires 120s; } # Gönderilen headeri silme (nginx-extras gerekiyor) Location bölümünde more_clear_headers Pragma; # Cache durumunu takip için header ekleme add_header X-Cached $upstream_cache_status; add_header X-Node "blok_kodu"; # ----------------------------------------------------------------------------- # AWSTATS # ----------------------------------------------------------------------------- Web istatistikleri # Kurulum aptitude install awstats aptitude install libgeo-ip-perl libgeoip1 geoip-database # Ayarlar dosyası /etc/awstats/awstats.conf LogFile="/home/awstats/access_merged.log" LogFormat=1 SiteDomain="Emrah" HostAliases="www.emrah.com localhost 127.0.0.1" DNSLookup=0 LoadPlugin="geoip GEOIP_STANDARD /usr/share/GeoIP/GeoIP.dat" Lang="en" # Apache ayarları vim /etc/apache2/sites-enabled/000-default # awstats Alias /awstats "/var/www/awstats" ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all # awstats icin parola gerekiyor AuthName "Guvenli Giris" AuthType Basic AuthUserFile /home/www-data/htaccess require valid-user # awstats icon Alias /awstats-icon/ /usr/share/awstats/icon/ Options None AllowOverride None Order allow,deny Allow from all # Nginx awstats ayarları (Apache yoksa) aptitude install fcgiwrap spawn-fcgi apache2-utils /etc/nginx/sites-available/default # /awstats-icon/ location ^~ /awstats-icon { access_log off; alias /usr/share/awstats/icon/; add_header Cache-Control "must-revalidate, proxy-revalidate"; expires 30d; } # /awstats/ location ~* (^/awstats$|^/awstats/$|awstats.pl$) { auth_basic "Restricted"; auth_basic_user_file /etc/nginx/htpasswd; access_log off; alias /usr/lib/cgi-bin/awstats.pl; include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/fcgiwrap.socket; fastcgi_param SCRIPT_FILENAME /usr/lib/cgi-bin/awstats.pl; } # Nginx-Awstats için authentication cd /etc/nginx htpasswd -c -m htpasswd kullanici chmod 640 htpasswd chown root:www-data htpasswd /etc/init.d/nginx configtest /etc/init.d/nginx restart # Web sayfası /var/www/awstats/index.html awstats # Nginx ve Apache loglarını birleştirme Bu klasor ve dosyalar root'a ait /home/awstats/awstats_merge.sh #!/bin/bash MERGE="/usr/share/awstats/tools/logresolvemerge.pl" DIR="/home/awstats" LOG_APACHE="/var/log/apache2/access.log" LOG_NGINX="/var/log/nginx/access.log" cp $LOG_APACHE $DIR/access_apache.log cp $LOG_NGINX $DIR/access_nginx.log $MERGE $DIR/access_apache.log $DIR/access_nginx.log >$DIR/access_merged.log.tmp mv $DIR/access_merged.log.tmp $DIR/access_merged.log # Crontab ayarları /etc/crontab 25 * * * * root /home/awstats/awstats_merge.sh >/dev/null 2>& # Doğrudan Apache loguna erişilecekse izinleri düzenlemek gerekiyor. Cron'dan çalışan awstats uygulaması www-data hakları ile çalışmakta. /etc/logrotate.d/apache2 #create 640 root adm create 644 root adm chmod 755 /var/log/apache2 chmod 644 /var/log/apache2/*.log