LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-27-2019, 07:29 PM   #1
smudge|lala
Member
 
Registered: Jan 2004
Location: New Zealand
Distribution: Mint | Sabayon
Posts: 160

Rep: Reputation: 16
Apache2 and let's encrypt issues with www redirect


Hi all, it's been a while since I setup a webserver on a VPS and am having a few challenges, hopefully you can help? Please note example.com is apache default text, I have used 'mydomain' in place of my actual domain.

I'm using Apache2 which is the new version and does not have httpd anymore it uses sites-available and sites-enabled in /etc/apache2.

I've enabled let's encrypt for SSL but only one domain is working, redirects are failing even with .htaccess in /var/www/html/.htaccess and permission set to 644.

Code:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
What I've done:

1. I setup two A records to point my one domain to my VPS IP for mydomain.com and www.mydomain.com
2. I created a single sites-enabled example.conf (maybe my error is here and I need to create a second) which contains:

Code:
Alias /example "/var/www/mydomain/"

<Directory /var/www/mydomain/>

Options +FollowSymlinks

AllowOverride All

<IfModule mod_dav.c>

Dav off

</IfModule>

SetEnv HOME /var/www/mydomain

SetEnv HTTP_HOME /var/www/mydomain

</Directory>
There are two other .conf files present from install/setup and they read:

Code:
<IfModule mod_ssl.c>
<VirtualHost *:443>
	# The ServerName directive sets the request scheme, hostname and port that
	# the server uses to identify itself. This is used when creating
	# redirection URLs. In the context of virtual hosts, the ServerName
	# specifies what hostname must appear in the request's Host: header to
	# match this virtual host. For the default virtual host (this file) this
	# value is not decisive as it is used as a last resort host regardless.
	# However, you must set it for any further virtual host explicitly.
	#ServerName www.example.com

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html

	# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
	# error, crit, alert, emerg.
	# It is also possible to configure the loglevel for particular
	# modules, e.g.
	#LogLevel info ssl:warn

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined

	# For most configuration files from conf-available/, which are
	# enabled or disabled at a global level, it is possible to
	# include a line for only one particular virtual host. For example the
	# following line enables the CGI configuration for this host only
	# after it has been globally disabled with "a2disconf".
	#Include conf-available/serve-cgi-bin.conf
ServerName mydomain.com
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias www.mydomain.com
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>
And

Code:
<VirtualHost *:80>
	# The ServerName directive sets the request scheme, hostname and port that
	# the server uses to identify itself. This is used when creating
	# redirection URLs. In the context of virtual hosts, the ServerName
	# specifies what hostname must appear in the request's Host: header to
	# match this virtual host. For the default virtual host (this file) this
	# value is not decisive as it is used as a last resort host regardless.
	# However, you must set it for any further virtual host explicitly.
	#ServerName www.example.com

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html

	# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
	# error, crit, alert, emerg.
	# It is also possible to configure the loglevel for particular
	# modules, e.g.
	#LogLevel info ssl:warn

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined

	# For most configuration files from conf-available/, which are
	# enabled or disabled at a global level, it is possible to
	# include a line for only one particular virtual host. For example the
	# following line enables the CGI configuration for this host only
	# after it has been globally disabled with "a2disconf".
	#Include conf-available/serve-cgi-bin.conf
RewriteEngine on
RewriteCond %{SERVER_NAME} =mydomain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
mydomain.com opens as SSL but I cannot redirect www.mydomain.com to this SSL. Do I need to create another sites-available and sites-enabled for www.mydomain.com? Perhaps with:

Code:
Alias /example "/var/www/www.mydomain/"

<Directory /var/www/www.mydomain/>

Options +FollowSymlinks

AllowOverride All

<IfModule mod_dav.c>

Dav off

</IfModule>

SetEnv HOME /var/www/www.mydomain

SetEnv HTTP_HOME /var/www/www.mydomain

</Directory>
Do I even need .htaccess in this case with two sites setup correctly? I'm a bit confused and making a mess.. how should this be done?
 
Old 02-27-2019, 07:54 PM   #2
smudge|lala
Member
 
Registered: Jan 2004
Location: New Zealand
Distribution: Mint | Sabayon
Posts: 160

Original Poster
Rep: Reputation: 16
Ok I just read that I should replace:

Code:
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.domain.tld [OR]
RewriteCond %{SERVER_NAME} =domain.tld
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
and edit the port 80 conf to read:

Code:
ServerName mydomain.com
ServerAlias www.mydomain.com
ServerAdmin contact@domain.tld
DocumentRoot /var/www/html

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Redirect permanent / https://mydomain.com/
</VirtualHost>
$ sudo systemctl restart apache2.service

And it works. Is this the correct way to go? Presumably I can erase the .htaccess file?
 
  


Reply

Tags
apache2, certificate, httpd, ssl



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Let's Automate Let's Encrypt LXer Syndicated Linux News 0 11-01-2016 06:51 AM
LXer: Let's Encrypt Aiming to Encrypt the Web LXer Syndicated Linux News 0 09-13-2016 03:11 AM
LXer: Encrypt Early, Encrypt Often! LXer Syndicated Linux News 0 08-10-2011 02:30 AM
crypt() perl function to encrypt Password in shell scripts or How Encrypt passwords ? balakrishnay Linux - General 13 01-14-2010 09:35 AM
encrypt and decrypt using encrypt(char block[64], int edflag) rockwell_001 Linux - Security 3 08-30-2009 09:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 05:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration