How to install and set up PHP and Apache(LAMP stack) on OpenSUSE Leap 15.3

In this guide we are going to Install and set up Apache virtual host to serve PHP content on an OpenSUSE Leap 15.4 system.

The Apache HTTP Server(Apache), is one of the most popular free and open-source cross-platform web server software, released under the terms of Apache License 2.0. Apache is popular as part of the LAMP setup, being the A in the Acronym. The apache server functionality can be extended with the many available modules.

PHP is a general-purpose scripting language geared towards web development. It is one of the popular programming languages for the web. Popular tools such as WordPress are coded using php. Big companies like Facebook also uses php heavily.

Prerequisites

To follow along this guide, please ensure you have the following:

  • An updated OpenSUSE leap 15.3 server
  • Root access to the server or User with sudo access
  • Internet access

Table of Content

  1. Ensuring that the server is up to date
  2. Installing PHP on OpenSUSE Leap 15.3
  3. Installing Apache on OpenSUSE Leap 15.3
  4. Configuring virtual host
  5. Testing the virtual host config

1. Ensuring that the server is up to date

Before proceeding, it is always a good practice to ensure that the server is up to date. Use these commands to achieve this

sudo zypper refresh
sudo zypper update -y

2. Installing PHP on OpenSUSE Leap 15.3

PHP is available in the default OpenSUSE repos. The latest version of PHP as of writing this blog is PHP 7.4.

Install PHP and some common packages using this command:

sudo zypper install -y \
    php \
    php-cli \
    php-gd \
    php-mbstring

Check the installed version of PHP

~> zypper info php7
Repository 'Update repository with updates from SUSE Linux Enterprise 15' is out-of-date. You can run 'zypper refresh' as root to update it.
Loading repository data...
Reading installed packages...


Information for package php7:
-----------------------------
Repository     : Update repository with updates from SUSE Linux Enterprise 15
Name           : php7
Version        : 7.4.6-3.29.1
Arch           : x86_64
Vendor         : SUSE LLC <https://www.suse.com/>
Installed Size : 10.3 MiB
Installed      : Yes
Status         : up-to-date
Source package : php7-7.4.6-3.29.1.src
Summary        : Interpreter for the PHP scripting language version 7
Description    :
    PHP is a server-side HTML embedded scripting language designed
    primarily for web development but also used as a general-purpose
    programming language.

    This package contains the standard implementation of PHP, namely Zend
    PHP. Included are the PHP command-line binary and the configuration
    file (php.ini). This package must be installed in order to use PHP.
    Additionally, extension modules and server modules (e.g. for Apache)
    may be installed.

    Additional documentation is available in package php-doc.

Check the installed php version using this command:

~> php -v
PHP 7.4.6 (cli) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

3. Installing apache on OpenSUSE Leap 15.3

Apache packages are available in the default OpenSUSE Leap 15.3 repositories as apache2. Install it using this command:

sudo zypper install -y apache2

Confirm the installed packages using this command:

~> zypper info apache2
Loading repository data...
Reading installed packages...


Information for package apache2:
--------------------------------
Repository     : Update repository with updates from SUSE Linux Enterprise 15
Name           : apache2
Version        : 2.4.43-3.32.1
Arch           : x86_64
Vendor         : SUSE LLC <https://www.suse.com/>
Installed Size : 4.3 MiB
Installed      : Yes
Status         : up-to-date
Source package : apache2-2.4.43-3.32.1.src
Summary        : The Apache Web Server
Description    :
    This version of httpd is a major release of the 2.4 stable branch,
    and represents the best available version of Apache HTTP Server.
    New features include Loadable MPMs, major improvements to OCSP support,
    mod_lua, Dynamic Reverse Proxy configuration, Improved Authentication/
    Authorization, FastCGI Proxy, New Expression Parser, and a Small Object
    Caching API.

     See /usr/share/doc/packages/apache2/, http://httpd.apache.org/, and
    http://httpd.apache.org/docs-2.4/upgrading.html.

The service will no tbe started by default.

sudo systemctl start apache2

Check the status using this command:

~> sudo systemctl status apache2
● apache2.service - The Apache Webserver
     Loaded: loaded (/usr/lib/systemd/system/apache2.service; disabled; vendor preset: disabled)
     Active: active (running) since Wed 2021-12-22 06:58:45 UTC; 53s ago
   Main PID: 27333 (httpd-prefork)
     Status: "Processing requests..."
      Tasks: 6
     CGroup: /system.slice/apache2.service
             ├─27333 /usr/sbin/httpd-prefork -DSYSCONFIG -C PidFile /var/run/httpd.pid -C Include /etc/apache2/sysconfig.d//loadmodule.conf -C Include /etc/apache2/sysconfig.d//global.conf -f /etc/apache>
             ├─27340 /usr/sbin/httpd-prefork -DSYSCONFIG -C PidFile /var/run/httpd.pid -C Include /etc/apache2/sysconfig.d//loadmodule.conf -C Include /etc/apache2/sysconfig.d//global.conf -f /etc/apache>
             ├─27341 /usr/sbin/httpd-prefork -DSYSCONFIG -C PidFile /var/run/httpd.pid -C Include /etc/apache2/sysconfig.d//loadmodule.conf -C Include /etc/apache2/sysconfig.d//global.conf -f /etc/apache>
             ├─27342 /usr/sbin/httpd-prefork -DSYSCONFIG -C PidFile /var/run/httpd.pid -C Include /etc/apache2/sysconfig.d//loadmodule.conf -C Include /etc/apache2/sysconfig.d//global.conf -f /etc/apache>
             ├─27343 /usr/sbin/httpd-prefork -DSYSCONFIG -C PidFile /var/run/httpd.pid -C Include /etc/apache2/sysconfig.d//loadmodule.conf -C Include /etc/apache2/sysconfig.d//global.conf -f /etc/apache>
             └─27344 /usr/sbin/httpd-prefork -DSYSCONFIG -C PidFile /var/run/httpd.pid -C Include /etc/apache2/sysconfig.d//loadmodule.conf -C Include /etc/apache2/sysconfig.d//global.conf -f /etc/apache>

Dec 22 06:58:45 ip-10-2-40-44 systemd[1]: Starting The Apache Webserver...
Dec 22 06:58:45 ip-10-2-40-44 systemd[1]: Started The Apache Webserver.

To enable the service on boot, use this command

sudo systemctl enable apache2

If you have a firewalld installed and enabled on your machine, remember to allow traffic to the Apache2 web server via the firewall before going to the next step.

$ sudo firewall-cmd --permanent --add-port=80/tcp
$ sudo firewall-cmd --permanent --add-port=443/tcp
$ sudo firewall-cmd --reload

For Apache to serve php files

sudo zypper install -y apache2-mod_php7

Then enable the php7 module

sudo a2enmod php7

Finally restart apache2 to apply the module addition

sudo systemctl restart apache2

4. Configuring virtual hosts

With Apache, it is possible to host multiple sites on the same server where the Apache is running. This can always be achieved using multiple domains each representing a site that the server is hosting.

Apache virtual hosts are similar to Nginx server blocks. Each virtual host will have configuration details for a single host. You can have as many virtual hosts as you want.

In this example, we will use the domain site1.citizix.com for the virtual host but be free to use the domain of your choice. Please note that the DNS for the domain to be used should already be configured to point to the IP address of the server.
Confirm that with:

dig -t A site1.citizix.com

Creating directory and sample content

The default page of apache is configured as a default virtual host serving content from /srv/www/htdocs. It’s a best practice to create the directory of your site in the /srv/www directory since its a best practice to serve from there.

Lets create a directory for our site with the following command:

sudo mkdir -p /srv/www/site1.citizix.com

Lets create a simple index.php page to serve from our site. You will require vim installed for this to work, if not use this command:

sudo zypper install -y vim

Edit the file:

sudo vim /srv/www/site1.citizix.com/index.php

Add content to the file:

<?php phpinfo(); ?>

Finally, lets make sure that the$USER user owns the site directory:

sudo chown -R wwwrun:wwwrun /var/www/site1.citizix.com

Creating virtual host

Apache Virtual host files specifies the configuration of each sites and tell the Apache web server how to respond to various domain requests.

The Apache configurations will be stored in the directory /etc/httpd/conf.d/. Lets create a config for our site:

sudo vim /etc/apache2/conf.d/site1.citizix.com.conf

Add the following content:

<VirtualHost *:80>
    ServerName site1.citizix.com
    ServerAlias site1.citizix.com

    ServerAdmin admin@citizix.com

    DocumentRoot /srv/www/site1.citizix.com
    ErrorLog /var/log/apache2/site1.citizix.com.error.log
    CustomLog /var/log/apache2/site1.citizix.com.requests.log combined

    <Directory "/srv/www/site1.citizix.com">
        Options Indexes
        AllowOverride All
        Require all granted
        DirectoryIndex index.php
    </Directory>
</VirtualHost>

This will tell Apache the following:

  • ServerName and ServerAlias – name of the host to serve
  • DocumentRoot – where to find the root directly that holds the publicly accessible web documents
  • DirectoryIndex – The file that apache will serve when the site is accessed
  • ErrorLog – where to store error logs for this site
  • CustomLog – where to store request logs for this particular site

Save and close the file when you are finished

Test the config

~> sudo apachectl configtest
Syntax OK

Then reload apache2 config

sudo systemctl reload apache2

5. Testing the Virtual Host config

To test the configuration, open your browser and load the domain you set up. In my case it is http://site1.citizix.com. You should see the php info page.

If you haven’t yet configured your DNS settings or simply do not want the site to go live yet, you can modify your computer’s hosts file. To do this, use the following command in your local computer’s CLI

sudo vim /etc/hosts

Then, add the IP address of your actual server followed by the domain name you are configuring, for example:

# Virtual Hosts Local Test
10.2.11.11 site1.citizix.com

Now you should be able to save the file and access it from within a web browser.

If something is not working as expected, you can check the virtualhost server logs using this

tail -f /var/log/apache2/site1.citizix.com.*

Conclusion

In this guide, we managed to set up Apache to serve PHP site on a Ubuntu 20.04 server.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy