I want to show you today how to do little operations and configurations when you have to setup a new application on an Apache HTTPD installation.
The following are classic daily operations that a system administrator does, which can be tricky if done once in a while.
Configure a virtual host
The configuration of a virtual host is used to tell the web server where to get the files to be exposed when called up from a particular domain.
Usually the configuration file that takes care of it can be found at the following path:
/opt/httpd/conf/extra/httpd-vhosts.conf
This file can be activated through another configuration file, by de-commenting the following line:
/opt/httpd/conf/httpd.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
The minimum configuration for this to work is as follows:
<VirtualHost *:80>
ServerName my-fe.it
DocumentRoot "/var/www/my-fe"
ServerAlias my-fe.it
</VirtualHost>
The following is a typical configuration for Single Page Application (SPA) applications (example: Angular, React). All traffic from any page belonging to that domain is sent back to a single root, which is the typical mechanism of this type of application:
<VirtualHost *:80>
ServerName my-fe.it
DocumentRoot "/var/www/my-fe"
ServerAlias my-fe.it
RewriteEngine On
Options +FollowSymLinks
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html [L]
</VirtualHost>
In the case of SPA applications, a BE of another technology is often associated, which allows the use of dynamic information, usually via DB. The following is a simple configuration of an Angular example application with a BE accessible to the context “my-be” (on Tomcat, in my example):
<VirtualHost *:80>
ServerName my-fe.it
DocumentRoot "/var/www/my-fe"
ServerAlias my-fe.it
ProxyRequests On
ProxyPass /my-be/api http://localhost:8080/my-be/api
ProxyPassReverse /my-be/api http://localhost:8080/my-be/api
RewriteEngine On
Options +FollowSymLinks
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteCond %{REQUEST_URI} !^/my-be.*
RewriteRule ^ /index.html [L]
</VirtualHost>
Restart an Apache HTTPD server
Once the configurations have been carried out, if the Virtual host has been modified as described above, it is necessary to restart the web server, launching the following command from the shell.
httpd -k restart
How to use “vi
” on linux/unix system
It often happens that a server cannot be accessed via ftp (downloading and uploading files) but only in ssh (from the command line). In those cases, the only way to modify a file is via the vi
interface or the like. Below is a list of the minimum operations that can be performed to modify lines in a file:
- Open a file location in ssh.
- Command:
vi file_to_modify.txt
to open/create a file with vi. - Navigate to the interested line/lines with arrows.
- Command:
i
from keyboard to start the insert mode. - When finished, command:
esc
, then:wq!
to save and close (write and quit).esc
and, if you want to undo your modification,:q!
(only quit without saving).