Apache2 on Ubuntu20.04 LTS

Introduction

A long time ago, I borrowed Sakura VPS to build Apache2 on CentOS and published a website, but Ubuntu has a different conf file name etc. and it took some time, so leave a note until it works. deep.

Execution environment

Ubuntu20.04 LTS (on GCP)

Memo content

    1. Apache installation and startup confirmation
    1. Check the Apache configuration file
    1. DocumentRoot change

1. 1. Apache installation and startup confirmation

After connecting with SSH, update and install the package from the following command.

python


#Update your package management tool to the latest
$ sudo apt update
$ sudo apt -y upgrade

#Apache installation
$ sudo apt install -y apache2

Check the startup status

python


$ systemctl status apache2

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-10-06 01:15:54 UTC; 21s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 17933 (apache2)
      Tasks: 55 (limit: 2374)
     Memory: 5.6M
     CGroup: /system.slice/apache2.service
             ├─17933 /usr/sbin/apache2 -k start
             ├─17935 /usr/sbin/apache2 -k start
             └─17936 /usr/sbin/apache2 -k start

⇒ Since it is [active (running)], it has been started.

To check if it is open to the outside, open an appropriate browser on your local PC, access http: // [external IP of VM instance] /, and the following screen is displayed. Qiita-no013_img01.jpg

2. 2. Check Apache configuration file

Check the location of the Apache2 conf file

$ find / -name 'apache2.conf'
/etc/apache2/apache2.conf

If you check the contents of apache2.conf, you will find the following description. (Excerpt because all lines are long)

apache2.conf


#Definition of file name for setting access authority
AccessFileName .htaccess

# .htaccess and.Deny access from web clients to htpasswd etc.
<FilesMatch "^\.ht">
        Require all denied
</FilesMatch>

#Read a file with port information set
Include ports.conf


#In each folder`load`Or`conf`It is set to read all files at startup

# Include module configuration:
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

# Include generic snippets of statements
IncludeOptional conf-enabled/*.conf

# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf

Play around with ports.conf

Try changing the port and connecting. Edit as follows in sudo nano /etc/apache2/ports.conf.

/etc/apache2/ports.conf


#Assigned port information
#Listen 80
Listen 20080

#Assigned port if there is an SSL related module
<IfModule ssl_module>
        Listen 443
</IfModule>

<IfModule mod_gnutls.c>
        Listen 443
</IfModule>

Restart Apache.

$ sudo systemctl restart apache2

Obviously, now I can't access it even if I enter the URL. If you add tcp: 20080 to the settings of the file on GCP, you can access the same screen withhttp: // [external IP of VM instance]: 20080 /. Qiita-no013_img02.jpg

Check in mods-enabled /

python


#All are symbolic links, the reality is/etc/apache2/mods-available/It is in

$ ls /etc/apache2/mods-enabled/
access_compat.load  authn_file.load  autoindex.load  env.load        mpm_event.load    setenvif.conf
alias.conf          authz_core.load  deflate.conf    filter.load     negotiation.conf  setenvif.load
alias.load          authz_host.load  deflate.load    mime.conf       negotiation.load  status.conf
auth_basic.load     authz_user.load  dir.conf        mime.load       reqtimeout.conf   status.load
authn_core.load     autoindex.conf   dir.load        mpm_event.conf  reqtimeout.load

$ readlink /etc/apache2/mods-enabled/dir.conf
../mods-available/dir.conf

Let's take a look at the contents of one.

/etc/apache2/mods-available/dir.conf


<IfModule mod_dir.c>
        DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

You can remove it from mods-enabled / with the following command, which will prevent the module from loading when Apache starts.

python


$sudo a2dismod [module name]
Module ~~~~~~ disabled.
To activate the new configuration, you need to run:
  systemctl restart apache2

This is the command to create a symbolic link again.

python


$sudo a2enmod [module name]
Enabling module ~~~~~~~~.
To activate the new configuration, you need to run:
  systemctl restart apache2

Check in conf-enabled /

python


#Similarly, all symbolic links. The reality is/etc/apache2/conf-available/It is in

$ ls /etc/apache2/conf-enabled/
charset.conf  localized-error-pages.conf  other-vhosts-access-log.conf  security.conf  serve-cgi-bin.conf

$ readlink /etc/apache2/conf-enabled/charset.conf
../conf-available/charset.conf

There aren't many configuration files, so let's take a look.

cat /etc/apache2/conf-enabled/charset.conf ⇒ Character code related settings. All defaults are commented out.

cat /etc/apache2/conf-enabled/localized-error-pages.conf ⇒ Error code related settings. All defaults are commented out.

cat /etc/apache2/conf-enabled/other-vhosts-access-log.conf ⇒ Access log related settings.

cat /etc/apache2/conf-enabled/security.conf ⇒ Apache security related settings. This article was organized in an easy-to-understand manner.   https://qiita.com/bezeklik/items/1c4145652661cf5b2271

cat /etc/apache2/conf-enabled/serve-cgi-bin.conf ⇒ CGI related settings

Check in sites-enabled /

python


#The default is only one site. Similarly, the reality is/etc/apache2/sites-available/It is in

$ ls /etc/apache2/sites-enabled
000-default.conf

$ readlink /etc/apache2/sites-enabled/000-default.conf
../sites-available/000-default.conf

The contents of the site config file are as follows.

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


#Only lines that are not commented out.
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Since DocumentRoot is "/ var / www / html", try editing index.html from the following command.

python


$ sudo nano /var/www/html/index.html

/var/www/html/index.html


#Change the title tag part

<!--    <title>Apache2 Ubuntu Default Page: It works</title> -->
    <title>Changed Title</title>  

Update the URL and reopen it to see that it has changed.

Supplement

There were some places where environment variables like $ {APACHE_LOG_DIR} were set, but these are set in / etc / apache2 / envvars.

3. 3. DocumentRoot change

Copy the default config file and try enable with the modified DocumentRoot.

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-availa
ble/test-site.conf

Setting contents of test-site.conf (change only DocumentRoot)

/etc/apache2/sites-available/test-site.conf


#Only lines that are not commented out.
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html-test/
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

Created because the above "html-test" directory should not be the default.

python


$ sudo mkdir /var/www/html-test/

python


#Delete the current site config once
$ sudo a2dissite 000-default

#Created site config
$ sudo a2ensite test-site

#Restart apache
$ sudo systemctl restart apache2

Since "html-test" does not have index.html, it was created because it was a big deal.

python


$ sudo nano /var/www/html-test/index.html

/var/www/html-test/index.html


<html>
 Hello Test-Site!
</html>

Reopen http: // [external IP of VM instance]: 20080 / and if it is the contents of the above html file, the setting is valid.

Supplement

As mentioned above, if you execute sudo a2ensite 000-default in the place where there is only" test-site.conf "in/ etc / apache2 / sites-enabled /, when you access from the outside," 000-default " 』Setting was prioritized. Also, when there is nothing in / etc / apache2 / sites-enabled /, it seems that it is set to "000-default (at the time of installation)", but where is the setting here? Is unknown.

Recommended Posts

Apache2 on Ubuntu20.04 LTS
Record video on ubuntu18.04 LTS
Installing OpenMX on Ubuntu 18.04.5 LTS
Install raspi-config on Ubuntu 20.04 (LTS)
Install WordPress 5.5 on Ubuntu 20.04 LTS
Wake on Lan settings on Ubuntu 20.04.1 LTS
tmux on Ubuntu
Until you run apache on ubuntu on docker
Screen recording on Ubuntu 20.04
Web Bluetooth on Ubuntu20.04
Try DisplayLink on Ubuntu 20.04
Reinstall Kubernetes on Ubuntu 19.10
Disk expansion on Ubuntu 20.04.1 LTS on GCP Compute Engine
Use Flutter on Ubuntu
Install pyqt5 on ubuntu
How to install multiple JDKs on Ubuntu 18.04 LTS
Setting JAVA_HOME on Ubuntu
Install Autoware on Ubuntu 18.04.5
Put JetBrains on Ubuntu
Use mkdir on ubuntu
Use cpplapack on ubuntu
ubuntu on wsl part 10
Install Homebrew on Ubuntu 20.04
Run tiscamera on Ubuntu 18.04
Ubuntu 18.04.05 About LTS setup
Build Zabbix on Ubuntu 20.04
Build Ubuntu 20.04 LTS desktop environment on Raspberry Pi 4 (+ Japanese)
Create SSL certificate on Ubuntu 18.04
Put Ubuntu 20.04.1 on Raspberry Pi 4
Build VNC Server on Ubuntu 20.04
Install Docker on Ubuntu Server 20.04
Ubuntu on Windows Community Preview
Japanese input on Ubuntu20.04 Desktop
Oracle Java 8 on Docker Ubuntu
Install zabbix agent (5.0) on Ubuntu 18.04
Install Arudino IDE on Ubuntu 20
Introduced knowledge (wiki) on Ubuntu
Ruby installation on WSL2 + Ubuntu 20.04
Install Ubuntu Desktop 20.10 on RaspberryPi4
Install Apache on CentOS on VirtualBox
Run chromium-mir-kiosk on Ubuntu Core
Run java applet on ubuntu
Run Eclipse CDT on Ubuntu
Set up ansible-playbook on Ubuntu 20.04
Install PlantUML on Intellij on Ubuntu
Install Ubuntu Server 20.04 on Btrfs
Note: Install PostgreSQL 9.5 on Ubuntu 18.04
Laravel environment construction (Ubuntu 18.04 LTS)
Install Ubuntu MATE 20.04 LTS on older MacBook Early 2008 (MB402 * / A, MB403 * / A, MB404 * / A)
How to Install Elixir and Phoenix Framework on Ubuntu 20.04 LTS
Disk expansion on Ubuntu 20.04.1 LTS on GCP Compute Engine
Build mate desktop environment on ec2 with terraform (Ubuntu 20.04LTS)
How to install java9 on elementaryOS Freya or Ubuntu 14.04 LTS
Set up a MineCraft Paper server on Ubuntu 20.04.1 LTS ② Update
Use virtual AP on Ubuntu 18.04 LTS to sub-monitor old iPad
Install Docker on AWS Ubunt 20.04 LTS
Install AWS IoT Greengrass on Ubuntu
Using Azure IOT Hub on Ubuntu 20.10.
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
Install JDK and JRE on Ubuntu 16.10
Install ngrok on ubuntu16.04 using Vagrant