Install Nginx PhP on Alpine
To install Nginx and PHP on Alpine Linux, you can follow these steps. This guide assumes you have a fresh Alpine Linux installation.
1. Update Alpine Linux:
Start by updating the package index to make sure all repositories are up to date:
apk update
2. Install Nginx:
Install Nginx from the Alpine repositories:
apk add nginx
Install openrc:
apk add openrc --no-cache
After installation, you can start Nginx using:
rc-service nginx start
To enable Nginx to start on boot:
rc-update add nginx
3. Install PHP and PHP-FPM:
Install PHP and PHP-FPM, as PHP is often used with Nginx via the FastCGI Process Manager (FPM):
apk add php php-fpm php-mysqli php-json php-opcache php-session php-ctype php-zlib php-curl php-xml php-dom
This installs PHP and some common extensions. You can install more PHP extensions as needed for your application.
4. Configure PHP-FPM:
PHP-FPM is needed to process PHP scripts. Configure it to run with Nginx:
Edit the PHP-FPM configuration to set the correct user and group for running PHP processes. Open /etc/php83/php-fpm.d/www.conf
:
vi /etc/php83/php-fpm.d/www.conf
The php83 path can be replace based on what php installed.
Find the lines for user
and group
, and set them to nginx
:
user = nginx
group = nginx
5. Configure Nginx to Use PHP-FPM:
Now, configure Nginx to pass PHP requests to PHP-FPM.
Edit the Nginx configuration file (usually /etc/nginx/nginx.conf
or /etc/nginx/conf.d/default.conf
or /etc/nginx/http.d/default.conf
):
vi /etc/nginx/http.d/default.conf
Inside the server
block, make sure you have the following configuration to pass PHP files to PHP-FPM:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
Ensure that the root path (/usr/share/nginx/html
) is where your PHP files will be located.
If you dont have path /usr/share/nginx/html yet
:
mkdir -p /usr/share/nginx/html
6. Start PHP-FPM:
Start PHP-FPM:
rc-service php-fpm83 start
To enable PHP-FPM on boot:
rc-update add php-fpm83
7. Test the Configuration:
-
Create a simple
index.php
file to test PHP processing. Place this in the root of the Nginx server:echo "some php code"> /usr/share/nginx/html/index.php
-
Restart Nginx to apply any changes:
rc-service nginx restart
-
Open your web browser and navigate to http://your-server-ip/. You should see the PHP info page if everything is set up correctly.
8. Firewall (Optional):
If you're running a firewall, make sure to allow HTTP traffic (port 80):
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
9. Troubleshooting:
If something goes wrong, check the logs for Nginx (/var/log/nginx/) and PHP (/var/log/php-fpm.log) for debugging.