Welcome to Tencent Cloud + community, get more Tencent mass technology practice dry goods oh ~

This article was published in the Cloud + Community column by Step

introduce

The Apache HTTP server is the most widely used Web server in the world. It offers many powerful features, including dynamically loadable modules, powerful media support, and extensive integration with other popular software.

In this article, we will introduce how to install Apache Web server on Tencent Cloud Ubuntu 18.04 server.

Prerequisite for

Before starting this article, Tencent Cloud uses non-root users of Sudo by default. In addition, the least you can do is to start the firewall and shield some unnecessary ports. You can follow our Tencent Cloud Security group configuration to learn how to configure regular user accounts and set up a firewall for your server.

If you have an account available, log in as a non-root user to get started.

Step 1: Install Apache

Apache can be used in Ubuntu’s default software repository and thus installed using traditional package management tools.

Update the local software package first

sudo apt update
Copy the code

Then, install: Apache2

sudo apt install apache2
Copy the code

Once confirmed, APT will install Apache and all required dependencies.

The second step, set up the firewall

Before testing Apache, it is necessary to modify the firewall Settings to allow external access to the default Web port.

During the installation, Apache registers itself with the UFW firewall, as well as providing some application configuration files that can be used to enable or disable Access to Apache through the firewall.

Enter the following content to list the application configuration uFW

sudo ufw app list
Copy the code

You will see a list of application configuration files:

OutputAvailable application:
  Apache
  Apache Full
  Apache Secure
  OpenSSH
Copy the code

As you can see, Apache has three configuration files:

  • Apache: This profile opens only port 80 (normal, unencrypted Web traffic)
  • Apache Full: This configuration file opens port 80 (normal, unencrypted Web traffic) and port 443 (TLS/SSL encrypted traffic)
  • Apache Secure: This configuration file only opens port 443 (TLS/SSL encrypted traffic)

It is recommended that you enable the most restrictive profile, which still allows the traffic you configure. Since we haven’t configured SSL for our server in this article, we only need to allow traffic on port 80:

sudo ufw allow 'Apache'
Copy the code

You can enter the following content to check whether the modification is successful:

sudo ufw status
Copy the code

You should see the allowed HTTP traffic in the output that is displayed:

OutputStatus: Active to Action From -- ------ ---- OpenSSH ALLOW Anywhere Apache ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Apache (v6) Allow Anywhere (v6)Copy the code

As you can see, the active configuration file has allowed access to the Web server.

Step 3: Check the Web server

At the end of the installation process, Apache will be running on Ubuntu 18.04. The Web server should be up and running.

Check the Systemd init system to ensure the service is running by typing the following command:

sudo systemctl status apache2
Copy the code
The Output low apache2. Service - The Apache HTTP Server The Loaded: The Loaded (/ lib/systemd/system/apache2. Service; enabled; Vendor preset: enabled) Drop - In: / lib/systemd/system/apache2. Service. D └ ─ apache2 - systemd. Conf Active: active (running) since Tue 2018-04-24 20:14:39 UTC; 9min ago Main PID: 2583 (apache2) Tasks: 55 (limit: 1153) CGroup: / system. Slice/apache2 service ├ ─ 2583 / usr/sbin/apache2 -k start ├ ─ 2585 / usr/sbin/apache2 -k start └ ─ 2586 /usr/sbin/apache2 -k startCopy the code

As you can see from this output, the service appears to have started successfully. However, the best way to test this is to open the Apache web page.

You can access the default Apache login page to verify that the software is running properly through your IP address. If you don’t know the IP address of the server, you can get several different ways from the command line.

Try typing at the server’s command prompt:

hostname -I
Copy the code

You’ll get addresses separated by Spaces. You can try each in a Web browser to see if they work.

Alternatively, type the following command, which should help you find your IP address:

curl - 4 icanhazip.COM
Copy the code

After obtaining the IP address of the server, type it in the address bar of your browser:

http://your_server_ip
Copy the code

You should see the default page for Ubuntu 18.04 Apache:

This page indicates that Apache is running properly. It also includes some basic information about the location of important Apache files and directories.

Step 4 – Manage the Apache process

Now that you have your Web server up and running, let’s look at some basic administration commands.

To stop the Web server, type:

sudo systemctl stop apache2
Copy the code

To start the Web server when it stops, type:

sudo systemctl start apache2
Copy the code

To restart the service, type:

sudo systemctl restart apache2
Copy the code

If you just make configuration changes, Apache can usually reload without dropping the connection. Use the following command:

sudo systemctl reload apache2
Copy the code

By default, Apache is started at startup by default. If you do not want to boot, please enter the following command:

sudo systemctl disable apache2
Copy the code

To restart Apache, enter:

sudo systemctl start apache2
Copy the code

Apache should now be able to boot up.

Step 5: Set up the virtual host

With the Apache Web server, you can use virtual hosts (similar to server blocks in Nginx) to encapsulate configuration details, and you can host multiple domain names on the server. We will set up a domain name named example.com, but you should replace it with your own domain name.

Apache on Ubuntu 18.04 has a server block enabled by default, configured to serve documents from the /var/www/html/ directory. While this works for a single site, it can be a bit more difficult if you’re hosting multiple sites. Instead of modifying /var/www/html/, create a directory structure within the example.com site in /var/www/html/ and use /var/www/html/ as the default directory if the client request does not match any other site.

Create a directory for example.com as follows, using the -p flag to create the required parent directory:

sudo mkdir - p /var/www/example.com/html
Copy the code

Next, use the environment variable to assign ownership of the directory: $USER

sudo chown - R $USER:$USER/var/www/xample.com/html
Copy the code

If you have not changed your values, your Web root directory permissions should be correct, use the following command to grant permissions to your site

sudo chmod - R 755 /var/www/example.com
Copy the code

Next, you can use nano to edit.

nano /var/www/example.com/html/index.html
Copy the code

Add the following code into the/var/www/example.com/html/index.html:

<html> <head> <title>Welcome to Example.com</title> </head> <body> <h1>Success! The example.com server block is working! </h1> </body> </html>Copy the code

When finished, save and close the file.

In order for Apache to be able to serve this content, the virtual host file must be created with the correct instructions. Instead of directly modifying the default configuration file in /etc/apache2/sites-available/000-default.conf, create a new file in the following location: /etc/apache2/sites-available/example.com.conf

sudosudo nano /etc/apache2/sites-available/example.com.conf
Copy the code

Copy the code below to/etc/apache2/sites-available/example.com.conf, and make corresponding changes

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Copy the code

Notice that we have updated DocumentRoot to the new directory and ServerAdmin to the email that the administrator of the example.com site can access. We also added two directives: ServerName, which establishes the base field that should match the virtual host definition; ServerAlias This defines more names that should be matched, just like their names.

When finished, save and close the file.

Let’s enable this file using the tool: a2ensite

sudo a2ensite example.com.conf
Copy the code

Disable the default site defined below: 000-default.conf

sudo a2dissite 000-default.conf
Copy the code

Next, let’s test if the configuration is wrong:

sudo apache2ctl configtest
Copy the code

You should see the following output:

OutputSyntax Ok
Copy the code

Restart Apache:

sudo systemctl restart apache2
Copy the code

Apache should now serve your domain name. You can test this by navigation, and you should see something like example.com

Step 6: Familiar Apache files and directories

Now that you know how to manage the Apache service itself, you should take a few minutes to familiarize yourself with some important directories and files.

content

  • /var/www/html: The rest of the page including the home page is in this directory (by default only contains the default Apache page you saw earlier) is in/var/www/htmlIn this directory. This can be changed by changing the Apache configuration file.

Server Configuration

  • /etc/apache2: Apache configuration directory. All Apache configuration files reside here.
  • /etc/apache2/apache2.conf: Main Apache configuration file. It can be modified to change the Apache global configuration. This file is responsible for loading many other files in the configuration directory.
  • /etc/apache2/ports.conf: Specifies the port on which Apache will listen. By default, Apache listens on port 80 and on port 443 when modules that provide SSL are enabled.
  • /etc/apache2/sites-available/: a directory that can store all virtual host sites. Apache does not use configuration files in this directory unless they are linked tosites-enabledDirectory. Typically, all server block configuration is done in this directory, and then by using thata2ensiteThe command links to other directories.
  • /etc/apache2/sites-enabled/: a directory that stores all enabled web hosting sites. Usually, these are linked tosites-availableDirectory to create configuration filesa2ensite. Apache reads configuration files and links in this directory when it starts or reloads to compile the full configuration.
  • /etc/apache2/conf-available/./etc/apache2/conf-enabled/: These catalogues andsites-availableIt has the same relationship as a directorysites-enabled, but used to store configuration fragments that do not belong to a virtual host.conf-availableYou can use thea2enconfCommand to enable and disable a file in a directorya2disconf.
  • /etc/apache2/mods-available/./etc/apache2/mods-enabled/: These directories contain available and enabled modules respectively. with.loadThe ending file contains the fragment used to load a particular module, while the file is.confThe file at the end contains the configuration for these modules. You can usea2enmodanda2dismodCommand to enable and disable modules.

Server Logs

  • /var/log/apache2/access.log: By default, every request to the Web server is logged in this log file unless Apache is configured to do something else.
  • /var/log/apache2/error.log: By default, all errors are logged in this file.LogLevel: directive in the Apache configuration that sets how much detail the error log will contain.

Have you learned how to set up Apache web server on Ubuntu 16.04? Try to buy a server: cloud.tencent.com/product/cvm


Reference: How To Install the Apache Web Server on Ubuntu 18.04

Huang Chenghao, Proofread: Techeek

Question and answer

Disappear stored procedure?

reading

Tencent Cloud CIS entry — Kubernetes deployment

Tencent Cloud API: Use Tencent Cloud API with Python (machine translation example)

Host migration practice sharing

Has been authorized by the author tencent cloud + community release, the original link: https://cloud.tencent.com/developer/article/1158260?fromSource=waitui

Welcome to Tencent Cloud + community or follow the wechat public account (QcloudCommunity), the first time to get more mass technology practice dry goods oh ~

Massive technical practice experience, all in the cloud plus community!