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.
Ubuntu20.04 LTS (on GCP)
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.
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
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 /
.
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
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
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.
There were some places where environment variables like $ {APACHE_LOG_DIR}
were set, but these are set in / etc / apache2 / envvars
.
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.
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