Setting Up Apache Web Server

broken image


Setting Up an Apache Container One of the amazing things about the Docker ecosystem is that there are tens of standard containers that you can easily download and use. In the following example we will instantiate an Apache 2.4 container named tecmint-web, detached from the current terminal. Configure the Apache vhost With the domains working properly, we need to configure Apache to route the domain names to our site directory. Locate and navigate to your Apache configuration files directory. On Ubuntu this is located at /etc/apache2. The Apache web server checks first if the user name provided may access this directory; If the name matches, the web server check if the password provided by the user matches the password in the htpasswd file; If the authentication was fine, the user can access the files provided in the directory.

Setting up Apache Web Server

The alternative to using a vendor-based installer is to set up Apache server by building and installing directly from the source code. This approach enables you to set up Apache server in platform-independent manner available for all operating systems. Apache is an open source web server that's available for Linux servers free of charge. In this tutorial we'll be going through the steps of setting up an Apache server. What you'll learn. How to set up Apache; Some basic Apache configuration; What you'll need. Ubuntu Server 16.04 LTS; Secure Shell (SSH) access to your server.

Now that we've got a server in the cloud, we can install and configure the Apache web server for our site. The first step is using the cloud console to SSH to the server as shown in the last post. Once there, we will use the yum package manager to install the web server, as shown below:

sudo yum -y update
sudo yum -y install httpd mod_ssl

Now let's see if it works. Try to start the server with sudo systemctl start httpd and then point your browser to the IP address of your server. You should see a default welcome page. If you use https, you'll see a browser warning because the installed certificate is self-signed. We'll fix that latter problem in the next post using Let's Encrypt and certbot.

Assuming the page fetch worked, issue one more command to make the web server start automatically after a reboot: sudo systemctl enable httpd.

Setting up apache web server online

We have the web server, but we still need to configure it. We're going to want this to be our site's primary web page. I'm using the domain bibliote.ch for this, so I'm going to want http://bibliote.ch, http://www.bibliote.ch, and https://bibliote.ch all to redirect to https://www.bibliote.ch, which is where I'll serve all my content.

My first step is out of scope for these posts: I need to set up two DNS A records, both pointing to my site's external IP address. Those records will be for the names @ (which is for the bare domain name) and www. I have my DNS hosted by my domain registrar, and each registrar has some way to do this through their web page. Once you set this up it will take a while for the changes to occur, but we can continue with configuration while waiting. We can't use Let's Encrypt, though, until the name records are live.

The second step is to configure the web server to deal with those redirects.

The configuration files are installed in /etc/httpd. The main server configuration is in /etc/httpd/conf/httpd.conf and the secure server configuration is in /etc/httpd/conf.d/ssl.conf. We will need to edit each of these files. Our regular account doesn't have permission to write these files, so we will use sudo vi /etc/httpd/conf/httpd.conf and sudo vi /etc/httpd/conf.d/ssl.conf to edit them.

The regular, non-secure, configuration changes are easier. In fact, we just need to add two lines, which can go almost anywhere in the file. I put them near the end, just before the last line that includes other files. Here they are:

Setting Up Apache Web Server On Ec2

RewriteEngine on
RewriteRule '^/(.*)' 'https://www.bibliote.ch/$1' [R,L]

The first line turns on the module that allows directives that rewrite browser requests. The second line says that any request for a URL starting (^) with a slash (/, which all start with from the server's perspective), followed by anything (.*), should result in telling the browser to instead request the page https://www.bibliote.ch/$1, where the $1 will be replaced by the matched part of the URL in the parentheses. The [R,L] at the end of the line means to send a redirect response to the client and to stop looking for and applying other rewrite rules.

I also made an unnecessary, but recommended, change by uncommenting the line that starts with #ServerName and replacing it with ServerName http://www.bibliote.ch:80.

After making those changes, I gracefully restarted the service with the command sudo service httpd graceful. After that, pointing the browser to the external IP address caused the browser to redirect to https://www.bibliote.ch, which is what I want. Once the DNS changes propagated, pointing to http://bibliote.ch or http://www.bibliote.ch each also pointed correctly to https://www.bibliote.ch.

We just need to make a couple more Apache configuration tweaks. We want https://www.bibliote.ch to be the canonical URL for the site, so we need https://bibliote.ch to redirect there, too.

The secure site is controlled by a file in /etc/httpd/conf.d: ssl.conf. That provides a single virtual host running TLS on port 443, so requests to any name that resolves to the external IP will be served by it. We're going to need two such virtual hosts, one for https://bibliote.ch that redirects all requests, and finally one for https://www.bibliote.ch that handles the web pages.

The easiest way I see to do this is to copy the ssl.conf file to ssl-www.conf in the same directory. They will both be loaded in alphabetical order. We will then edit ssl.conf to serve a redirect for https://bibliote.ch and edit ssl-www.conf to serve the canonical site https://www.bibliote.ch.

After copying the file, edit ssl.conf as follows:

  • Uncomment the line starting with #ServerName, and change the hostname in that line to your bare domain name (bibliote.ch in this case).
  • Near the end, just before the line, add the two lines to serve the redirect, just as we did for the regular sites:

RewriteEngine on
RewriteRule '^/(.*)' 'https://www.bibliote.ch/$1' [R,L]

Finally, edit ssl-www.conf. Get rid of all the lines before the one, because they would repeat what's in ssl.conf. Then uncomment the #ServerName line and change the host name to your canonical site's name: http://www.bibliote.ch in this case. Finally, uncomment the #DocumentRoot line so that it will point to /var/www/html, which is where you'll put your content. Use sudo vi /var/www/html/index.html to create a holder web page:

Setting Up Apache Web Server Download Windows 10



Placeholder

Placeholder


Setting Up Apache Web Server On Windows 10

Now reload the configuration files with sudo service httpd graceful, and try each of your possible site names. All of them should redirect to https://www.bibliote.ch, though each secure site will show a security warning because we don't have valid certificates yet. That's the topic for the next post.





broken image