Configure two websites in the same instance in Liferay 7

One of the most common configurations we want to apply in Liferay is to serve two or more websites with the same Liferay instance. However, configuring Liferay in the right way to serve our websites and generate the links properly is not an easy and immediate task.

This guide contains a set of steps you can follow in order to configure two websites in the same Liferay instance. We are supposing you have a regular Liferay configuration with these points:

  • Tomcat is serving Liferay in the port 8080
  • Apache is getting the HTTPS client’s requests (port 443)

Step 1. Configure Virtual Hosts in Apache

A virtual host configuration for domain-one.com (Apache)

This is an example of a virtual host configuration file compatible with Apache. This configuration needs the mod_proxy and mod_ssl enabled.

<IfModule mod_ssl.c>
	<VirtualHost _default_:443>
		# Your server name should be set here
        ServerName domain-one.com

		DocumentRoot /var/www/html

		ErrorLog ${APACHE_LOG_DIR}/error_domain-one.com.log
		CustomLog ${APACHE_LOG_DIR}/access_domain-one.com.log combined

		# We turn SSL Engine on
		SSLEngine on
        SSLProtocol -ALL +TLSv1 +TLSv1.1 +TLSv1.2
        SSLCipherSuite ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256

        # We provide the certificates
		SSLCertificateFile	/path/to/ssl/certs/domain-one.com.crt
        SSLCertificateChainFile /path/to/ssl/certs/ca.crt
		SSLCertificateKeyFile /path/to/ssl/certs/domain-one.com.key

		<FilesMatch "\.(cgi|shtml|phtml|php)$">
				SSLOptions +StdEnvVars
		</FilesMatch>

		# This allows or prevents Apache httpd from functioning as a forward proxy server. (Setting ProxyRequests to Off does not disable use of the ProxyPass directive.)
		# In a typical reverse proxy or gateway configuration, this option should be set to Off.
		ProxyRequests Off

		# We configure our mod_proxy module (including the reverse proxy)
		# This line is VERY important, since it will allow us to pass the Host: line from the incoming request to the proxied host, 
		# instead of the hostname specified in the ProxyPass line
        ProxyPreserveHost On
        ProxyTimeout 300

        # This directive allows remote servers to be mapped into the space of the local server. The local server does not act as a proxy in the conventional sense but appears to be a mirror of the remote server. 
        # The local server is often called a reverse proxy or gateway. 
        ProxyPass        /  http://192.168.1.203:8080/

		# This directive lets Apache httpd adjust the URL in the Location, Content-Location and URI headers on HTTP redirect responses. 
		# This is essential when Apache httpd is used as a reverse proxy (or gateway) to avoid bypassing the reverse proxy because of HTTP redirects on the backend servers 
		# which stay behind the reverse proxy.
        
        ProxyPassReverse /  http://192.168.1.203:8080/

	</VirtualHost>
</IfModule>

A virtual host configuration for domain-two.com (Apache)

Repeat the previous configuration just replacing the “domain-one.com” configuration by “domain-two.com”.

Step 2. Configure Proxy Connector in Tomcat

Following is a fragment of the server.xml file available in a path similar to /path/to/liferay/tomcat/conf/server.xml

<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN" address="192.168.1.203">
  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">
    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" address="192.168.1.203" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8"
               proxyPort="443"
               scheme="https"
              />
    </Engine>
  </Service>
</Server>

The important part of the previous file is the <Connector />. Please, be aware we ARE NOT setting the proxyName which could be configured to one valid domain like domain-one.com.

Caution. Following configuration is NOT going to work since we are setting the proxyName which will modify the Host request header.
<Connector port="8080" address="192.168.1.203" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8"
               proxyPort="443"
               proxyName="domain-one.com"
               scheme="https"
              />
Correct. Now, if we remove the proxyName, our Liferay websites would be ready to keep the Host request header and generate the links as expected.
<Connector port="8080" address="192.168.1.203" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8"
               proxyPort="443"
               scheme="https"
              />

Step 3. Configure Virtual Hosts for Public / Private Pages in Liferay

Go to Your Site > Site Settings > General > Site URL and configure your “Friendly URL”, “Public Pages” and “Private Pages”. Notice this last “Private Pages” is empty to Liferay can autogenerate the URLs.

This process needs to be repeated for both “domain-one.com”, “domain-two.com” and any other domain you want to configure in the same Liferay instance.

That’s it! From now on, your Liferay instance is going to be able to serve many sites with custom domains.

See you next time!

Best regards.

3 comments on “Configure two websites in the same instance in Liferay 7”

  1. Will you be adding content for Step 3 at some point? Very interested in reading about your solution and wondering why only part of this article displays. Hoping there’s more…

  2. Hi, Alex
    I just tried this with my Liferay server, but I miss the configuration for
    web.server.http.port = 80
    web.server.https.port = 443
    web.server.host=domain-one.com
    web.server.protocol=https

    If I do not have web.server.* all the links Imges and Stylesheets gets not loaded, but
    if I put that, the domain-two.com will redirected to domain-one.com on the fist link that i klick on domain-two.com

    I now why this is like this, so my question,
    how you config liferay to serve the webPage links in the correct form without having the web.server.* properties set?

    Greetings Jens

Leave a Reply

Your email address will not be published. Required fields are marked *