Web Servers :: TYPO3 CMS & SEO
ARM Technologies   ...  Linux Web Servers 

Popular web servers

Some basic Apache & Nginx tips

TYPO3 websites need special settings on Apache and nginx web servers. Here we are providing a regular configuration of TYPO3 vhost on apache and nginx. Tips include .htaccess rules and settings for TYPO3 websites. We also informed how to turn off php notices with .htaccess, enabling vhost in apache, reloading apache configuration.

How to enable a website (vhost) in Apache

To manage several virtual hosts for apache web server, it is advisable to create separate conf files under /sites-available folder. Apache creates a symbolic link to /sites-enabled folder for all active websites. The following is the command to enable a website present in the file /sites-available/website2.conf

sudo ap2ensite website2

sudo apache2 reload

Typical apache vhost configuration for TYPO3 websites

<virtualhost>
ServerName yourtypo3.com
DocumentRoot /var/www/html/yourtypo3
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<directory /var/www/html/yourtypo3/>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</directory>
</virtualhost>

.htaccess settings for TYPO3 websites

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]

RewriteRule ^fileadmin/(.*/)?_recycler_/ - [F]
RewriteRule ^fileadmin/templates/.*(\.txt|\.ts)$ - [F]
RewriteRule ^typo3conf/ext/[^/]+/Resources/Private/ - [F]
RewriteRule ^(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
RewriteRule ^typo3$ typo3/index_re.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule .* index.php [L]
</IfModule>

Typical nginx vhost configuration for TYPO3

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;
    index index.php index.html index.htm;
    server_name yourdomain.com www.yourdomain.com;
        
    location ~ \.(gif|png|jpe?g|pdf)$ {
        valid_referers none blocked yourdomain.com www.yourdomain.com;
        if ($invalid_referer) {
            return 403;
        }
    }

    location /fileadmin/user_upload {
        valid_referers none blocked yourdomain.com www.yourdomain.com;
        if ($invalid_referer) {
            return 403;
        }
    }

    location ~ \.php$ {
        try_files $uri =404;
           fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        include fastcgi_params;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 256 16k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_read_timeout 1200;
    }
    
    client_max_body_size 100M;
    
    location ~ /\.(css|js|gif|png|jpg|svg)$ {
        expires 604800s;
    }

    location ~ ^/typo3//gfx/i/ {
        allow all;
    }

    location ~ /\. {
          deny all;
          access_log off;
          log_not_found off;
    }
    
    location (\.(bak|config|sql(\.zip|\.gz|\.bz2)?|ini|log|sh|inc|swp|t3d)|~)$ {
          deny all;
          access_log off;
          log_not_found off;
    }

    location ~ ^/fileadmin/(.*/)?_recycler_/ {
          deny all;
          access_log off;
          log_not_found off;
    }
    
    location ~ ^/typo3conf/ext/[^/]+/Resources/Private/ {
          deny all;
          access_log off;
          log_not_found off;
    }

    if (!-e $request_filename) {
          rewrite ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ $1.$3 last;
    }
    
    location / {
          try_files $uri $uri/ /index.php$is_args$args;
    }
}

Turn off PHP notices via .htacces file

php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_value docref_root 0
php_value docref_ext 0