Home LAMP Stack Setup
Post
Cancel

LAMP Stack Setup

LAMP is a bundle of 4 different software technologies that developers use to build websites and application. LAMP is an acronym for the operating system, Linux; the web server, Apache; the database server, MySQL; and the programming language, PHP.

So going to practice some server setup for linux administration for practice.

We are going to setup a network of 4 computers and a load balancer which will help us manage traffic and suggest which server the user should communicate with.

  • 1 Load Balancer with HAProxy
  • 2 or more Web Servers with Nginx + PHP-FPM + Laravel and Jetstream installed
  • 2 database servers with #MySQL replication
  • 1 Redis server to store your #PHP sessions
  • NFS server to store your files on

Stack Setup

Table of Contents:

  1. How to How to Setup nginx, PHP, and PHP-FPM

How to Setup nginx, PHP, and PHP-FPM

  1. Install Nginx on Ubuntu.
  2. Install the php-fpm for Nginx package.
  3. Edit the server’s default config file to support PHP in Nginx.
  4. Add a new pool configuration file.
  5. Add a PHP file to Nginx’s html directory, Test the PHP, Nginx and PHP-FPM configuration..

Before continueing we need to make sure packages are good to go.

1
2
apt update
apt upgrade

Install nginx on ubuntu

nginx like Apache is a webserver that is used to host files that are accessible to the internet. I am using lubuntu installed on a couple different vms for this.

1
apt install nginx

and verify the server is operational

1
systemctl status nginx

Install the php-fpm packages

We can install php-fpm with the apt-get command. We have to make sure we install php-fpm and not php for conflicts will arise, it will install apache2 which will conflict with our nginx installation.

1
apt install php8.1-fpm php8.1-common php8.1-mysql php8.1-xml php8.1-xmlrpc php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-dev php8.1-imap php8.1-mbstring php8.1-opcache php8.1-redis php8.1-soap php8.1-zip -y

and make sure the service is running

1
systemctl status php8.1-fpm

Add PHP Support to nginx

We will edit the default nginx config file located at /etc/nginx/sites-available/default. This will allow the PHP FastCGI Process Managaer to handle requests that have a .php extension.

We will make the following changes to the Nginx config to support PHP and PHP-FPM on the server:

  • Add index.php to the index list.
  • Uncomment the PHP scripts to FastCGI entry block.
  • Uncomment the line to include snippets/fastcgi-php.conf.
  • Uncomment the line to enable the fastcgi_pass and the php8.1-fpm. sock.
  • Uncomment the section to deny all access to Apache .htaccess files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/wordpress/html;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/run/php/php8.1-fpm-wordpress.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
        #       deny all;
        #}
}

We can validate a nginx config file with nginx -t

1
2
3
root@0xskar-webserver01:/home/oskar# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Add a PHP file to Nginx’s html directory

We can add info.php to the webdirectory to check everything is working correctly

1
<?php echo phpinfo();?>x

Add a new pool configuration file

Visit /etc/php/8.1/fpm/pool.d and copy www.conf into a new file for example wordpress.conf and edit it to your prefrences. Just make sure to change the pool name and the listen to use the correct sock.

We can also add some custom enviromental variables here to make sure we everything is working correctly.

1
2
3
4
5
...
...
...
env[HOSTNAME] = $HOSTNAME
env[TMP] = /tmp

Upon doing all of this we can restart all of the services and visit the info.php. Should see at the top the server API is using FPM/FastCGI.

lamp php-fpm setup

We can do this again lets say for a joomla user and install a joomla website for them.

I’ve done this on one server with a wordpress at home.osk and a joomla site at joomla.home.osk, also another wordpress site at home2.osk on another nginx server. Going to setup mysql replication now.

This post is licensed under CC BY 4.0 by the author.