{"id":1471,"date":"2020-06-24T16:59:06","date_gmt":"2020-06-24T08:59:06","guid":{"rendered":"http:\/\/blog.davcloud.top\/?p=1471"},"modified":"2020-07-08T12:16:15","modified_gmt":"2020-07-08T04:16:15","slug":"install-and-configure-apache2-and-enable-https-on-ubuntu","status":"publish","type":"post","link":"https:\/\/blog.davcloud.top\/?p=1471","title":{"rendered":"Install and configure apache2 and enable https on Ubuntu"},"content":{"rendered":"\n<h2><strong>Abstract<\/strong><\/h2>\n\n\n\n<p>Apache2 is an open sourced http server project, which is widely used in plenty of websites and service. This article will record how to install and configure apache2 and enable https on Ubuntu, and commonly used commands will be listed as well.<\/p>\n\n\n\n<h2><strong>Part 1. Install and test apache2<\/strong><\/h2>\n\n\n\n<h3><strong>Step 1. Install apache2<\/strong><\/h3>\n\n\n\n<p>Install apache2 with command below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install apache2<\/code><\/pre>\n\n\n\n<h3><strong>Step 2. Enable 80 port of the firewall<\/strong><\/h3>\n\n\n\n<p>Use ufw to enable 80 port, which will be used to test default website on 80 port:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ufw allow 80<\/code><\/pre>\n\n\n\n<p class=\"has-text-color has-luminous-vivid-amber-color\">Notice: For using ufw, please see my another article:<\/p>\n\n\n\n<figure class=\"wp-block-embed-wordpress aligncenter wp-block-embed is-type-wp-embed is-provider-davcloud-blog\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"6nkrARBUNV\"><a href=\"https:\/\/blog.davcloud.top\/?p=224\">Use ufw to config the firewall on Debian<\/a><\/blockquote><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Use ufw to config the firewall on Debian&#8221; &#8212; davcloud_Blog\" src=\"https:\/\/blog.davcloud.top\/?p=224&#038;embed=true#?secret=vPkBplUlMr#?secret=6nkrARBUNV\" data-secret=\"6nkrARBUNV\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h3><strong>Step 3. Test default website<\/strong><\/h3>\n\n\n\n<p>Access http:\/\/127.0.0.1\/ in browser to test default website. If the apache2 installed successfully, the default website for apache2 will be shown like this:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1908\" height=\"974\" src=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/06\/apache2-default-website.png\" alt=\"\" class=\"wp-image-1474\"\/><\/figure><\/div>\n\n\n\n<h2><strong>Part 2. Configure apache2 on ubuntu<\/strong><\/h2>\n\n\n\n<p>Before configure a website with an example, let us understand the file structure of the apache2 installed path \u201c\/etc\/apache2\/\u201d first:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/etc\/apache2\/\n\u251c\u2500\u2500 apache2.conf\t# Main settings for apache2, rarely modified.\n\u251c\u2500\u2500 conf-available\n\u251c\u2500\u2500 conf-enabled\n\u251c\u2500\u2500 envvars\t# Environment variables for apache2, rarely modified.\n\u251c\u2500\u2500 magic\n\u251c\u2500\u2500 mods-available  # Installed apache2 modules, not running modules.\n\u251c\u2500\u2500 mods-enabled          # Enabled apache2 modules, running modules.\n\u251c\u2500\u2500 ports.conf\t          # All the listening ports are in this file.\n\u251c\u2500\u2500 sites-available\t      # All virtual host configuration files.\n\u2514\u2500\u2500 sites-enabled  # Enabled and running virtual host configurations.<\/code><\/pre>\n\n\n\n<p>Now we will configure an website with virtual host as an example. Assume a service has been deployed on 9264 port, and we want to access the service with the website <a href=\"http:\/\/service.example.com\/\">http:\/\/service.example.com\/<\/a>, which the domain service.example.com has been resolved to the ubuntu server through the A record. We can configure a new .conf file with virtual host and enable it to achieve this.<\/p>\n\n\n\n<h3><strong>Step 1. Create a new configuration file<\/strong><\/h3>\n\n\n\n<p>Create a new configuration file named \u201cservice.conf\u201d in \u201c\/etc\/apache2\/sites-available\/\u201d:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>~$ cd  \/etc\/apache2\/sites-available\/\n\/etc\/apache2\/sites-available$ sudo vim service.conf<\/code><\/pre>\n\n\n\n<h3><strong>Step 2. Edit the configuration file<\/strong><\/h3>\n\n\n\n<p>Edit the file with codes below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;VirtualHost _default_:80>     # Create a virtual host listen 80 port.\n    Servername service.example.com                      # Domain name.\n    ProxyPass \/ http:\/\/localhost:9264\/\t# Forward access to port 9264.\n    ProxyPassReverse \/ http:\/\/localhost:9264\/\t\n                                # Forward reverse access to port 9264.\n    ProxyPreserveHost On\n&lt;\/VirtualHost><\/code><\/pre>\n\n\n\n<p> Save the file and exit.<\/p>\n\n\n\n<h3><strong>Step 3. Enable the site and reload apache2<\/strong><\/h3>\n\n\n\n<p>Enable the site with following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2ensite service.conf<\/code><\/pre>\n\n\n\n<p> Follow the guide to reload apache2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl reload apache2<\/code><\/pre>\n\n\n\n<p class=\"has-text-color has-luminous-vivid-amber-color\">Notice: The apache2 is required to be reload when any file in the apache2 is modified, sometimes even required to restart the apache2 if the restart notice is shown in terminal.<\/p>\n\n\n\n<p>After this, the service can be accessed with the website <a href=\"http:\/\/service.example.com\/\">http:\/\/service.example.com\/<\/a>.<\/p>\n\n\n\n<h3><strong>Step 4. Enable ssl module and restart apache2<\/strong><\/h3>\n\n\n\n<p>To improve the safety of access the service, we can enable the https (default on 443 port). Before that, enable the ssl module with following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2enmod ssl<\/code><\/pre>\n\n\n\n<p>If success, the notice will say restart the apache2 service:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart apache2<\/code><\/pre>\n\n\n\n<p>Check the \u201c\/etc\/apache2\/ports.conf\u201d, the 443 port is listened as default as usual. If not, add \u201cListen 443 https\u201d in the file and reload the apache2.<\/p>\n\n\n\n<h3><strong>Step 5. Enable https for the virtual host<\/strong><\/h3>\n\n\n\n<p>Before modify the virtual host configuration file, a SSL certificate is required for the domain. To apply a free SSL certificate, please see my another article:<\/p>\n\n\n\n<figure class=\"wp-block-embed-wordpress aligncenter wp-block-embed is-type-wp-embed is-provider-davcloud-blog\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"aWK8z5iLys\"><a href=\"https:\/\/blog.davcloud.top\/?p=670\">Apply the wildcard SSL certificate of Let&#8217;s Encrypt<\/a><\/blockquote><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Apply the wildcard SSL certificate of Let&#8217;s Encrypt&#8221; &#8212; davcloud_Blog\" src=\"https:\/\/blog.davcloud.top\/?p=670&#038;embed=true#?secret=gLr9mTXttz#?secret=aWK8z5iLys\" data-secret=\"aWK8z5iLys\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Modify the configuration file and add the virtual host for listening 443 port and enable SSL verification:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;VirtualHost _default_:443>   # Create a virtual host listen 443 port.\n    Servername service.example.com\n    SSLEngine on                                         # Enable SSL.\n    SSLCertificateFile \/path\/to\/certificate\/file\n    SSLCertificateKeyFile \/path\/to\/certificate\/key\/file\n\n    ProxyPass \/ http:\/\/localhost:9264\/\t# Forward access to port 9264.\n    ProxyPassReverse \/ http:\/\/localhost:9264\/\t\n                                # Forward reverse access to port 9264.\n    ProxyPreserveHost On\n&lt;\/VirtualHost><\/code><\/pre>\n\n\n\n<p>Save and exit the file, and reload the apache2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl reload apache2<\/code><\/pre>\n\n\n\n<h3><strong>Step 6. Enable the 443 port on firewall with ufw<\/strong><\/h3>\n\n\n\n<p>Enable 443 port in the firewall with ufw:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ufw allow 443<\/code><\/pre>\n\n\n\n<p>After this we can access the service with the website <a href=\"https:\/\/service.example.com\/\">https:\/\/service.example.com\/<\/a>.<\/p>\n\n\n\n<h2><strong>Part 3. Commonly used commands<\/strong><\/h2>\n\n\n\n<h3>Switch of the service<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl start apache2\t\t# Start apache2 service\nsudo systemctl stop apache2\t\t# Stop apache2 service\nsudo systemctl reload apache2\t\t# Reload apache2 service\nsudo systemctl restart apache2\t\t# Restart apache2 service<\/code><\/pre>\n\n\n\n<h3>Enable\/Disable virtual host configuration<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2ensite &lt;.conf file>\nsudo a2dissite &lt;.conf file><\/code><\/pre>\n\n\n\n<h3>Enable\/Disable apache2 module<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2enmod &lt;module name>\nsudo a2dismod &lt;module name><\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Abstract Apache2 is an open sourced http server project, which is widely used in plenty of websites and service. This article will record how to install and configure apache2 and enable https on Ubuntu, and commonly used commands will be listed as well. Part 1. Install and test apache2 Step 1. Install apache2 Install apache2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13,16],"tags":[617,108,35],"_links":{"self":[{"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/posts\/1471"}],"collection":[{"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1471"}],"version-history":[{"count":17,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/posts\/1471\/revisions"}],"predecessor-version":[{"id":1525,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/posts\/1471\/revisions\/1525"}],"wp:attachment":[{"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}