Install PHP 7.3 on Arch Linux with Apache or Nginx

System Requirements

  • A server with an updated Arch Linux installation
  • An active web server (Apache or Nginx)
  • Sudo privileges
  • Commands that must be executed with root access are marked with #. It is advised to use sudo before each command when operating as a normal user
  • An installed text editor (e.g., vi, vim, nano, emacs) and basic familiarity with its usage

Installing PHP and FastCGI

To set up PHP along with its FastCGI process manager, run the following:

Setting the Time Zone for PHP

Visit PHP’s official timezone list. Once you’ve chosen your timezone, such as “America/New_York”, modify the /etc/php/php.ini file accordingly:

date.timezone = America/New_York

Starting and Enabling PHP FastCGI

Enable and launch the PHP FastCGI service to ensure it starts automatically upon boot:

# systemctl enable --now php-fpm

PHP Configuration for Apache

Creating a Dedicated Apache PHP-FPM Configuration File

Generate a file at /etc/httpd/conf/extra/php-fpm.conf and insert the following content. Ensure there are no spaces around the pipe character to avoid misconfigurations:

DirectoryIndex index.php index.html
<FilesMatch \.php$>
    SetHandler "proxy:unix:/run/php-fpm/php-fpm.sock|fcgi://localhost/"
</FilesMatch>

Allowing Apache to Use FastCGI

Modify /etc/httpd/conf/httpd.conf and append the following lines at the end of the LoadModule section:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

Allowing PHP Files in Hosted Sites

If you want PHP support enabled for all websites hosted on Apache, edit /etc/httpd/conf/httpd.conf and include this line at the end:

Include conf/extra/php-fpm.conf

For more granular control, if you use virtual hosts or separate directories for HTTP and HTTPS, insert the line above within the appropriate <VirtualHost> block.

Restart Apache

Apply all changes by restarting the Apache server:

PHP Configuration for Nginx

Creating a FastCGI Configuration File

Create the file /etc/nginx/php.conf and insert the following configuration:

# Correctly handle request like /test.php/foo/blah.php or /test.php/
fastcgi_split_path_info ^(.+?\.php)(/.*)$;

try_files $uri $document_root$fastcgi_script_name =404;

# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";

fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;

Editing Nginx Server Blocks

In the /etc/nginx/nginx.conf file, or within individual virtual host files, add the following inside each server block that should process PHP files:

location ~ \.php$ {
    root         /usr/share/nginx/html/;
    include      php.conf;
}

Restarting Nginx

Apply the new configuration by restarting the Nginx service:

Testing PHP Setup

Navigate to your web server’s root directory and create a test file named test.php containing the following:

Then open your browser and visit http://YOUR-SERVER-WEB-ADDRESS-OR-IP/test.php to confirm that PHP is functioning correctly. You should see a detailed PHP configuration page.

Don’t forget to remove the test.php file afterward to avoid exposing server information.

Conclusion

By completing the above steps, you have successfully installed and configured PHP 7.3 with FastCGI on an Arch Linux server running Apache or Nginx. You also ensured your setup is secure and verified through a test script. Always remember to clean up test files and regularly check for software updates to maintain a secure and optimized environment.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: