Unit 09-Configuring Services
Unit 09-Configuring Services
2
Installing the Apache Web Server
❑ As indicated by the output, there are three profiles available for Apache:
Apache: This profile opens only port 80 (normal, unencrypted web traffic)
Apache Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443
(TLS/SSL encrypted traffic)
Apache Secure: This profile opens only port 443 (TLS/SSL encrypted traffic)
❑ It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve
configured. Since we haven’t configured SSL for our server yet in this guide, we will only need to
allow traffic on port 80:
sudo ufw allow 'Apache'
❑ You can verify the change by typing:
sudo ufw status
❑ The output will provide a list of allowed HTTP traffic:
❑ As indicated by the output, the profile has been activated to allow access to the Apache web
server.
3
Installing the Apache Web Server
3. Step 3 — Checking your Web Server: At the end of the installation process, Ubuntu 20.04 starts Apache.
The web server should already be up and running.
❑ Check with the systemd init system to make sure the service is running by typing:
sudo systemctl status apache2
❑ As confirmed by this output, the service has started successfully. However, the best way to test this
is to request a page from Apache. You can access the default Apache landing page to confirm that
the software is running properly through your IP address. If you do not know your server’s IP
address, you can get it a few different ways from the command line. Try typing this at your server’s
command prompt:
hostname –I
❑ When you have your server’s IP address, enter it into your browser’s address bar. You should see
the default Ubuntu 20.04 Apache web page:
http://your_server_ip
4
Installing the Apache Web Server
4. Step 4 — Managing the Apache Process
❑ Now that you have your web server up and running, let’s go over some basic management
commands using systemctl. To stop your web server, type:
sudo systemctl stop apache2
❑ To start the web server when it is stopped, type:
sudo systemctl start apache2
❑ To stop and then start the service again, type:
sudo systemctl restart apache2
❑ If you are simply making configuration changes, Apache can often reload without dropping
connections. To do this, use this command:
sudo systemctl reload apache2
❑ By default, Apache is configured to start automatically when the server boots. If this is not what
you want, disable this behavior by typing:
sudo systemctl disable apache2
❑ To re-enable the service to start up at boot, type:
sudo systemctl enable apache2
❑ Apache should now start automatically when the server boots again.
5. Step 5 — Setting Up Virtual Hosts (Recommended)
❑ Apache on Ubuntu 20.04 has one server block enabled by default that is configured to serve
documents from the /var/www/html directory. While this works well for a single site, it can
become unwieldy if you are hosting multiple sites.
❑ Instead of modifying /var/www/html, let’s create a directory structure within /var/www for a
your_domain site, leaving /var/www/html in place as the default directory to be served if a client
request doesn’t match any other sites.
5
Installing the Apache Web Server
5. Step 5 — Setting Up Virtual Hosts (Recommended)
❑ Create the directory for your_domain as follows:
sudo mkdir /var/www/your_domain
❑ Next, assign ownership of the directory with the $USER environment variable:
sudo chown -R $USER:$USER /var/www/your_domain
❑ The permissions of your web roots should be correct if you haven’t modified your umask value,
which sets default file permissions. To ensure that your permissions are correct and allow the
owner to read, write, and execute the files while granting only read and execute permissions to
groups and others, you can input the following command:
sudo chmod -R 755 /var/www/your_domain
❑ Next, create a sample index.html page using nano or your favorite editor:
sudo nano /var/www/your_domain/index.html
❑ Inside, add the following sample HTML:
/var/www/your_domain/index.html
<html>
<head>
<title>Welcome to Your_domain!</title>
</head>
<body>
<h1>Success! The your_domain virtual host is working!</h1>
</body>
</html>
❑ Save and close the file when you are finished.
6
Installing the Apache Web Server
5. Step 5 — Setting Up Virtual Hosts (Recommended)
❑ In order for Apache to serve this content, it’s necessary to create a virtual host file with the
correct directives. Instead of modifying the default configuration file located at
/etc/apache2/sites-available/000-default.conf directly, let’s make a new one at
/etc/apache2/sites-available/your_domain.conf:
sudo nano /etc/apache2/sites-available/your_domain.conf
❑ Paste in the following configuration block, which is similar to the default, but updated for our
new directory and domain name:
/etc/apache2/sites-available/your_domain.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName your_domain
ServerAlias www.your_domain
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
❑ Notice that we’ve updated the DocumentRoot to our new directory and ServerAdmin to an
email that the your_domain site administrator can access. We’ve also added two directives:
ServerName, which establishes the base domain that should match for this virtual host
definition, and ServerAlias, which defines further names that should match as if they were the
base name.
❑ Save and close the file when you are finished.
7
Installing the Apache Web Server
5. Step 5 — Setting Up Virtual Hosts (Recommended)
❑ Let’s enable the file with the a2ensite tool:
sudo a2ensite your_domain.conf
❑ Disable the default site defined in 000-default.conf:
sudo a2dissite 000-default.conf
❑ Next, let’s test for configuration errors:
sudo apache2ctl configtest
❑ You should receive the following output:
Output
Syntax OK
❑ Restart Apache to implement your changes:
sudo systemctl restart apache2
❑ Apache should now be serving your domain name. You can test this by navigating to
http://your_domain, where you should see something like this:
❑ Apache virtual host example
8
Could not get lock /var/lib/dpkg/lock-frontend Error
1. The full error is:
E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
2. Do not remove the lock files immediately. Before doing so, locate and kill all processes that may be using the
files.
sudo lsof /var/lib/dpkg/lock-frontend
3. The output returns a result similar to:
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
Output information may be incomplete.
lsof: WARNING: can't stat() fuse file system /run/user/1000/doc
Output information may be incomplete.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
unattende 1127 root 2oW REG 7,2 0 165231 /var/lib/dpkg/lock-frontent
4. If you see unattended under the command column, your system is installing a security update. Wait for the
process to complete.
5. Otherwise, note all process IDs and kill them with the force option:
sudo kill -9 process_id
6. Once you do that, you can try removing the lock-frontend files. This should fix the error.
sudo rm /var/lib/dpkg/lock-frontend
7. The last thing you can try after deleting the lock files is reconfiguring dpkg as we showed in the fifth section.
sudo dpkg --configure -a
8. To test if these steps fixed the error, run the update command:
sudo apt update
9. When the process completes successfully, that shows you fixed the “E: Could not get lock
/var/lib/dpkg/lock” error.
1
PROCESS MAKE-IN-INDIA
RE-ENGINEERING
CAPACITY BUILDING
DIGITAL LOCKER
AURANGABAD