Apache

From campisano.org
Jump to navigation Jump to search

Apache directory listing themed by Apaxy

  • NOTE: define a global folder installation path

for example, imagine

/somewhere/
 |-- site/
     |-- app/         # as YOUR_APACHE_APP_DIR_OUTSIDE_APACHE
     |-- www/         # as YOUR_APACHE_ROOT_DIR
         |-- files/   # as YOUR_FOLDER_TO_HAVE_FILELIST

so, the result will be

/somewhere/
 |-- site/
     |-- app/
         |-- apaxy-master/
     |-- www/
         |-- _dir_theme -> ../app/apaxy_master/apaxy/theme
         |-- files
             |-- .htaccess -> ../../app/apaxy_master/apaxy/htaccess.txt

Installation:

# global apaxy install, to be actived locally
cd YOUR_APACHE_APP_DIR_OUTSIDE_APACHE
wget https://github.com/AdamWhitcroft/Apaxy/archive/master.zip
unzip master.zip
rm master.zip
mv apaxy-master/apaxy/theme/htaccess.txt apaxy-master/apaxy/theme/.htaccess
mv apaxy-master/apaxy/htaccess.txt apaxy-master/apaxy/htaccess.txt_orig
cat apaxy-master/apaxy/htaccess.txt_orig | sed -e 's^theme^/_dir_theme^g' -e 's^//_dir_theme^/_dir_theme^g' > apaxy-master/apaxy/htaccess.txt
rm apaxy-master/apaxy/htaccess.txt_orig
cd YOUR_APACHE_ROOT_DIR
ln -s YOUR_APACHE_APP_DIR_OUTSIDE_APACHE/apaxy-master/apaxy/theme _dir_theme
# local apaxy activation
cd YOUR_FOLDER_TO_HAVE_FILELIST
ln -s YOUR_APACHE_APP_DIR_OUTSIDE_APACHE/apaxy-master/apaxy/htaccess.txt .htaccess

Reference

Php as fcgi

apt-get update
apt-get install apache2 apache2-mpm-worker libapache2-mod-fcgid apache2-suexec php5-cgi
a2dismod php5
a2enmod include
a2enmod actions
a2enmod rewrite
a2enmod suexec
a2enmod fcgid
/etc/init.d/apache2 stop
cd /srv/config/init.d
ln -s /etc/init.d/apache2 apache2
cd ../rc.d
ln -s ../init.d/apache2 S75_apache2
ln -s ../init.d/apache2 K25_apache2
cd /etc/apache2/sites-available
cat > SITE.WHR << EOF
<VirtualHost *:80>
    ServerName SITE.WHR

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^SITE.WHR
        RewriteRule ^/(.*)$ http://www.SITE.WHR/$1 [L,R=301]
    </IfModule>
</VirtualHost>

<VirtualHost *:80>
    ServerName www.SITE.WHR
    ServerAdmin root@SITE.WHR
    DocumentRoot /srv/domain/SITE.WHR/www

    LogLevel warn
    ErrorLog /srv/domain/SITE.WHR/log/error.log
    CustomLog /srv/domain/SITE.WHR/log/access.log combined

    <Directory /srv/domain/SITE.WHR/www>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all

        <IfModule mod_fcgid.c>
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

            <IfModule dir_module>
                DirectoryIndex index.php index.html
            </IfModule>

            # mediawiki pretty url
            <IfModule mod_rewrite.c>
                RewriteEngine On
                RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/mediawiki/index.php [L]
                RewriteRule ^/*$ %{DOCUMENT_ROOT}/mediawiki/index.php [L]
            </IfModule>

            AddHandler fcgid-script .php
            FCGIWrapper /usr/bin/php-cgi .php
        </IfModule>
    </Directory>
</VirtualHost>
EOF
cd ../sites-enabled
rm 000-default
ln -s ../sites-available/SITE.WHR 000-SITE.WHR
nano ../mods-available/fcgid.conf
<IfModule mod_fcgid.c>
  AddHandler	fcgid-script .fcgi
  FcgidConnectTimeout 20
### ADD FOLLOW
  FcgidMaxProcesses 5
  FcgidMinProcessesPerClass 1
  FcgidMaxProcessesPerClass 3
  FcgidIdleTimeout 30
  FcgidIdleScanInterval 10
  FcgidMaxRequestsPerProcess 100
  FcgidFixPathinfo 1
### END
</IfModule>
/etc/init.d/apache2 stop
/etc/init.d/apache2 start

Optimization

this post shows how to estimate a good limit for apache workers

Php as module

Apache

root@host:~# apt-get install gcc g++ autoconf automake make gettext binutils file libtool pkg-config libssl-dev libxml2-dev libpcre3-dev libbz2-dev libjpeg62-dev libpng12-dev libxpm-dev libgd2-xpm-dev libcurl4-openssl-dev ssl-cert libmcrypt-dev libmhash-dev libtidy-dev libpq-dev libmysqlclient15-dev libsqlite3-dev libapr1-dev libaprutil1-dev libpspell-dev
root@host:~# wget http://opensource.become.com/apache/httpd/httpd-2.2.17.tar.bz2
root@host:~# tar -xjf httpd-2.2.17.tar.bz2
root@host:~# cd httpd-2.2.17
root@host:~/httpd-2.2.17# ./configure --prefix=/usr/local/apache
root@host:~/httpd-2.2.17# make
root@host:~/httpd-2.2.17# make install
root@host:~# groupadd -g 5001 apache
root@host:~# useradd -g 5001 -u 5001 -d /usr/local/apache apache
root@host:~# chown -R apache:apache /usr/local/apache

Php

root@host:~# wget http://museum.php.net/php5/php-5.3.3.tar.bz2
root@host:~# tar -xjf php-5.3.3.tar.bz2
root@host:~# cd php-5.3.3
root@host:~/php-5.3.3# ./configure --prefix=/usr/local --enable-force-cgi-redirect --enable-fastcgi
root@host:~/php-5.3.3# make
root@host:~/php-5.3.3# make install

SuPhp

root@host:~# wget http://www.suphp.org/download/suphp-0.7.1.tar.gz
root@host:~# tar -xzf suphp-0.7.1.tar.gz 
root@host:~# cd suphp-0.7.1
root@host:~/suphp-0.7.1# ./configure --prefix=/usr/local/suphp --with-apr=/usr/local/apache/bin --with-apxs=/usr/local/apache/bin/apxs
root@host:~/suphp-0.7.1# make
root@host:~/suphp-0.7.1# make install

CORS Origin workaround

<IfModule mod_headers.c>
    # from http://stackoverflow.com/a/22331292
    # tested at http://regexr.com/3e33k
    SetEnvIf Origin "^(https?://(?:[\w-]+\.?)+(?::\d{1,5})?)(?:/.*)?$" CORS_ALLOW_ORIGIN=$1
    Header set Access-Control-Allow-Origin %{CORS_ALLOW_ORIGIN}e env=CORS_ALLOW_ORIGIN
    Header merge Vary "Origin"
    Header set Access-Control-Allow-Credentials "true"
    Header set Access-Control-Max-Age "1728000"
</IfModule>

MPM

Worker

Notes

from stackoverflow.com apache default vhost

http://httpd.apache.org/docs/1.3/vhosts/name-based.html

(Should be true for 2.x also)

"If no matching virtual host is found, then the first listed virtual
host that matches the IP address will be used.

As a consequence, the first listed virtual host is the default virtual
host. The DocumentRoot from the main server will never be used when an
IP address matches the NameVirtualHost directive. If you would like to
have a special configuration for requests that do not match any
particular virtual host, simply put that configuration in a container
and list it first in the configuration file."


from http://wiki.apache.org/httpd/PerformanceScalingUp

Requests for dynamically generated content usually take significantly
more resources than requests for static content. Static content
consists of simple filespages, images, etc.-on disk that are very
efficiently served. Many operating systems also automatically cache
the contents of frequently accessed files in memory.

outro link interessante: http://wiki.apache.org/httpd/PerformanceScalingOut

o que acredito: client pede uma página para o apache, o apache avalia o percurso (virtualhost), se o client usa cache o apache verifica a data do file, de qualquer jeito pega o file e entrega; o varnish recebe a requisição, gera um hasmap e verifica na cache dele, se tem na cache entrega, senão pede para o apache.

Em caso de disponibilidade de memória, conteúdo muito requisitado tem muita probabilidade de já estar na cache RAM do OS, nesse caso uma leitura de um file resulta de uma leitura em um setor de memória já preenchido na leitura anterior; em caso não esteja na memória, o tempo de carregamento de file no disco deveria ser parecido. O apache poderia ter uma sobrecarga para coisas que faz amais do varnish (segurança, sessão, rewrite..)...

Um pequeno ganho vezes muitas requisiçoes pode dar resultados, mas do monitoramento que temos não parece perceptível.


para ver as portas abertas

   netstat -tulpn


para ver o trafico em entrada, o comando a seguir da para entender:

   tcpdump -n -i eth0 host 187.126.64.20 and not tcp port 22 and not tcp port 80


count dos processos do apache

   ps ax | grep httpd | wc -l