How To Install Nginx with PHP-FPM on Alpine Linux

In today’s guide we will cover the installation of Nginx Web server with PHP-FPM on Alpine Linux server. Nginx is high performance, open source web server which can also be used as reverser proxy. When hosting a PHP application with Nginx web server, a FastCGI Process Manager is required for loading web pages. FPM is the recommended FastCGI handler to be used with Nginx.

Login to your Alpine Linux Server and make sure it is updated. If converting this steps to container image build process, the commands are to be added to your Dockerfile.

$ sudo apk update
fetch http://nl.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
fetch http://nl.alpinelinux.org/alpine/edge/main/x86_64/APKINDEX.tar.gz
fetch http://nl.alpinelinux.org/alpine/edge/community/x86_64/APKINDEX.tar.gz
v3.7.3-183-gcc9ad2b48d [http://nl.alpinelinux.org/alpine/v3.7/community]
v20200626-2349-g06046a5a3e [http://nl.alpinelinux.org/alpine/edge/main]
v20200626-2353-g8dcf96ab2b [http://nl.alpinelinux.org/alpine/edge/community]
OK: 16383 distinct packages available

Also upgrade list of packages installed.

sudo apk upgrade

Once you’ve update application list index on Alpine Linux you can proceed to install and configure Nginx with PHP-FPM.

Install Nginx on Alpine Linux

Use the apk command line tool to install Nginx web server on Alpine Linux.

$ sudo apk add nginx
(1/3) Installing pcre (8.45-r0)
(2/3) Installing nginx (1.27.2-r3)
Executing nginx-1.27.2-r3.pre-install
(3/3) Installing nginx-openrc (1.27.2-r3)
Executing busybox-1.32.0-r1.trigger
2 errors; 114 MiB in 59 packages

Start nginx service after installation.

$ sudo rc-service nginx start
 * Caching service dependencies ...                                                                                                                         [ ok ]
 * /run/nginx: creating directory
 * /run/nginx: correcting owner
 * Starting nginx ...                                                                                                                                       [ ok ]
alpine:~$

Confirm service status:

$ rc-service nginx status
 * status: started

Set nginx service to be started at system boot.

$ sudo rc-update add nginx default
 * service nginx added to runlevel default

Install curl:

$ sudo apk add curl
(1/5) Installing ca-certificates (20191127-r4)
(2/5) Installing brotli-libs (1.0.7-r5)
(3/5) Installing nghttp2-libs (1.41.0-r0)
(4/5) Installing libcurl (7.71.1-r0)
(5/5) Installing curl (7.71.1-r0)
Executing busybox-1.32.0-r1.trigger
Executing ca-certificates-20191127-r4.trigger
2 errors; 117 MiB in 64 packages

Use curl command to confirm the web server is working.

$ curl -ik localhost
HTTP/1.1 404 Not Found
Server: nginx
Date: Thu, 30 Oct 2024 22:04:23 GMT
Content-Type: text/html
Content-Length: 146
Connection: keep-alive
....

Install PHP-FPM on Alpine Linux

Next instal php packages and include php-fpm extension on Alpine Linux.

sudo apk --update add \
        php8.3 \
        php8.3-bcmath \
        php8.3-ctype \
        php8.3-curl \
        php8.3-fpm \
        php8.3-gd \
        php8.3-iconv \
        php8.3-intl \
        php8.3-json \
        php8.3-mbstring \
        php8.3-mcrypt \
        php8.3-mysqlnd \
        php8.3-opcache \
        php8.3-openssl \
        php8.3-pdo \
        php8.3-pdo_mysql \
        php8.3-pdo_pgsql \
        php8.3-pdo_sqlite \
        php8.3-phar \
        php8.3-posix \
        php8.3-session \
        php8.3-soap \
        php8.3-xml \
        php8.3-zip

Start and enable service.

$ sudo rc-service php-fpm8.3 start
 * Caching service dependencies ...                                                                                                                         [ ok ]
 * Checking /etc/php8.3/php-fpm.conf ...
 * /run/php-fpm8.3: creating directory
 * Starting PHP FastCGI Process Manager ...                                                                                                                 [ ok ]
alpine:~$

PHP and FPM configuration files are available in the following directory.

$ ls /etc/php8.3/
conf.d        php-fpm.conf  php-fpm.d     php.ini

You can install vim and use it to modify configuration files.

sudo apk add vim

You can now upload PHP web application data and configure Nginx to serve your web content.

Explore More with CloudSpinx

Looking to streamline your tech stack? At CloudSpinx, we deliver robust solutions tailored to your needs:

  • Hosting Services: Web Hosting, VPS, and Email Hosting
  • Development Services: Custom Web & Mobile Apps, UI/UX Design
  • Consultancy Services: Cloud, DevOps, Cybersecurity, and Kubernetes Experts
  • Managed Services: End-to-end infrastructure support
  • Dedicated Servers: Power and control, fully managed

Learn more about how we can support your journey with CloudSpinx – Linux Infra, Containerization, Virtualization, IT services, among many others.

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

The web browser is the key arsenal that we use to browse the internet. If you want to access information […]

In this guide,we will look at how you can install Java 11 on Oracle Linux 8. Java is a widely […]

The adoption of Containers and microservice architectures has been amazing and speedy in the past few years. Docker is widely […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.