Cart

    Sorry, we could not find any results for your search querry.

    Installing an Apache webserver in CentOS 7

    In this article, we explain the installation and initial configuration of an Apache web server (with httpd-tools, mod_ssl, and PHP-FPM) in CentOS 7.

    Together with MariaDB and PHP, Apache forms the so-called LAMP stack (Linux Apache Mariadb PHP). If you host a dynamic website (e.g. WordPress) on your VPS, you also need MariaDB (or a comparable alternative depending on your use case).

    In this article, we cover the following topics:

    • Installing Apache
    • Allowing Apache in your firewall
    • Installing PHP
    • Using vhosts

    In addition, we also recommend setting up an SFTP server and securing the websites that you host with SSL.

    • Execute the steps in this article as a root user or use sudo
       
    • Would you like to immediately test your Apache server? If so, point the DNS of one of your domains to your VPS (through the A- and AAAA-record).

    Installing Apache

     

    Step 1

    Connect to your VPS via SSH or use the VPS console and update your server first:

    yum -y update
        

     

    Step 2

    Install Apache (and the required extra packages) with the command:

    yum -y install httpd httpd-tools mod_ssl

    With this you install:

    • httpd: The Apache software itself.
    • httpd tools: Administrative and security scripts.
    • mod_ssl: SSL v3 and TLS v1.x support for Apache. 

    Allowing Apache in your firewall

     

    By default, CentOS 7 comes with Firewalld as a firewall and, out-of-the-box, all ports are closed in it. You open the http-(80) and https-(443) ports with the commands:

    firewall-cmd --zone=public --permanent --add-service=http
        firewall-cmd --zone=public --permanent --add-service=https
        firewall-cmd --reload

    The last command above makes the change live. By default, CentOS comes with Selinux as additional security. The http- and https- ports are already open by default and you do not need to adjust anything.


     

    Testing

    You can now test the functioning of your web server directly by going to http://example.com in a browser, replacing example.com with your hostname (checkable with the 'hostname' command), or the IP address of your VPS. You will then see the default test page:


    Installing and updating PHP

     

    Step 1

     

    Apache comes without PHP after installation. If you want to host a dynamic PHP website (e.g. WordPress), you will need PHP. Older versions of PHP are insecure, or will soon no longer receive updates, so below we install PHP 7.3

    First install the EPEL-release, Remi repository (which contains PHP 7.3) and yum-utils:

    yum -y install epel-release yum-utils 
    http://rpms.remirepo.net/enterprise/remi-release-7.rpm

     

    Step 2

    Enable the Remi repository with:

    yum-config-manager --enable remi-php73

     

    Step 3

    Now install PHP 7.3 with:

    yum -y install php php-common php-mysql php-fpm
    • php-mysql adds mysql support
    • php-fpm adds a PHP-FastCGI Process Manager that, compared to regular FastCGI, offers better performance for better-visited websites

    You check the exact installed version with:

    php -v

     

    Step 4

    Apache comes with a minimal configuration that does not automatically assume that you want to host a PHP website. The consequence is that when you visit a website that uses PHP (i.e. via index.php) the Apache placeholder is displayed instead of your website. 

    Therefore, it is important to make a small change in Apache so that Apache actually shows your website. To do this, open httpd.conf:

    nano /etc/httpd/conf/httpd.conf

     

    Step 5

    Look for the piece that looks like the one below:

    #
        # DirectoryIndex: sets the file that Apache will serve if a directory
        # is requested.
        #
        <IfModule dir_module>
            DirectoryIndex index.html
        </IfModule>

    Adjust it to:

    #
        # DirectoryIndex: sets the file that Apache will serve if a directory
        # is requested.
        #
        <IfModule dir_module>
            DirectoryIndex index.html index.php
        </IfModule>

    Save the changes and close Nano (ctrl + x> y> enter). It no longer matters whether a website uses index.html or index.php.


     

    Step 6

    Apache comes with a configuration that is not based on extra options such as PHP-FPM. A small adjustment in the Apache configuration is therefore required to use PHP-FPM.

    Open the PHP-FPM configuration file:

    nano /etc/httpd/conf.d/php.conf

     

    Step 7

    Scroll down to the line that starts with 'SetHandler' and adjust that from:

    SetHandler application/x-httpd-php

    to:

    SetHandler "proxy:fcgi://127.0.0.1:9000"

    Save your changes and close nano with ctrl + x> y> enter.


     

    Step 8

    Apache and PHP-FPM are disabled after installation and do not automatically start after a restart of CentOS. You enable both and let them start automatically with the commands:

    systemctl enable httpd
        systemctl enable php-fpm
        systemctl start httpd
        systemctl start php-fpm

    Your Apache server is now completely ready for PHP. Do not forget to install MariaDB if there is also a database linked to your website and to configure your VHost (see below) if you are hosting multiple sites.


    Using a VHost

     

    Do you want to host more than one domain? Then, you use a VHost system (also known as VirtualHost). A VHost file contains, among other things, the data on which specific VPS domains are hosted. As a result, visitors to domains that you host on your VPS are automatically sent to the correct folders on your VPS where the websites are located (without them noticing it).

     

    Step 1

    Create the vhost file with the command:

    nano /etc/httpd/conf.d/vhost.conf

     

    Step 2

    Copy the content below, replacing example.com with the name of the domain you want to host on your VPS. 

    NameVirtualHost *:80
        <VirtualHost *:80>
            ServerAdmin admin@example.com
            ServerName example.com
            ServerAlias www.example.com
            DocumentRoot /var/www/html/example.com/
            ErrorLog /var/log/httpd/example.com/error.log
            CustomLog /var/log/httpd/example.com/access.log combined
        </VirtualHost>

    For every extra domain that you want to add (regardless of the number), add the part from <VirtualHost *:80> up to </VirtualHost> again, replacing the domain name with the actual other domain. For example.com and example.nl this would look like this:

    NameVirtualHost *:80
        <VirtualHost *:80>
            ServerAdmin admin@example.com
            ServerName example.com
            ServerAlias www.example.com
            DocumentRoot /var/www/html/example.com/
            ErrorLog /var/log/httpd/example.com/error.log
            CustomLog /var/log/httpd/example.com/access.log combined
        </VirtualHost>
        <VirtualHost *:80>
            ServerAdmin admin@voorbeeld.nl
            ServerName voorbeeld.nl
            ServerAlias www.voorbeeld.nl
            DocumentRoot /var/www/html/voorbeeld.nl/
            ErrorLog /var/log/httpd/voorbeeld.nl/error.log
            CustomLog /var/log/httpd/voorbeeld.nl/access.log combined
        </VirtualHost>

     

    Step 3

    The directories listed in the previous step do not yet exist and you must first create them together with the log files. Replace example.com with the name of the domain you are adding:

    mkdir -p /var/www/html/example.com
        mkdir -p /var/log/httpd/example.com

    The -p (-parent) suffix creates all directory structures that stand for /example.com if necessary. In principle, all of these should already exist.


     

    Step 4

    You can now immediately get started with your website, by uploading it via SFTP or FTPS, or by creating an html website with a text editor from command-line (for PHP, see PHP installation). For example, you can create an info.php page as follows:

    • Open php.info:
    nano /var/www/html/example.com/info.php
    • Add the following content:
    <?php
        phpinfo();

     

    Your Apache server is now ready! Does your website use a database? Then, take a look at our MariaDB manual. To secure your website, you can use our Apache SSL manual.

    Should you have any questions left regarding this article, do not hesitate to contact our support department. You can reach them via the ‘Contact Us’ button at the bottom of this page.

    Need help?

    Receive personal support from our supporters

    Contact us