How to install TYPO3 8.1 with Nginx (LEMP) on Ubuntu 16.04

This tutorial shows how to install and run a TYPO3 (version 8.1) web site on a Ubuntu 16.04 system that has a Nginx web server installed instead of Apache. This kind of setup is often called LEMP = Linux + Nginx (pronounced "engine x") + MySQL + PHP). Nginx is a fast and efficient HTTP server that uses less resources than Apache and delivers pages a lot faster, especially static files.

 

1 Preliminary Note

I want to install TYPO3 in a vhost called www.example.com here with the document root /var/www/www.example.com/web.

You should have a working LEMP stack. If you don't have a LEMP installation yet, use this tutorial: Ubuntu LEMP Server

Because we have to run all the steps from this tutorial with root privileges, we can either prepend all commands in this tutorial with the string sudo, or we become root right now by typing

sudo su

I will use the nano editor to edit config files on the shell, nano can be installed with this command:

apt-get install nano 

 

2 Installing TYPO3

First we will install some additional PHP modules that are required by Typo3:

apt-get install php7.0-soap php7.0-zip

The document root of my www.example.com web site is /var/www/www.example.com/web - if it doesn't exist, create it as follows:

mkdir -p /var/www/www.example.com/web

Next, we download TYPO3 8.1 as a .tar.gz file from http://typo3.org/download/ and place it in our document root:

cd /var/www/www.example.com
wget get.typo3.org/8.1 -O typo3_src-8.1.2.tar.gz
tar xfz typo3_src-8.1.2.tar.gz
rm typo3_src-8.1.2.tar.gz
cd web
ln -s ../typo3_src-8.1.2 typo3_src
ln -s typo3_src/index.php index.php
ln -s typo3_src/typo3 typo3

It is recommended to make the document root and the TYPO3 files in it writable by the Nginx daemon which is running as user www-data and group www-data:

chown -R www-data:www-data /var/www/www.example.com

If you haven't already created a MySQL database for TYPO3 (including a MySQL TYPO3 user), you can do that as follows (I name the database typo3 in this example, and the user is called typo3_admin, and his password is typo3_admin_password):

mysql --defaults-file=/etc/mysql/debian.cnf
CREATE DATABASE typo3;
ALTER DATABASE typo3 CHARACTER SET utf8 COLLATE utf8_general_ci;
GRANT ALL PRIVILEGES ON typo3.* TO 'typo3_admin'@'localhost' IDENTIFIED BY 'typo3_admin_password';
GRANT ALL PRIVILEGES ON typo3.* TO 'typo3_admin'@'localhost.localdomain' IDENTIFIED BY 'typo3_admin_password';
FLUSH PRIVILEGES;
quit

Next we create a Nginx vhost configuration for our www.example.com vhost in the /etc/nginx/sites-available/ directory as follows:

nano /etc/nginx/sites-available/www.example.com.vhost
server {
       listen 80;
       server_name www.example.com example.com;
       root /var/www/www.example.com/web;

       if ($http_host != "www.example.com") {
                 rewrite ^ http://www.example.com$request_uri permanent;
       }

       index index.php index.html;

       location = /favicon.ico {
                log_not_found off;
                access_log off;
       }

       location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
       }

       # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
       location ~ /\. {
                deny all;
                access_log off;
                log_not_found off;
       }

        location ~ \.php$ {
                        try_files $uri =404;
                        include /etc/nginx/fastcgi_params;
                        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        fastcgi_intercept_errors on;
                        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 ~ /\.(js|css)$ {
                expires 604800s;
        }

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

        location ~* ^/fileadmin/(.*/)?_recycler_/ {
                deny all;
        }
        location ~* ^/fileadmin/templates/.*(\.txt|\.ts)$ {
                deny all;
        }
        location ~* ^/typo3conf/ext/[^/]+/Resources/Private/ {
                deny all;
        }
        location ~* ^/(typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) {
        }

        location / {
                        if ($query_string ~ ".+") {
                                return 405;
                        }
                        if ($http_cookie ~ 'nc_staticfilecache|be_typo_user|fe_typo_user' ) {
                                return 405;
                        } # pass POST requests to PHP
                        if ($request_method !~ ^(GET|HEAD)$ ) {
                                return 405;
                        }
                        if ($http_pragma = 'no-cache') {
                                return 405;
                        }
                        if ($http_cache_control = 'no-cache') {
                                return 405;
                        }
                        error_page 405 = @nocache;

                        try_files /typo3temp/tx_ncstaticfilecache/$host${request_uri}index.html @nocache;
        }

        location @nocache {
                        try_files $uri $uri/ /index.php$is_args$args;
        }

}

This configuration already contains everything that is needed for clean URLs (because of the try_files $uri $uri/ /index.php$is_args$args; line in the @nocache location).

Next, make sure you have the following line in /etc/nginx/mime.types:

nano /etc/nginx/mime.types
[...]
        text/x-component                        htc;
[...]

To enable the vhost, we create a symlink to it from the /etc/nginx/sites-enabled/ directory:

cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/www.example.com.vhost www.example.com.vhost

Reload Nginx for the changes to take effect:

service nginx reload

Create the FIRST_INSTALL file in the website root to enable the TYPO3 installer.

touch /var/www/www.example.com/web/FIRST_INSTALL

And change the owner to www-data so that the TYPO3 installation script can remove the file when the installation is finished.

chown www-data:www-data /var/www/www.example.com/web/FIRST_INSTALL

We have to adjust a few php.ini values to match the requirements for TYPO3. Open the php.ini file:

nano /etc/php/7.0/fpm/php.ini

And adjust the following config options so that they have these values:

[....]
max_execution_time=240
[....]
post_max_size = 20M
[....]
upload_max_filesize = 20M
[....]
max_input_vars=1500
[....]
memory_limit = 256M
[....]

Then save the file and restart PHP-FPM to load the new configuration:

service php7.0-fpm restart

Now we can launch the web-based TYPO3 installer by going to http://www.example.com/:

Start the TYPO3 installer.

Click on the "System looks good. Continue" button to start the installation process. In case that this page shows a message that not all prerequisites are fulfilled, then adjust your config first to match the requirements before you proceed with the installation:

Next fill in the database details (user: typo3_admin; password: typo3_admin_password from when we created the typo3 database), select connection type socket and click on Continue:

Enter the database User and password.

On the next page choose Use an existing empty database and pick typo3 from the drop-down menu. Then click on Continue:

Select a database for TYPO3.

Next, provide a username and password for the TYPO3 admin user and enter a name for your TYPO3 website. Click on Continue then:

Enter a TYPO3 username and password.

The installation is now finished. If you want to start with a demo website instead of a completely empty system, select the Yes, download the list of distributions option (this will not install demo data immediately, but just make a demo website available in the backend from where you can choose to install it). I'll choose here to not download any demo sites. Then click on Open the backend:

TYPO3 installation finished.

The admin area can be found under http://www.example.com/typo3/. Log in with the username admin and the password you defined during installation:

Typo3 Login

And login with the TYPO3 administrator user that you created above.

The TYPO3 8.1 CMS

And start to build your TYPO3 website. The frontend will show an error like "Service Unavailable (503) No pages are found on the root level!" until you added a root page in the backend. I recommend taking a look at the excellent TYPO3 documentation on how to create your first website in TYPO3 if you are not familiar with this CMS yet.

3 Virtual machine image download of this tutorial

This tutorial is available as ready to use virtual machine image in ovf/ova format for Howtoforge Subscribers. The VM format is compatible with VMWare and Virtualbox. The virtual machine image uses the following login details:

SSH / Shell Login

Username: administrator
Password: howtoforge

This user has sudo rights.

MySQL / MariaDB Login

Username: root
Password: howtoforge

The IP of the VM is 192.168.1.100, it can be changed in the file /etc/network/interfaces. 

TYPO3 Login

Username: admin
Password: howtoforge

Please change all the above passwords to secure the virtual machine.

 

Share this page:

4 Comment(s)