Introduction

 Quickly set up Apache, MySQL and PHP on Ubuntu optionally using FastCGI with PHP5-FPM - ideal for your busy site.

Benefits

PHP5-FPM with Apache with mpm-event reduces the number of processes which can increase performance by requiring less forks and less memory overall. In case you want to switch back to the standard mode, the commands are also listed.

Install all dependencies - without actually installing the typo3 packages themselves

export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install exim4 dbconfig-common debconf graphicsmagick lsb-base mysql-client php5 php5-cli php5-gd php5-mysql debconf

Enable multiverse and install php5-fpm

apt-get install software-properties-common -y
apt-add-repository multiverse
apt-get update
apt-get install apache2-mpm-event # might already be installed
apt-get install libapache2-mod-fastcgi php5-fpm

Create the php5-fpm configuration for apache2

cat > /etc/apache2/conf-available/php5-fpm.conf << EOF
<IfModule mod_fastcgi.c>
AddHandler php5-fcgi .php
Action php5-fcgi /php5-fcgi
Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization
<Directory /usr/lib/cgi-bin>
    Require all granted
</Directory>
</IfModule>
EOFi

Tune php5 for Typo3

cat > /etc/php5/mods-available/typo3_tune.ini << EOF
; configuration for php tuning 
; priority=20
max_execution_time = 240
memory_limit = 80M
upload_max_filesize = 1G
post_max_size = 1G
max_input_vars = 1500
EOF

php5enmod gd
php5enmod mysql
php5enmod typo3_tune

Change DocumentRoot to /var/www (instead of /var/www/html )

# vi /etc/apache2/sites-enabled/000-default.conf

sed -ie 's/DocumentRoot .*/DocumentRoot \/var\/www/' /etc/apache2/sites-enabled/000-default.conf

service apache2 restart
service php5-fpm restart

Optional: Check if php5-fpm is working

cat > /var/www/info.php << EOF
<?php phpinfo(); ?>
EOFi

Open http:// /info.php  and check for Server API = FPM/FastCGI

You should delete /var/www/info.php when done.

Enable UTF8 for mysql

Configure mysql for UTF8

cat > /etc/mysql/conf.d/utf8_charset.cnf << EOF
[mysqld]
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
[client]
default-character-set=utf8
EOFi

Create the Typo3 Database

mysqladmin create TYPO3

Allow the mysql user typo3 with password typo3 to use the above database. (use a different password !)

echo "grant all privileges on TYPO3.* to typo3@localhost identified by 'typo3'; flush privileges;" | mysql -u root

NOTE:

If you used a root password for mysql, use -p here.

Set mysql root password (optional)

View current users

mysql -e "SELECT User, Host, Password FROM mysql.user;"

Set all root passwords to ’newpwd'

mysql -u root -e "UPDATE mysql.user SET Password = PASSWORD('newpwd') WHERE User = 'root'; FLUSH PRIVILEGES;"

Typo3 Installation

Get and unpack the latest Typo3 Release

cd /var/www
curl -L  get.typo3.org/current | tar zxpvf -
# You can also use the following to get the latest release
# wget prdownloads.sourceforge.net/typo3/typo3_src-7.3.0.tar.gz
# wget get.typo3.org/current --content-disposition
# curl -L -o typo3_src.tgz get.typo3.org/current
# tar xpvf typo3_src-*.tar.gz
ln -s typo3_src-* typo3_src
ln -s typo3_src/typo3
ln -s typo3_src/index.php
mkdir typo3temp
mkdir typo3conf
mkdir uploads
chown www-data.www-data typo3* uploads /var/www
chmod 2775 /var/www
chmod -R 2775 typo3* uploads
chown root:www-data -R typo3_src-*
chmod -R 775 typo3_src-*

Enable the Install Tool

touch typo3conf/ENABLE_INSTALL_TOOL

and go to the Install Tool to set up Typo3 !

http:// <yoursite> /typo3/install

Enable php5-fpm, mpm_event for apache

a2dismod mpm_prefork php5
a2enmod actions fastcgi alias mpm_event
a2enconf php5-fpm
service apache2 restart

Return to default

If you prefer to run apache with mod-php5, you can easily switch back to mod-php/mpm_prefork for apache

a2dismod mpm_event fastcgi
a2enmod actions alias mpm_prefork php5
a2disconf php5-fpm
service apache2 restart

Optionally:

service php5-fpm stop
service php5-fpm disable

Google Pagespeed

Enable Optimization Module

Beta Version:

#!/bin/sh
curl -O dl-ssl.google.com/dl/linux/direct/mod-pagespeed-beta_current_amd64.deb
dpkg -i mod-pagespeed-beta_current_amd64.deb
service apache2 restart

Stable Version:

#!/bin/sh
curl -O dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_amd64.deb
dpkg -i mod-pagespeed-stable_current_amd64.deb
service apache2 restart

The bleeding edge development versions are on github : github.com/pagespeed/mod_pagespeed/releases

For Typo3 you can safely enable these additional options - use ModPagespeedDisallow to disable pagespeed for certain paths.

/etc/apache2/mods-available/pagespeed.conf has the original configuration, we will add our own file with the local changes now:

cat > /etc/apache2/conf-available/local_pagespeed.conf << EOF
<IfModule pagespeed_module>
    <Location /pagespeed_admin>
        Order allow,deny
        Allow from localhost
        Allow from 127.0.0.1
        SetHandler pagespeed_admin
    </Location>
    <Location /pagespeed_global_admin>
        Order allow,deny
        Allow from localhost
        Allow from 127.0.0.1
        SetHandler pagespeed_global_admin
    </Location>
    ModPagespeedFetchHttps enable
    ModPagespeedEnableFilters defer_javascript
    ModPagespeedEnableFilters move_css_to_head
    # ModPagespeedEnableFilters prioritize_critical_css
    ModPagespeedEnableFilters local_storage_cache
    ModPagespeedEnableFilters sprite_images
    # ModPagespeedEnableFilters insert_ga
    ModPagespeedEnableCachePurge on
    ModPagespeedDisallow "*/path/*"
</IfModule> 
EOF

a2enconf local_pagespeed.conf

Optimize your Webserver further

Convert .htaccess to apache config files

cat > htaccess_import.sh << XEOF
#!/bin/bash
for i in \$( find /var/www -name .htaccess )
do
ht_path=\$( dirname \$i )
ht_conf=\${ht_path//\//_}
# echo \${ht_conf} \${i}
cat > /etc/apache2/conf-available/\${ht_conf}.conf <<EOF
<Directory \$ht_path>
\$( cat \$i )
</Directory>
EOF
done
(cd /etc/apache2/conf-available/; a2enconf _var*)
echo "Check the configuration for AllowOverride All in these files (if any)"
find /etc/apache2/ -type f | xargs grep -li "AllowOverride.*All"
XEOF
chmod 755 htaccess_import.sh
./htaccess_import.sh