In this guide, we’ll walk you through the process of installing and configuring Nginx, PHP-FPM, and MySQL to build a powerful LEMP stack on Ubuntu 24.04, tailored for hosting WordPress sites. These essential tools provide the backbone for a high-performance web server and application environment. Let’s dive in and get started!
Before proceeding, ensure you have SSH access to your server:
ssh pivotlar@<your-server-ip>
ssh pivotlar@<your-server-ip>
You’ll be greeted with the Ubuntu 24.04 welcome screen, showing system details.
Step 1: Installing Nginx
Nginx is a powerful and popular web server choice. To get the latest stable version, use the repository maintained by Ondrej Sury:
Add the Nginx repository and update your package lists:
sudo add-apt-repository ppa:ondrej/nginx -y
sudo apt update
Upgrade existing packages:
sudo apt dist-upgrade -y
Install Nginx:
sudo apt install nginx -y
Confirm the installation:
nginx -v
You should see the installed version displayed.
Visit your server’s IP address in a browser to view the default Nginx welcome page. Use http:// since SSL is not configured yet.
Step 2: Configuring Nginx
Optimize Nginx for your server’s specifications:
Identify your CPU core count:
grep processor /proc/cpuinfo | wc -l
Check the open file limit:
ulimit -n
Edit the Nginx configuration file:
sudo nano /etc/nginx/nginx.conf
Adjust the following settings:
- Set the user to your login username (e.g.,
pivotlar). - Configure
worker_processesto match your CPU core count. - Update
worker_connectionsto your open file limit. - Enable
multi_acceptfor efficient connection handling.
Add or update directives as needed:
user pivotlar;
worker_processes auto;
events {
worker_connections 1024;
multi_accept on;
}
http {
keepalive_timeout 15;
sendfile on;
server_tokens off;
client_max_body_size 64m;
gzip on;
gzip_proxied any;
gzip_comp_level 5;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
}
Test and restart Nginx:
sudo nginx -t sudo service nginx restart
Step 3: Installing PHP 8.3
PHP-FPM integrates seamlessly with Nginx for dynamic content. Install PHP 8.3 using the Ondrej Sury repository:
Add the PHP repository and update:
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
Install PHP and required extensions:
sudo apt install php8.3-fpm php8.3-common php8.3-mysql \
php8.3-xml php8.3-intl php8.3-curl php8.3-gd \
php8.3-imagick php8.3-cli php8.3-dev php8.3-imap \
php8.3-mbstring php8.3-opcache php8.3-redis \
php8.3-soap php8.3-zip -y
Verify the installation:
php-fpm8.3 -v
Step 4: Configuring PHP-FPM
Configure PHP-FPM for better compatibility with Nginx:
Edit the PHP-FPM pool configuration:
sudo nano /etc/php/8.3/fpm/pool.d/www.conf
Update these lines:
user = pivotlar
group = pivotlar
listen.owner = pivotlar
listen.group = pivotlar
Modify php.ini for upload limits and performance:
sudo nano /etc/php/8.3/fpm/php.ini
Adjust the following values:
upload_max_filesize = 60M
post_max_size = 60M
opcache.enable_file_override = 1
Test and restart PHP-FPM:
sudo php-fpm8.3 -t sudo service php8.3-fpm restart
Step 5: Testing Nginx and PHP Integration
Ensure Nginx and PHP are working together:
Edit the default Nginx site configuration:
sudo nano /etc/nginx/sites-available/default
Uncomment and adjust the PHP block:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
Create a PHP info file:
cd /var/www/html
sudo nano info.php
Add the following content:
<?php phpinfo();
Assign proper permissions:
sudo chown pivotlar info.php
Test in your browser: Visit http://<your-server-ip>/info.php. You should see the PHP info screen.
Delete the test file:
sudo rm /var/www/html/info.php
Step 6: Configuring a Catch-All Server Block
To prevent default pages from displaying for unconfigured domains:
Remove the default site configuration:
sudo rm /etc/nginx/sites-available/default
sudo rm /etc/nginx/sites-enabled/default
Add a catch-all block to nginx.conf:
sudo nano /etc/nginx/nginx.conf
Insert this block under the include /etc/nginx/sites-enabled/*; line:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 444;
}
Test and restart Nginx:
sudo nginx -t sudo service nginx restart
Step 7: Installing WP-CLI
WP-CLI simplifies WordPress management from the command line:
Download WP-CLI:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Verify it works:
php wp-cli.phar --info
Make it executable and move to the system PATH:
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
Test WP-CLI:
wp --info
Step 8: Installing MySQL
Install and secure MySQL as part of your LEMP stack:
Install MySQL:
sudo apt install mysql-server -y
Run the security script to configure MySQL:
sudo mysql_secure_installation
Verify the installation:
mysql -u root -p
Congratulations! Your server is now fully configured with Nginx, PHP 8.3, and MySQL, providing a solid foundation to start hosting your WordPress sites seamlessly on Ubuntu 24.04. You're all set to launch your web projects with confidence!
