Welcome to my test site!

How to deploy an Apache web server quickly

Laptop with receipts

Sometimes you need to deploy a web server quickly. A web server makes it easy to do things like test web applications, implement a proof-of-concept project, or just get familiar with web development.

Great Linux resources

The steps below cover how to do that using the venerable Apache HTTP Server. I’ll begin with installation, then continue with editing a basic configuration file, creating a few documents, setting the firewall, and finally, testing the service.

Note that this is a very basic test deployment. It’s not suitable for internet-facing web servers. To be production-ready, you must configure firewalls and audit your server settings.

Install Apache

If your Linux distribution doesn’t already include Apache, run the following command to install the package:

Adjust the command accordingly if you use a different package manager.

Edit the config file

Using your text editor of choice (mine is Vim, but Nano or others work just as well), open /etc/httpd/conf/httpd.conf .

These two values may already be set in that file, but confirm them to be sure:

DocumentRoot /var/www/html Listen 80

Use the search function in your editor to find the appropriate stanzas. You may need to uncomment the DocumentRoot stanza. This article uses the standard location at /var/www/html .

The Listen directive simply indicates which port the service listens on. In this case, use the standard port 80 for HTTP.

[ Better integrate security into your software lifecycle. Download the guide to implementing DevSecOps. ]

Manage the service

You’ve installed a web server, but you haven’t started running it yet. Use the systemctl command to start and enable the httpd service:

$ sudo systemctl start httpd $ sudo systemctl status httpd

Starting a service doesn’t mean it will start automatically from now on. To make your web server start automatically after a reboot, use the enable subcommand:

$ sudo systemctl enable --now httpd

Open port 80

Now that the site is configured (and in a real-world situation, secured), it’s time to open the firewall. I’ll assume you’re using firewalld , so type the following commands to permit HTTP traffic into the server:

$ sudo firewall-cmd --permanent --zone=public --add-service=http $ sudo firewall-cmd --reload $ sudo firewall-cmd --list-all --zone=public

You may need to adjust the zone value, depending on your distribution.

Test the server

IT Automation ebook

First, test the server with a browser. Launch Firefox and navigate to localhost:80 to see the Apache web server’s default confirmation page.

Читайте также:  All in one emulator linux

Create content

You probably didn’t install a web server just to see «It works!» in your browser. To make your server a little more interesting, launch your favorite text editor using sudo , and create a few pages to simulate a website. Feel free to make this as simple or complex as you’d like. You can also create a text file to test downloads from the site.

Your site’s homepage is by default index.html , and it’s located in /var/www/html . Make a backup copy of the existing index.html file and create a new one containing the following code:

    This site is for testing purposes only 

Additionally, create a new text file called download-this.txt with the following content so that you can test the curl and wget commands later:

If you can read this, the download succeeded!

Store these two files at /var/www/html .

Look at files

Assuming that works, do a test download of the download-this.txt file.

Use a different system on the same segment to test the web service. On the new system, launch a web browser. In the address bar, type the web server’s IP address.

Don’t forget, you can launch a browser and point it at a site from the terminal:

Do you see your welcome page?

Sysadmins frequently use curl or wget to download files using HTTP. This ability is particularly handy for hands-free downloads in scripts or other automation scenarios.

Run these two commands to confirm file download functionality:

$ sudo dnf -y install curl $ curl -O http://IP/download-this.txt
$ sudo dnf -y install wget $ wget http://IP/download-this.txt

Did you receive the download-this.txt file? Use ls to display the file and cat to read its contents. You might have two copies of the file if you tested the download using both the curl and wget commands.

Check the logs

Check the Apache log files for access information. For example, type the following to see log entries indicating the file download transactions:

$ sudo cat /var/log/httpd/access_log | grep -I download-this.txt

Wrap up

That’s it! You’ve installed, configured, and tested a very basic web server deployment in just a few steps. Don’t forget to investigate security settings before placing such a deployment in your production environment or exposing it to the outside world.

Источник

Quickly set up a LAMP stack on Red Hat Enterprise Linux 8

Red Hat Enterprise Linux 8

Have you tried Red Hat Enterprise Linux 8 (RHEL8) yet? Read on to learn how to quickly set up a LAMP stack on RHEL8 so you can play around with the new features built into the operating system.

A LAMP stack is made up of four main components and some glue. The first main component in a LAMP stack (the «L») is Linux. In my example, I’m using Red Hat Enterprise Linux 8 for that, which gives me a secure operating system, a modern programming environment, and a user-friendly set of tools to control it.

Читайте также:  Check ssl certificate linux

As for the web server, traditionally the “A” in LAMP stood for Apache, but in Red Hat Enterprise Linux 8, we actually have options here. We ship Apache httpd with RHEL8, but we also ship NGINX. Because I’m a bit traditional here, I’ll opt for Apache.

In RHEL8, Apache ships as an AppStream, which—among other things—allows us to provide content with varying life cycles. With AppStreams, we can, for example, ship multiple versions of Python and add new versions of programming environments outside of the normal RHEL release cadence.

Installing Apache on RHEL8 as easy as it was on earlier versions of Red Hat Enterprise Linux. Run:

(You are using sudo , right? If you didn’t set your user ID to be an administrator during installation, see How to enable sudo on Red Hat Enterprise Linux.) This command enables the Apache 2.4 AppStream and installs the httpd package, including its default dependencies.

To start this newly installed web server and make sure it will automatically start after a reboot, I’ll need to run:

$ sudo systemctl enable --now httpd

And, because I’ll want my server to be reachable over the network, I’ll need to open ports 80 and 443 on my system. We can do that from the Red Hat Enterprise Linux 8 Web Console (see the DevNation video at the end of this article for a demo), but for now, let’s use the command-line tools provided with RHEL8. They are quite easy:

$ sudo firewall-cmd --add-service=http --add-service=https
$ sudo firewall-cmd --add-service=http --add-service=https --permanent

That’s it. The first command opens ports 80 and 443 right now, and the second command makes sure that, after a reboot or firewall restart, the ports remain open.

Now for the database part. Again, traditionally, the “M” in LAMP stood for MySQL. However, nowadays, it can also mean MariaDB, MongoDB, or even PostgreSQL. You can see what databases RHEL8 ships with by running:

(I’ve stripped the non-database AppStreams from the output in Figure 1 for brevity.)

As you can see, MongoDB is not an option for RHEL8. You can read the RHEL8 release notes for a little background on why that is. What we do have, though, is MySQL 8, MariaDB 10.3, PostgreSQL 9.6 and 10, and Redis 5. That’s a lot to choose from!

I want to build a fairly traditional LAMP stack here, so I’ll opt for MariaDB, which is a drop-in replacement for MySQL. I want to install a database server, so the default profile (‘ server ’, indicated by the [d] in the output above) will work for me. If I only wanted the client bits, I could have installed the client profile, saving me a bit of disk space, and giving me, obviously, only the client bits of MariaDB.

$ sudo yum -y module install mariadb

By the way, a standard sudo yum -y install mariadb-server will work just as well.

Читайте также:  How to install apps on linux

A database that’s not running is of little use, so let’s start it with:

$ sudo systemctl enable --now mariadb

I don’t need to open firewall ports, because my web server and database server run on the same machine. If you have separate machines for Apache and MariaDB, you’ll need to add the MySQL service to the firewall, using the firewall-cmd command I showed above. You would also need to tune the SELinux policy to allow Apache to make network connections to a database (safety first!), by running:

$ sudo setsebool -P httpd_can_network_connect_db on

Finally, because I take my lessons around security to heart, I’ll run the mysql_secure_installation script:

$ sudo mysql_secure_installation

We are almost there. I have a proper Linux machine, I have my web server, and I have my database server. What’s still missing is a programming environment and some glue. Let’s see what programming environments are available for the «P» in LAMP:

I’ll not show the whole output again here, but we have PHP, we have Python in two major versions, we have Ruby, and a plethora of other options. Traditional LAMP means PHP for me, though, so that’s what I’ll be installing. One simple command should that care of it:

$ sudo yum -y module install php

Two final steps remain. First, there’s the glue. To enable connecting to the MariaDB database from my PHP pages, I need to install a tiny library:

$ sudo yum -y install php-mysqlnd

Then, as the final step, I’ll restart Apache to pick up my newly installed PHP and the PHP MySQL library:

$ sudo systemctl restart httpd

That’s it, we are done. We can go into /var/www/html and drop a PHP application in it and everything should work.

Some months back, Burr Sutter hosted me on DevNation Live, and we recorded an overview of RHEL8 from a developer point of view. We covered installing and using programming environments, managing your development systems, and much more. Interested? Watch the video:

I hope this overview is helpful when you’re ready to set up a LAMP stack on RHEL8. Let me know what you think in the comments or on Twitter at @MaximBurgerhout.

Last updated: June 13, 2023

Источник

Оцените статью
Adblock
detector