亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Install Nginx:
Install Apache (alternative):
2. Install PHP 8.1 from Remi Repository
Enable Remi and install PHP:
Verify installation:
3. Configure PHP-FPM for Nginx or Apache
Start and enable PHP-FPM:
Configure Nginx to use PHP-FPM:
Optimize php.ini:
4. Set Up a Database (MariaDB or MySQL)
Install MariaDB:
Secure installation:
5. Secure the Stack
6. Deploy Your PHP App Securely
7. Monitor and Maintain
Final Notes
Home Backend Development PHP Tutorial Building a Production-Ready PHP Stack on CentOS 9

Building a Production-Ready PHP Stack on CentOS 9

Jul 25, 2025 am 09:48 AM
PHP Installation

Install Nginx or Apache with firewall configuration for HTTP/HTTPS. 2. Install PHP 8.2 from the Remi repository and verify version. 3. Configure PHP-FPM with optimized php.ini and OPcache settings. 4. Install and secure MariaDB, then create a dedicated database and user. 5. Secure the stack using Let’s Encrypt for HTTPS, disable dangerous PHP functions, run PHP-FPM under least privilege, and enforce SELinux policies. 6. Deploy apps with proper file permissions, avoid root execution, and store configuration outside web root. 7. Monitor logs, enable slow request logging, automate security updates via dnf-automatic, and schedule regular backups of code and databases. This production-ready PHP stack on CentOS 9 ensures security, performance, and maintainability for deploying modern PHP applications reliably.

Building a Production-Ready PHP Stack on CentOS 9

Setting up a production-ready PHP stack on CentOS 9 means more than just installing PHP and a web server — it’s about security, performance, maintainability, and reliability. CentOS 9 (Stream), being a modern enterprise Linux distribution, provides a solid foundation. Here's how to build a robust PHP stack that’s suitable for real-world deployment.

Building a Production-Ready PHP Stack on CentOS 9

1. Install and Configure Apache or Nginx

While both Apache and Nginx are viable, Nginx is often preferred in production for its performance and low memory footprint. However, Apache with mod_php or PHP-FPM is still widely used and easier for beginners.

Install Nginx:

sudo dnf install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx

Install Apache (alternative):

sudo dnf install httpd -y
sudo systemctl enable httpd
sudo systemctl start httpd

? Don’t forget to adjust the firewall:

Building a Production-Ready PHP Stack on CentOS 9
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

For better performance and flexibility, use PHP-FPM regardless of your web server choice.


2. Install PHP 8.1 from Remi Repository

CentOS 9’s default repositories may not include the latest PHP versions. Use the Remi repository, which is trusted and widely used.

Building a Production-Ready PHP Stack on CentOS 9

Enable Remi and install PHP:

sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm -y
sudo dnf module enable php:8.2 -y
sudo dnf install php php-cli php-fpm php-mysqlnd php-gd php-xml php-mbstring php-pdo php-opcache php-curl php-zip -y

? Use PHP 8.2 or 8.3 if your apps support it — they offer better performance and features.

Verify installation:

php -v

3. Configure PHP-FPM for Nginx or Apache

PHP-FPM improves process management and is essential for high-traffic sites.

Start and enable PHP-FPM:

sudo systemctl enable php-fpm
sudo systemctl start php-fpm

Configure Nginx to use PHP-FPM:

Edit your site config (e.g., /etc/nginx/conf.d/default.conf) and update the server block:

location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

? For Apache, you can use mod_proxy_fcgi to route PHP requests to PHP-FPM, or stick with libphp if simpler setup is acceptable.

Optimize php.ini:

Edit /etc/php.ini and adjust:

upload_max_filesize = 64M
post_max_size = 128M
memory_limit = 512M
max_execution_time = 300
expose_php = Off
cgi.fix_pathinfo = 0

Also enable OPcache in /etc/php.d/opcache.ini:

opcache.enable=1
opcache.memory_consumption=256
opcache.max_accelerated_files=20000
opcache.validate_timestamps=0  ; Only in production; disable for dev
opcache.fast_shutdown=1

4. Set Up a Database (MariaDB or MySQL)

Most PHP apps need a database. MariaDB is default on CentOS.

Install MariaDB:

sudo dnf install mariadb-server mariadb -y
sudo systemctl enable mariadb
sudo systemctl start mariadb

Secure installation:

sudo mysql_secure_installation

Create a database and user:

CREATE DATABASE appdb;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;

5. Secure the Stack

Security is critical in production.

  • Use HTTPS with Let’s Encrypt:

    sudo dnf install certbot python3-certbot-nginx -y  # or python3-certbot-apache
    sudo certbot --nginx -d yourdomain.com
  • Disable unused PHP functions (optional but recommended): In php.ini:

    disable_functions = exec,passthru,shell_exec,system,proc_open,popen
  • Run PHP-FPM with least privilege: Edit /etc/php-fpm.d/www.conf:

    user = nginx
    group = nginx
    listen.owner = nginx
    listen.group = nginx
  • Enable SELinux policies: CentOS 9 uses SELinux by default. Ensure it's enforcing:

    sudo setsebool -P httpd_can_network_connect_db 1  # For DB connections
    sudo setsebool -P httpd_exec_mem 0                # Disable if not needed

6. Deploy Your PHP App Securely

  • Place your app in /var/www/html or a custom directory like /var/www/myapp.
  • Set correct permissions:
    sudo chown -R nginx:nginx /var/www/myapp
    sudo chmod -R 755 /var/www/myapp
    sudo chmod 644 /var/www/myapp/*.php
  • Avoid running web server as root.
  • Use .env files outside the web root for configuration.

7. Monitor and Maintain

  • Log monitoring:

    • Nginx: /var/log/nginx/error.log
    • PHP-FPM: /var/log/php-fpm.log
    • Enable slow log in PHP-FPM for debugging performance issues.
  • Automate updates: Use dnf-automatic to apply security patches:

    sudo dnf install dnf-automatic -y
    sudo systemctl enable dnf-automatic.timer
    sudo systemctl start dnf-automatic.timer
  • Backup regularly: Automate database and code backups using cron and mysqldump.


  • Final Notes

    A production-ready PHP stack on CentOS 9 isn’t just about installing components — it’s about configuring them securely, optimizing performance, and planning for maintenance. Stick to trusted repos like Remi, use PHP-FPM, enable HTTPS, and keep SELinux on.

    With this setup, you’re ready to deploy Laravel, WordPress, or any modern PHP application in a secure, scalable environment.

    Basically, keep it updated, locked down, and monitored.

    The above is the detailed content of Building a Production-Ready PHP Stack on CentOS 9. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
Harnessing the Power of WSL 2 for a Linux-Native PHP Development Workflow Harnessing the Power of WSL 2 for a Linux-Native PHP Development Workflow Jul 26, 2025 am 09:40 AM

WSL2isthenewstandardforseriousPHPdevelopmentonWindows.1.InstallWSL2withUbuntuusingwsl--install,thenupdatewithsudoaptupdate&&sudoaptupgrade-y,keepingprojectsintheLinuxfilesystemforoptimalperformance.2.InstallPHP8.3andComposerviaOnd?ejSury’sPPA

Mastering PHP-FPM and Nginx: A High-Performance Setup Guide Mastering PHP-FPM and Nginx: A High-Performance Setup Guide Jul 25, 2025 am 05:48 AM

NginxhandlesstaticfilesandroutesdynamicrequeststoPHP-FPM,whichprocessesPHPscriptsviaFastCGI;2.OptimizePHP-FPMbyusingUnixsockets,settingpm=dynamicwithappropriatemax_children,spareservers,andmax_requeststobalanceperformanceandmemory;3.ConfigureNginxwit

Setting Up PHP on macOS Setting Up PHP on macOS Jul 17, 2025 am 04:15 AM

It is recommended to use Homebrew to install PHP, run /bin/bash-c"$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" to install Homebrew, and then execute brewinstallphp or a specified version such as brewinstallphp@8.1; after installation, edit the php.ini file in the corresponding path to adjust memory_limit, upload_max_filesize, post_max_size and display_

Unlocking Peak PHP Performance: Configuring OPcache and JIT Compilation Unlocking Peak PHP Performance: Configuring OPcache and JIT Compilation Jul 24, 2025 pm 09:58 PM

OPcache and JIT are the core tools for PHP8.0 performance optimization. Correct configuration can significantly improve execution efficiency; 1. Enable OPcache and set opcache.enable=1, opcache.memory_consumption=192, opcache.max_accelerated_files=20000, opcache.validate_timestamps=0 to implement opcode caching and reduce parsing overhead; 2. Configure JIT to enable tracking JIT through opcache.jit_buffer_size=256M and opcache.jit=1254

Deploying a Scalable PHP Environment on AWS EC2 from Scratch Deploying a Scalable PHP Environment on AWS EC2 from Scratch Jul 26, 2025 am 09:52 AM

LaunchanEC2instancewithAmazonLinux,appropriateinstancetype,securesecuritygroup,andkeypair.2.InstallLAMPstackbyupdatingpackages,installingApache,MariaDB,PHP,startingservices,securingMySQL,andtestingPHP.3.DecouplecomponentsbymovingdatabasetoRDS,storing

Demystifying PHP Compilation: Building a Custom PHP from Source for Optimal Performance Demystifying PHP Compilation: Building a Custom PHP from Source for Optimal Performance Jul 25, 2025 am 06:59 AM

CompilingPHPfromsourceisnotnecessaryformostprojectsbutprovidesfullcontrolforpeakperformance,minimalbloat,andspecificoptimizations.2.ItinvolvesconvertingPHP’sCsourcecodeintoexecutables,allowingcustomizationlikestrippingunusedextensions,enablingCPU-spe

Automating Your PHP Environment Setup: Integrating PHP into a CI/CD Pipeline Automating Your PHP Environment Setup: Integrating PHP into a CI/CD Pipeline Jul 26, 2025 am 09:53 AM

ChooseaCI/CDplatformlikeGitHubActionsorGitLabCIfortightversioncontrolintegrationandminimalinfrastructure;2.DefineaconsistentPHPenvironmentusingcontainerizationwithimageslikephp:8.2-cliorcomposer:latestandinstalldependenciesviacomposerinstall--no-inte

Troubleshooting Common PHP Installation Pitfalls: A Diagnostic Checklist Troubleshooting Common PHP Installation Pitfalls: A Diagnostic Checklist Jul 26, 2025 am 09:50 AM

VerifysystemrequirementsanddependenciesbyconfirmingOScompatibilityandinstallingessentiallibrariesandbuildtools,usingpackagemanagerslikeaptoryumtosimplifydependencymanagement.2.CheckPHPconfigurationandcompilationerrorsbyrunningaminimal./configurecomma

See all articles