Apache – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Wed, 07 Sep 2022 12:46:28 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 Simple Redirects with .htaccess https://tecadmin.net/simple-redirects-with-htaccess/ https://tecadmin.net/simple-redirects-with-htaccess/#respond Mon, 15 Aug 2022 15:37:06 +0000 https://tecadmin.net/?p=31184 Google Chrome is one of the most widely used web browsers in the world. Unfortunately, that also means that a lot of users will see broken links on your website if you don’t take precautions to prevent it. Re directing or ‘Redirecting’ an old URL to a new one is one such precaution you can [...]

The post Simple Redirects with .htaccess appeared first on TecAdmin.

]]>
Google Chrome is one of the most widely used web browsers in the world. Unfortunately, that also means that a lot of users will see broken links on your website if you don’t take precautions to prevent it. Re directing or ‘Redirecting’ an old URL to a new one is one such precaution you can take.

There are two redirect types:

  • Permanent Redirect: A 301 Redirect is a permanent redirection. When a user types in a URL and gets redirected to another page, the new page comes up with a fresh title and description in the search engine results. The user will not be able to see the previous URL, and the previous URL will be replaced with the new URL in the browser’s history.
  • Temporary Redirect: The 302 Redirect is a temporary redirect. Once the user clicks on the link from the new page, the browser will show the old URL in the address bar. However, if the user finds the page through a search engine, the page title and description remain the same. A 302 Redirect can be used to redirect the user to another page temporarily, but it is not ideal for redirecting to a permanent URL.

Redirect Syntax

Apache mod_alias module provides a Redirect directive that used to make temporary or permanent redirects. The basic syntax of Redirect is:

Redirect [status] [URL-path] URL

Here

  • The Redirect is a directive to maps an old URL into a new one. The keyword is case-sensitive.
  • The status can be either 301 for permanent redirects or 302 for temporary redirects. We can also use keywords instead permanent or temp.
  • The old URL-path is is the case-sensitive path that begins with a slash. It’s optional with settings, the default will redirect the entire site.
  • The new URL is the new URL to redirect. It can be the directory path (URL-path) beginning with a slash (/) or an absolute URL beginning with a scheme and hostname.

Redirect Examples

Let’s discuss a few examples of redirecting domains or URLs to other URLs.

  1. Redirect one page to another: Sometimes you changed the permalink (URL) of any page. Then you can redirect all users to a new page, who are still connecting to the old page.
    # Redirect to a new URL on the same host
    Redirect 301 "/old-path" "/new-new"
    Redirect 301 "/app/services.html" "/app/v2/services.html"
    
  2. Redirect to other domains: This is useful when you want to redirect users to a page hosted on other domains.
    # Redirect to a URL on a different host
    Redirect 301 "/app/service" "https://app.example.com/service"
    
  3. Redirect the entire website: If you have planned to change your domain name. It will be the best practice to configure 301 redirects for your entire website to a new domain. That will help you to restore all SEO.
    # Redirect the entire website to a new domain
    Redirect "/" "https://example.net"
    

    All the URLs and sub URLs of the website will be redirected to new https://example.net.

Benefits of using .htaccess to implement redirects

You don’t have to change the content of your website. This means that you don’t have to worry about making sure the content remains the same. You can add redirects without changing the content at all.

You don’t have to worry about Google penalizing your website. When you change the content on a page, you can trigger a penalty from Google. However, Google understands that redirects are essential for a healthy website.

Limitations of using .htaccess for 301 Redirects

If you are transferring a website to a new domain, you will probably want to change the content in the source code to redirect visitors to the new domain. Using a .htaccess redirect will only redirect the URL, but will not change the content.

Editing the .htaccess file is often a quick way of doing things, but it can also be a quick way of breaking things. If you make a mistake while editing the .htaccess file, you might break the whole site.

You cannot use .htaccess to redirect users from one subdomain to another subdomain. For example, if you have www.example.com and example.com as subdomains, you can’t redirect users from www.example.com to example.com.

Conclusion

Redirects are an essential part of maintaining a healthy website. They help ensure that broken links don’t lead to 404 pages and that your content is accessible. There are two redirect types: The 302 Redirect is a temporary redirect. Once the user clicks on the link from the new page, the browser will show the old URL in the address bar.

However, if the user finds the page through a search engine, the page title and description remain the same. A 302 Redirect can be used to redirect the user to another page temporarily, but it is not ideal for redirecting to a permanent URL. A 301 Redirect is a permanent redirect.

The post Simple Redirects with .htaccess appeared first on TecAdmin.

]]>
https://tecadmin.net/simple-redirects-with-htaccess/feed/ 0
How To Disable HTTP Methods in Apache https://tecadmin.net/how-to-disable-http-methods-in-apache/ https://tecadmin.net/how-to-disable-http-methods-in-apache/#comments Thu, 30 Dec 2021 06:09:30 +0000 https://tecadmin.net/?p=28430 The HTTP methods are used to perform create, read, update, and delete (or CRUD) operations. The most common methods are POST, GET, PUT, PATCH, and DELETE. Its good practice to disable methods, which are unused and insecure like PUT, PATCH, and DELETE. This tutorial explains, how to disable HTTP methods for an apache web server. [...]

The post How To Disable HTTP Methods in Apache appeared first on TecAdmin.

]]>
The HTTP methods are used to perform create, read, update, and delete (or CRUD) operations. The most common methods are POST, GET, PUT, PATCH, and DELETE. Its good practice to disable methods, which are unused and insecure like PUT, PATCH, and DELETE.

This tutorial explains, how to disable HTTP methods for an apache web server.

Disable HTTP Methods in Apache

Create a “.htaccess” file under the document root directory and add the following code. Make sure that the Apache rewrite module and .htaccess are enabled.

RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^(HEAD|PUT|DELETE|PATCH|TRACK|OPTIONS) 
RewriteRule .* - [F]

The above configuration will disable HEAD, PUT, DELETE, PATCH, TRACK, and OPTIONS methods.

Next, restart the Apache webserver to apply changes.

sudo systemctl restart apache2 

Verify Setup

You can verify changes using the curl command line utility. Let’s send a request from your system to verify that the server accepts specific header requests. For example, the below command will send an “OPTIONS” request to the server.

curl -i -X OPTIONS https://tecadmin.net 
Output
HTTP/1.1 403 Forbidden Date: Thu, 30 Dec 2021 05:50:03 GMT Server: Apache/2.4.41 (Ubuntu) Content-Length: 281 Content-Type: text/html; charset=iso-8859-1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>403 Forbidden</title> </head><body> <h1>Forbidden</h1> <p>You don't have permission to access this resource.</p> <hr> <address>Apache Server at tecadmin.net Port 443</address> </body></html>

You will see a forbidden message in the result. This means that the Apache server rejected the OPTIONS request.

Conclusion

Hopefully, this article will help you disable the HTTP methods for your Apache webserver.

The post How To Disable HTTP Methods in Apache appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-disable-http-methods-in-apache/feed/ 1
How To Enable Brotli Compression in Apache https://tecadmin.net/how-to-enable-brotli-compression-in-apache/ https://tecadmin.net/how-to-enable-brotli-compression-in-apache/#comments Thu, 01 Jul 2021 09:44:53 +0000 https://tecadmin.net/?p=26417 Just like Gzip, Brotli is also a generic-purpose compression algorithm developed by Google. It compresses data using a combination of modern technologies and algorithms. It is similar in speed to deflate but provides higher compression. Brotli compression is supported by all the major browsers like Chrome, Firefox, Safari, Edge. The Brotli compression is opted by [...]

The post How To Enable Brotli Compression in Apache appeared first on TecAdmin.

]]>
Just like Gzip, Brotli is also a generic-purpose compression algorithm developed by Google. It compresses data using a combination of modern technologies and algorithms. It is similar in speed to deflate but provides higher compression. Brotli compression is supported by all the major browsers like Chrome, Firefox, Safari, Edge.

The Brotli compression is opted by the top tech fortunes like Cloudflare etc. This is the reason, we recommend switching to brotli from the old deflate data compression algorithm.

This tutorial helps you to enable brotli compression in the Apache webserver.

Prerequisites

Shell access to your server with sudo privileged account.

We assume that you already have a running Apache server. Also created a virtual host for the web application.

Step 1 – Installing Brotli

First, install the brotli package on your system. For the Ubuntu and Debian systems, it’s available in the default repositories.

Open a terminal and type:

sudo apt install brotli -y 

This will install the required package containing the algorithm files on your system.

Step 2 – Configure Brotli with Apache

The Apache server contains the Brotli module default. You can enable the brotli module in Apache with the following command.

sudo a2enmod brotli 

Next, you have to configure the Apache virtual host to enable compression with brotli. You need to add the below code in the virtual host configuration file.

<IfModule mod_brotli.c>
    AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript
</IfModule>

After enabling the brotli compression the Virtual host configuration file looks like below:

<VirtualHost *:80>
      ServerAdmin webmaster@localhost
      ServerName example.com
      DocumentRoot /var/www/

      <IfModule mod_brotli.c>
            AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript
      </IfModule>
	
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save the configuration file and close it. Then reload the Apache service to apply changes.

sudo systemctl restart apache2 

That’s it. You have successfully enabled brotli compression in the Apache server.

Step 3 – Test Compression

Access your web application in a browser and check headers value in the browser console. You need to search for the Content-Encoding value. It must contain br as value, which denotes that the web page is compressed with brotli compression.

The command line heroes can also use curl command to access the header values as below:

curl -I -H 'Accept-Encoding: br' http://example.com 

You will see the result below.

HTTP/1.1 200 OK
Date: Thu, 01 Jul 2021 06:26:54 GMT
Server: Apache/2.4.41 (Ubuntu)
Upgrade: h2,h2c
Connection: Upgrade
Last-Modified: Fri, 05 Feb 2021 08:55:44 GMT
ETag: "33-5ba92fc4cecdf-br"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: br
Content-Length: 46
Content-Type: text/html

Check for the value of Content-Encoding option.

Conclusion

This tutorial helped you to configure Brotli compression in the Apache webserver.

The post How To Enable Brotli Compression in Apache appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-enable-brotli-compression-in-apache/feed/ 2
Create A Custom 404 Error Page on Apache https://tecadmin.net/create-a-custom-404-error-page-on-apache/ https://tecadmin.net/create-a-custom-404-error-page-on-apache/#comments Fri, 12 Jun 2020 18:32:13 +0000 https://tecadmin.net/?p=22291 Apache Error code 404 means “file not found”. This is the error code when a user requested a web page or file which does not exist on the server. As per the SEO perspective, it’s not good to return a 404 error code to the user even requested file doesn’t exist. Now, You can either [...]

The post Create A Custom 404 Error Page on Apache appeared first on TecAdmin.

]]>
Apache Error code 404 means “file not found”. This is the error code when a user requested a web page or file which does not exist on the server. As per the SEO perspective, it’s not good to return a 404 error code to the user even requested file doesn’t exist.

Now, You can either configure a custom 404 error page on your server or redirect the website to the home page. In this tutorial, we will help you to create a custom 404 error page for your Apache server.

Create A Custom 404 Error Page

First of all, create a 404 file on your server. You can create this file under the document root of your application. For example, I have created a file named custom_404.html under the document root with the following content.

<html>
<head>
<title>Error 404</title>
</head>
<body>
<div class="container">
<h1>404</h1>
<h2>Page Not Found</h2>
<p>The Page you are looking for doesn't exist or moved to other location. Go to <a href="">Home Page.</a></p>
</div>
</body>
</html>

Next, create and edit the .htaccess file under the document root of your application and add the following content to the end of the file.

#### Error Documents
ErrorDocument 404 /custom_404.html

Save the file and close it.

Now, access any file to your domain, which doesn’t exist on the server. You will see the custom error page instead of the default Apache error message on your web browser.

The post Create A Custom 404 Error Page on Apache appeared first on TecAdmin.

]]>
https://tecadmin.net/create-a-custom-404-error-page-on-apache/feed/ 1
Apache 404 Redirect to Homepage https://tecadmin.net/apache-404-redirect-to-homepage/ https://tecadmin.net/apache-404-redirect-to-homepage/#respond Fri, 12 Jun 2020 18:15:10 +0000 https://tecadmin.net/?p=22267 Apache Error code 404 means “file not found”. This is the error code when a user requested a web page or file which does not exists on the server. As per the SEO perspective, its not good to return 404 error code to user even requested file doesn’t exit. To solve this error, you can [...]

The post Apache 404 Redirect to Homepage appeared first on TecAdmin.

]]>
Apache Error code 404 means “file not found”. This is the error code when a user requested a web page or file which does not exists on the server. As per the SEO perspective, its not good to return 404 error code to user even requested file doesn’t exit.

To solve this error, you can create a custom error page or redirect user to website home page. This tutorial will help you to redirect user to home page if 404 error occurred on server.

Setup 404 Redirect to Homepage

Edit .htaccess file under the document root of your Website hosted via Apache server. Add below content to the end of file.

For example, you website document root is /var/www/domain.com. The the .htaccess file needs to edit at /var/www/domain.com/.htaccess. Make sure to change domain.com with your actual domain name.

#### Error Documents
ErrorDocument 404 https://domain.com/

Save file and close it.

Now, access any file to your domain, which doesn’t exist on server. Test if webpage is redirected to the home page instead of showing 404 error page.

The post Apache 404 Redirect to Homepage appeared first on TecAdmin.

]]>
https://tecadmin.net/apache-404-redirect-to-homepage/feed/ 0
How to Enable expire headers in Apache https://tecadmin.net/leverage-browser-caching-with-apache/ https://tecadmin.net/leverage-browser-caching-with-apache/#comments Fri, 22 May 2020 13:23:16 +0000 https://tecadmin.net/?p=21680 Browser caching is used to save files on end user browser cache and re use on recurring requests. It significantly improve the page load times. Apache web server provides module mod_expire. Which controls the setting of the HTTP header and for expires and max-age directive of the Cache-Control HTTP header in the server responses. Setup [...]

The post How to Enable expire headers in Apache appeared first on TecAdmin.

]]>
Browser caching is used to save files on end user browser cache and re use on recurring requests. It significantly improve the page load times. Apache web server provides module mod_expire. Which controls the setting of the HTTP header and for expires and max-age directive of the Cache-Control HTTP header in the server responses.

Setup Expire headers on Apache

Before using this, you must have mod expires module enabled on Apache server. On the Debian based systems (Ubuntu, Debian and Linuxmint) expires module is disabled by default. You can enable this by running the following command:

sudo a2enmod expires

After enabling modules, restart Apache server to load new settings.

Now, Add the below settings to Apache virtual host configuration file, or add this in .htaccess file under your application.

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css A31536000
    ExpiresByType text/x-component A31536000
    ExpiresByType application/x-javascript A31536000
    ExpiresByType application/javascript A31536000
    ExpiresByType text/javascript A31536000
    ExpiresByType text/x-js A31536000
    ExpiresByType text/html A3600
    ExpiresByType text/richtext A3600
    ExpiresByType text/plain A3600
    ExpiresByType text/xsd A3600
    ExpiresByType text/xsl A3600
    ExpiresByType text/xml A3600
    ExpiresByType video/asf A31536000
    ExpiresByType video/avi A31536000
    ExpiresByType image/bmp A31536000
    ExpiresByType application/java A31536000
    ExpiresByType video/divx A31536000
    ExpiresByType application/msword A31536000
    ExpiresByType image/gif A31536000
    ExpiresByType application/x-gzip A31536000
    ExpiresByType image/x-icon A31536000
    ExpiresByType image/jpeg A31536000
    ExpiresByType image/webp A31536000
    ExpiresByType application/json A31536000
    ExpiresByType audio/midi A31536000
    ExpiresByType video/quicktime A31536000
    ExpiresByType audio/mpeg A31536000
    ExpiresByType video/mp4 A31536000
    ExpiresByType video/mpeg A31536000
    ExpiresByType video/webm A31536000
    ExpiresByType application/x-font-otf A31536000
    ExpiresByType audio/ogg A31536000
    ExpiresByType application/pdf A31536000
    ExpiresByType image/png A31536000
    ExpiresByType audio/x-realaudio A31536000
    ExpiresByType image/svg+xml A31536000
    ExpiresByType application/x-shockwave-flash A31536000
    ExpiresByType application/x-tar A31536000
    ExpiresByType image/tiff A31536000
    ExpiresByType application/x-font-ttf A31536000
    ExpiresByType audio/wav A31536000
    ExpiresByType audio/wma A31536000
    ExpiresByType application/font-woff A31536000
    ExpiresByType application/font-woff2 A31536000
    ExpiresByType application/zip A31536000
</IfModule>

Making changes in .htaccess will will be effective immediately, But if you add above settings in Apache virtual host configuration file, you need to reload Apache server.

Conclusion

In this tutorial, you have learned to enable Apache modules and configure Apache HTTP headers to save files on users browser.

The post How to Enable expire headers in Apache appeared first on TecAdmin.

]]>
https://tecadmin.net/leverage-browser-caching-with-apache/feed/ 1
How to Install Apache with PHP-FPM on Ubuntu 20.04 https://tecadmin.net/setup-apache-php-fpm-ubuntu-20-04/ https://tecadmin.net/setup-apache-php-fpm-ubuntu-20-04/#comments Sat, 09 May 2020 06:07:25 +0000 https://tecadmin.net/?p=21436 PHP FPM (FastCGI Process Manager) is an alternative implementation of PHP FastCGI. It provides some additional features like Adaptive process spawning which is useful for sites. This tutorial will help you to install Apache with PHP-FPM/FastCGI on Ubuntu 20.04 system. In this tutorial, we are using PHP 7.4 and configure with Apache using PHP-FPM and [...]

The post How to Install Apache with PHP-FPM on Ubuntu 20.04 appeared first on TecAdmin.

]]>
PHP FPM (FastCGI Process Manager) is an alternative implementation of PHP FastCGI. It provides some additional features like Adaptive process spawning which is useful for sites. This tutorial will help you to install Apache with PHP-FPM/FastCGI on Ubuntu 20.04 system. In this tutorial, we are using PHP 7.4 and configure with Apache using PHP-FPM and FastCGI.

You can also visit the previous tutorial to configure Apache with multiple PHP versions using PHP-FPM/FastCGI on Ubuntu systems.

Step 1 – Installing Apache

Apache web server debian packages are available under the default repositories. Login to your Ubuntu system with sudo privileges account. Open a terminal and execute the following commands:

sudo apt update 
sudo apt install apache2 libapache2-mod-fcgid

The above commands will install Apache and FastCGI module to your server.

Step 2 – Install PHP with FPM

Next, install PHP and PHP-FPM on your Ubuntu system. For this tutorial, we choose PHP 7.4 to install using

For the PHP installation we recommend to use ppa:ondrej/php PPA. Execute below couple of commands to add the PPA to your system.

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Then install PHP 7.4 (or required version) the latest version available on the day of writing this tutorial. Simply execute follows commands for the installation of PHP and PHP-FPM packages.

sudo apt update
sudo apt install php7.4 php7.4-fpm
Note:- When you are using PHP-FPM. All the PHP modules configurations are residing under /etc/php/7.4/fpm directory. You can read more about enable/disable PHP modules.

After installing the packages php7.4-fpm service will automatically be started. To make sure, type:

sudo systemctl status php7.4-fpm

● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2020-05-09 04:41:44 UTC; 19s ago
       Docs: man:php-fpm7.4(8)
    Process: 375077 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74 (code=exited>
   Main PID: 375073 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 2283)
     Memory: 9.3M
     CGroup: /system.slice/php7.4-fpm.service
             ├─375073 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─375075 php-fpm: pool www
             └─375076 php-fpm: pool www

May 09 04:41:43 tecadmin systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
May 09 04:41:44 tecadmin systemd[1]: Started The PHP 7.4 FastCGI Process Manager.

Step 3 – Apache Configuration

Now, You need to enable some of the Apache modules required for the FastCGI confiugration. You can enable the requird module by running command:

sudo a2enmod actions fcgid alias proxy_fcgi

Then configure Apache Virtual Host to run with FPM/FastCGI. For this tutorial, we use default VirtualHost. Edit VirtualHost host configuration file in a text editor. You can also create a new configuration as per your choice.

sudo vim /etc/apache2/sites-available/000-default.conf

Update the configuration as followings.

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    <FilesMatch \.php$>
        # 2.4.10+ can proxy to unix socket
        SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost"
    </FilesMatch>

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

Save your changes to the configuration file and restart Apache to reload the changes.

sudo systemctl restart apache2

Step 4 – Verify Setup

Apache with PHP-FPM configuration has been done. Now all the PHP files will be run with the FPM.

To verify these settigns, create a PHP script with phpinfo() function and place it to your server document root. Use below command to create file:

echo "<?php phpinfo(); ?>" > /var/www/html/info.php

Then access the info.php using server IP address (for default VirtualHost) or your configured domain in Apache VirtualHost.

Setup Apache with php fpm ubuntu

Conclusion

This tutorial helps you to install Apache with PHP-FPM on Ubuntu system.

The post How to Install Apache with PHP-FPM on Ubuntu 20.04 appeared first on TecAdmin.

]]>
https://tecadmin.net/setup-apache-php-fpm-ubuntu-20-04/feed/ 5
How to Install and Secure Apache on Ubuntu 20.04 https://tecadmin.net/install-apache-ubuntu-20-04/ https://tecadmin.net/install-apache-ubuntu-20-04/#comments Wed, 29 Apr 2020 17:04:42 +0000 https://tecadmin.net/?p=20042 This tutorial will help you to install Apache web server on Ubuntu 20.04 LTS (Focal Fossa) operating system. You will also learn how to secure your domain using Let’s encrypt SSL certificate. This tutorial will also work on Ubuntu 18.04, Ubuntu 16.04 and Ubuntu 19.10 Linux systems. Before We Start Before begin your work: Running [...]

The post How to Install and Secure Apache on Ubuntu 20.04 appeared first on TecAdmin.

]]>
This tutorial will help you to install Apache web server on Ubuntu 20.04 LTS (Focal Fossa) operating system. You will also learn how to secure your domain using Let’s encrypt SSL certificate. This tutorial will also work on Ubuntu 18.04, Ubuntu 16.04 and Ubuntu 19.10 Linux systems.

Before We Start

Before begin your work:

  • Running Ubuntu 20.04 system with sudo privileges shell access.
  • Complete initial server setup instructions
  • A domain name registered and pointed to your server’s public IP address. For this tutorial, we use webhost.tecadmin.net, which is pointed to our server.

Step 1 — Installing Apache

Apache packages are available under the default software repositories on Ubunts. You can easily install it using the conventional package management tool.

First of all, update the local package index to reflect the latest upstream changes. Then install Apache2 web server.

sudo apt update
sudo apt install apache2

After the confirmation, apt will install Apache and other required dependencies on your system.

Step 2 — Test Your Web Server

Once the installation finished, Apache service will automatically start on your Ubuntu system. You can find the Apache service status by running the following command:

sudo systemctl status apache2

Sample output:

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2020-04-26 05:28:08 UTC; 10min ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 15464 (apache2)
      Tasks: 55 (limit: 2283)
     Memory: 6.9M
     CGroup: /system.slice/apache2.service
             ├─15464 /usr/sbin/apache2 -k start
             ├─18646 /usr/sbin/apache2 -k start
             └─18647 /usr/sbin/apache2 -k start

Apr 26 05:28:08 tecadmin systemd[1]: Starting The Apache HTTP Server...
Apr 26 05:28:08 tecadmin systemd[1]: Started The Apache HTTP Server.

The result status like “Active: active (running)” means Apache service have started successfully. However, the best way to test web server, request a page from Apache in web browser.

Apache default page ubuntu 20.04

You will see the default Apache landing page. It means the Apache web server is running properly on your system.

Step 3 — Create A Virtual Host

With the help of virtual hosts, you can host more than one domain from a single server. A virtual host encapsulate the configuration of a domain to it. We will set up a virtual host with a sub-domain called webhost.tecamin.net, but you should replace this with your own domain name.

Let’s start with a directory for our domain as follows:

sudo mkdir /var/www/webhost
sudo chmod -R 755 /var/www/webhost
sudo chown -R www-data:www-data /var/www/webhost

Next, create a sample index.html page to host on this subdomain. Edit this file in your favorite text editor like vim or nano:

nano /var/www/webroot/index.html

Add the following sample HTML content:

<html>
  <head>
      <title>Welcome to TecAdmin.net!</title>
  </head>
  <body>
      <h1>Success!!!</h1>
  </body>
</html>

Save file and close it.

With the newly installed Apache server, you will see a default virtual host configuration file located at etc/apache2/sites-available/000-default.conf. But this is an good habbit to create seperate configuration file for each virtual host. So create a new virtual host file as /etc/apache2/sites-available/webhost.tecadmin.net.conf:

sudo nano /etc/apache2/sites-available/webhost.tecadmin.net.conf

Add the following configuration to virtual host file. Make sure to change the proper ServerAdmin email address, Servername to your domain name. You can also include ServerAlias to add more domain or subdomains. Then set correct DocumentRoot as created above.

<VirtualHost *:80>
    ServerAdmin webmaster@tecadmin.net
    ServerName webhost.tecadmin.net
    #ServerAlias www.webhost.tecadmin.net   ##Uncomment it to use
    DocumentRoot /var/www/webhost
    <Directory /var/www/webhost>
       Allowoverride all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save file and close it.

By default Apache reads virtual host configuration files under /etc/apache2/sites-available directory. Use a2ensite tool to enable this virtual host:

sudo a2ensite webhost.tecadmin.net.conf

The above command will create a symbolic link webhost.tecadmin.net.conf file to sites-available directory.

Once done, run the below command to verify the configuration files:

sudo apache2ctl configtest

You should see the following output:

Syntax OK

Next, Restart Apache service to apply your changes:

sudo systemctl restart apache2

Apache is ready to serve your content on your configured domain name. You can test this by navigating to http://webhost.tecadmin.net,

Step 4 — Configure Let’s Encrypt SSL

We use Let’s encrypt SSL certificate to secure website on Apache web server. Certbot is the command line utility to work with Let’s encrypt certificates. Run the following command to install certbot binary:

sudo apt install python3-certbot-apache

This will install all the required packages for the certbot.

Once the installation process finished. Run the below command to request let’s encrypt certificate authority to issue certificate for our domain webhost.tecadmin.net. You can add multiple domains or subdomains using separate “-d” parameters.

certbot -d webhost.tecadmin.net

Wizard will ask your email address to send your updates. Then accept Terms of Service to continue. Below is the complete logs of command:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): webmaster@tecadmin.net

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for webhost.tecadmin.net
Enabled Apache rewrite module
Waiting for verification...
Cleaning up challenges
Created an SSL vhost at /etc/apache2/sites-available/webhost.tecadmin.net-le-ssl.conf
Enabled Apache socache_shmcb module
Enabled Apache ssl module
Deploying Certificate to VirtualHost /etc/apache2/sites-available/webhost.tecadmin.net-le-ssl.conf
Enabling available site: /etc/apache2/sites-available/webhost.tecadmin.net-le-ssl.conf

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Enabled Apache rewrite module
Redirecting vhost in /etc/apache2/sites-enabled/webhost.tecadmin.net.conf to ssl vhost in /etc/ap                         ache2/sites-available/webhost.tecadmin.net-le-ssl.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://webhost.tecadmin.net

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=webhost.tecadmin.net
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/webhost.tecadmin.net/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/webhost.tecadmin.net/privkey.pem
   Your cert will expire on 2020-07-25. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

You have successfully configured you domain with SSL certificate. Now, you can access your domain over https protocol as shown in the screenshot below:

Apache with Lets encrypt SSL on ubuntu 20.04

Step 5 — Adjust Firewall Rules

You can use the service name like “http” or “https” to allow in FirewallD. To open HTTP and HTTPS port in FirewallD run the below commands:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

Once you add the rules, reload changes using the following command.

sudo firewall-cmd --reload

Step 6 — Manage Apache Service

Now, you have your Apache web server up and running, let’s go with the Apache service management commands.

To stop Apache web server, type:

sudo systemctl stop apache2

To start Apache web server (if stopped), type:

sudo systemctl start apache2

To restart (stop and then start) Apache service, type:

sudo systemctl restart apache2

Instead of stop then start a running server, use reload option to apply configuration file changes without dropping current connections. But this will not read any new configuration file:

sudo systemctl reload apache2

To disable Apache service to auto start on system boot, type:

sudo systemctl disable apache2

To enable Apache service to auto start on system boot, type:

sudo systemctl enable apache2

Conclusion

The Apache web server is running on your Ubuntu 20.04 LTS operating system.

The post How to Install and Secure Apache on Ubuntu 20.04 appeared first on TecAdmin.

]]>
https://tecadmin.net/install-apache-ubuntu-20-04/feed/ 2
How to Configure X-Frame-Options in Apache https://tecadmin.net/configure-x-frame-options-apache/ https://tecadmin.net/configure-x-frame-options-apache/#comments Mon, 06 Apr 2020 16:02:32 +0000 https://tecadmin.net/?p=21031 The X-Frame-Options in used as HTTP response header. This prevents your site content embedded into other sites. Based on this value a browser allowed other sites to open web page in iframe. It also secure your Apache web server from clickjacking attack. There are three options available to set with X-Frame-Options: ‘SAMEORIGIN’ – With this [...]

The post How to Configure X-Frame-Options in Apache appeared first on TecAdmin.

]]>
The X-Frame-Options in used as HTTP response header. This prevents your site content embedded into other sites. Based on this value a browser allowed other sites to open web page in iframe. It also secure your Apache web server from clickjacking attack.

There are three options available to set with X-Frame-Options:

  • ‘SAMEORIGIN’ – With this setting, you can embed pages on same origin. For example, add iframe of a page to site itself.
  • ‘ALLOW-FROM uri – Use this setting to allow specific origin (website/domain) to embed pages of your site in iframe.
  • ‘DENY – This will not allow any website to embed your site pages in an iframe.

Setup X-Frame-Options with Apache Configuration

Edit Apache configuration file based on your operating system. The configuration file can be found:

Debian based systems: /etc/apache2/conf-enabled/security.conf
Redhat based systems: /etc/httpd/conf/httpd.conf

Now add one of the following entry to file:

  • Allow for Same Origin (Default Action)

    Header set X-Frame-Options: "SAMEORIGIN"
    
  • Allow from specific origin

    Header set X-Frame-Options: "ALLOW-FROM http://example.com/" 
    Header set X-Frame-Options: "ALLOW-FROM http://www.example.com/" 
    Header set X-Frame-Options: "ALLOW-FROM https://example.com/" 
    Header set X-Frame-Options: "ALLOW-FROM https://www.example.com/" 
    
  • Deny to everyone

    Header set X-Frame-Options: "DENY"
    

Save the configuration file and restart Apache service to apply changes.

Setup X-Frame-Options with .htaccess

The websites running over shared hosting environment, You may not have privileges to modify Apache configuration. In this case, you can create .htaccess file on document root and append the same settings as above:

Header append X-Frame-Options: "SAMEORIGIN"

The post How to Configure X-Frame-Options in Apache appeared first on TecAdmin.

]]>
https://tecadmin.net/configure-x-frame-options-apache/feed/ 3
How to Install Apache mod_wsgi Module on CentOS 8 https://tecadmin.net/install-apache-mod-wsgi-centos-8/ https://tecadmin.net/install-apache-mod-wsgi-centos-8/#comments Tue, 25 Feb 2020 17:25:29 +0000 https://tecadmin.net/?p=20277 The mod_wsgi Apache module is used for serving Python scripts over HTTP via the Apache web server. This tutorial helps you to how to install the Apache Python module (mod_wsgi) on CentOS 8 Linux. We will also create a sample page in Python and deploy it with the Apache web server. You may like: How [...]

The post How to Install Apache mod_wsgi Module on CentOS 8 appeared first on TecAdmin.

]]>
The mod_wsgi Apache module is used for serving Python scripts over HTTP via the Apache web server. This tutorial helps you to how to install the Apache Python module (mod_wsgi) on CentOS 8 Linux. We will also create a sample page in Python and deploy it with the Apache web server.

You may like:

Step 1 – Prerequisites

Login to the CentOS 8 server console via SSH. Then must have python installed on our system. Use the following commands to install python as its dependencies on your system.

sudo dnf install python3 python3-pip

Step 2 – Install mod_wsgi Module

Before starting, you will need to install some prerequisite Apache components in order to work with mod_wsgi. You can install all the required components by simply running the following command:

sudo dnf install mod_wsgi httpd

Restart Apache service to get mod_wsgi to work.

sudo systemctl restart httpd.service

Step 3 – Configure Apache for WSGI

Next, create a python script to serve via the mod_wsgi Apache module. For testing, I have created a test_wsgi.py file under the default document root.

sudo vi /var/www/html/test_wsgi.py

And added the following content:

def application(environ, start_response):
    status = '200 OK'
    output = b'Hooray, mod_wsgi is working'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

After that, configure the Apache server to serve this file over the web. Let’s create a configuration file to serve the test_wsgi.py script over a sub URL.

sudo vi /etc/httpd/conf.d/python-wsgi.conf

Add the following content:

WSGIScriptAlias /test_wsgi /var/www/html/test_wsgi.py

<Directory /var/www/html>
Order allow,deny
Allow from all
</Directory>

After completing the above steps enable mod-wsgi configuration and restart Apache service.

sudo systemctl restart httpd.service

Step 4 – Testing

The setup is ready now. You can test the script by accessing the following URL in a web browser. Change your-server-ip with the actual server IP or hostname.

 http://your-server-ip/test_wsgi

You will see the results as below:

Install Apache mod_wsgi Module on CentOS 8

The post How to Install Apache mod_wsgi Module on CentOS 8 appeared first on TecAdmin.

]]>
https://tecadmin.net/install-apache-mod-wsgi-centos-8/feed/ 5