{"id":2118,"date":"2024-06-16T15:08:45","date_gmt":"2023-04-20T00:32:29","guid":{"rendered":""},"modified":"2025-02-02T00:46:18","modified_gmt":"2025-02-01T23:46:18","slug":"how-to-install-nginx-with-php-and-mariadb-lemp-stack-with-opcache-redis-and-let-039-s-encrypt-on-ubuntu","status":"publish","type":"post","link":"https:\/\/netcloud24.com\/knowledgebase\/how-to-install-nginx-with-php-and-mariadb-lemp-stack-with-opcache-redis-and-let-039-s-encrypt-on-ubuntu\/","title":{"rendered":"How to Install Nginx with PHP and MariaDB (LEMP Stack) with Opcache, Redis, and Let&#039;s Encrypt on Ubuntu"},"content":{"rendered":"<header>\n<h1>\u00a0<\/h1>\n<\/header>\n<article>\n<section>\n<h2>Introduction<\/h2>\n<p>LEMP stack consists of Linux, Nginx, MySQL\/MariaDB, and PHP, providing a powerful framework for web hosting. This guide will walk you through the installation of Nginx, PHP, and MariaDB on Ubuntu, along with Opcache for caching, Redis for data storage, and Let&#8217;s Encrypt for SSL certificates. This setup is ideal for deployment on a  for improved performance and security.<\/p>\n<\/section>\n<section>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>An Ubuntu server (20.04 or later) with root access<\/li>\n<li>Basic knowledge of Linux commands<\/li>\n<li>A domain name pointed to your server (for Let&#8217;s Encrypt)<\/li>\n<\/ul>\n<\/section>\n<section>\n<h2>Step 1: Update Your System<\/h2>\n<p>Begin by updating your package index and upgrading any installed packages:<\/p>\n<pre><code>sudo apt update &amp;&amp; sudo apt upgrade -y<\/code><\/pre>\n<\/section>\n<section>\n<h2>Step 2: Install Nginx<\/h2>\n<p>Install Nginx using the following command:<\/p>\n<pre><code>sudo apt install nginx -y<\/code><\/pre>\n<p>Start and enable Nginx to run on boot:<\/p>\n<pre><code>sudo systemctl start nginx\r\nsudo systemctl enable nginx<\/code><\/pre>\n<\/section>\n<section>\n<h2>Step 3: Install MariaDB<\/h2>\n<p>Install MariaDB by executing the following command:<\/p>\n<pre><code>sudo apt install mariadb-server -y<\/code><\/pre>\n<p>Secure the MariaDB installation:<\/p>\n<pre><code>sudo mysql_secure_installation<\/code><\/pre>\n<p>Follow the prompts to set a root password and secure your installation.<\/p>\n<\/section>\n<section>\n<h2>Step 4: Install PHP and Extensions<\/h2>\n<p>Install PHP along with the necessary extensions:<\/p>\n<pre><code>sudo apt install php-fpm php-mysql php-opcache php-redis -y<\/code><\/pre>\n<p>To install additional PHP extensions, you can run:<\/p>\n<pre><code>sudo apt install php-xml php-mbstring php-curl php-zip php-gd -y<\/code><\/pre>\n<\/section>\n<section>\n<h2>Step 5: Configure PHP Opcache<\/h2>\n<p>Edit the PHP configuration file to enable Opcache:<\/p>\n<pre><code>sudo nano \/etc\/php\/8.1\/fpm\/conf.d\/10-opcache.ini<\/code><\/pre>\n<p>Add or uncomment the following lines:<\/p>\n<pre><code>opcache.enable=1\r\nopcache.memory_consumption=128\r\nopcache.interned_strings_buffer=8\r\nopcache.max_accelerated_files=10000\r\nopcache.revalidate_freq=2\r\nopcache.fast_shutdown=1<\/code><\/pre>\n<p>Save the file and restart PHP:<\/p>\n<pre><code>sudo systemctl restart php8.1-fpm<\/code><\/pre>\n<\/section>\n<section>\n<h2>Step 6: Configure Nginx to Use PHP<\/h2>\n<p>Edit the default Nginx configuration file:<\/p>\n<pre><code>sudo nano \/etc\/nginx\/sites-available\/default<\/code><\/pre>\n<p>Update the server block to include the following configuration:<\/p>\n<pre><code>server {\r\n    listen 80;\r\n    server_name your_domain.com; # Replace with your domain\r\n\r\n    root \/var\/www\/html; # Path to your web files\r\n    index index.php index.html index.htm;\r\n\r\n    location \/ {\r\n        try_files $uri $uri\/ \/index.php?$query_string;\r\n    }\r\n\r\n    location ~ \\.php$ {\r\n        include snippets\/fastcgi-php.conf;\r\n        fastcgi_pass unix:\/var\/run\/php\/php8.1-fpm.sock; # Adjust PHP version if necessary\r\n        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\r\n        include fastcgi_params;\r\n    }\r\n\r\n    location ~* \\.(jpg|jpeg|gif|png|css|js|ico|svg)$ {\r\n        expires 30d;\r\n        access_log off;\r\n    }\r\n}<\/code><\/pre>\n<p>Test the Nginx configuration for syntax errors:<\/p>\n<pre><code>sudo nginx -t<\/code><\/pre>\n<p>If everything is correct, restart Nginx:<\/p>\n<pre><code>sudo systemctl restart nginx<\/code><\/pre>\n<\/section>\n<section>\n<h2>Step 7: Install and Configure Redis<\/h2>\n<p>Install Redis server:<\/p>\n<pre><code>sudo apt install redis-server -y<\/code><\/pre>\n<p>Start and enable the Redis service:<\/p>\n<pre><code>sudo systemctl start redis-server\r\nsudo systemctl enable redis-server<\/code><\/pre>\n<\/section>\n<section>\n<h2>Step 8: Secure Your Site with Let&#8217;s Encrypt<\/h2>\n<p>Install Certbot and the Nginx plugin:<\/p>\n<pre><code>sudo apt install certbot python3-certbot-nginx -y<\/code><\/pre>\n<p>Obtain an SSL certificate:<\/p>\n<pre><code>sudo certbot --nginx -d your_domain.com<\/code><\/pre>\n<p>Follow the prompts to set up your SSL certificate.<\/p>\n<\/section>\n<section>\n<h2>Step 9: Test Your Setup<\/h2>\n<p>To test if everything is working correctly, navigate to <code>http:\/\/your_domain.com<\/code> in your web browser. You should see the Nginx default page or your application if you have uploaded files to <code>\/var\/www\/html<\/code>.<\/p>\n<\/section>\n<section>\n<h2>Step 10: Conclusion<\/h2>\n<p>You have successfully installed Nginx with PHP and MariaDB (LEMP stack) on Ubuntu, including Opcache, Redis, and Let&#8217;s Encrypt for secure hosting. This setup provides a robust platform for web applications, especially when using a . For further options, consider exploring , including <a href=\"https:\/\/ie.netcloud24.com\">Windows VPSVirtual Private Server Hosting<\/a> and <a href=\"https:\/\/ie.netcloud24.com\">Windows VPS Hosting UK<\/a> for optimal performance.<\/p>\n<\/section>\n<\/article>\n<footer>\n<p>\u00a9 2024 LEMP Stack Installation Tutorial. All rights reserved.<\/p>\n<\/footer>\n<div class=\"post-author-box\" style=\"border-top:1px solid #ddd;margin-top:20px;padding-top:15px;\">\n<p><strong>Author:<\/strong> \u0141ukasz Bodziony<\/p>\n<p><strong>Website:<\/strong> <a href=\"https:\/\/ca.netcloud24.com\" target=\"_blank\" rel=\"dofollow\">Windows VPS<\/a><\/p>\n<p><em>\u0141ukasz Bodziony is the CEO and founder of <a href=\"https:\/\/netcloud24.com\" target=\"_blank\" rel=\"dofollow\">NETCLOUD24<\/a>, a global VPS hosting brand proudly originating from Poland. With extensive experience in cloud computing, virtualization, and server management, he delivers high-performance <strong>Windows VPS<\/strong> and <strong>Remote Desktop Services (RDS)<\/strong> solutions to clients across Europe, North America, and beyond.<\/em><\/p>\n<p><em>His expertise covers a wide range of technologies, including <strong>Microsoft Azure<\/strong>, <strong>Proxmox VE<\/strong>, <strong>Amazon Web Services (AWS)<\/strong>, and numerous other virtualization and cloud platforms.<\/em><\/p>\n<p><em>Beyond running his hosting business, \u0141ukasz also provides <strong>professional paid server configuration and optimization services<\/strong> for companies and individuals. Outside of work, he is dedicated to caring for his children and building a secure future for them.<\/em><\/p>\n<p><em>If you are interested in working with him or need expert assistance with your hosting, cloud environment, or server setup, feel free to reach out via <a href=\"https:\/\/ca.netcloud24.com\" target=\"_blank\" rel=\"dofollow\">Windows VPS<\/a>.<\/em><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 Introduction LEMP stack consists of Linux, Nginx, MySQL\/MariaDB, and PHP, providing a powerful framework for web hosting. This guide will walk you through the installation of Nginx,\u2026<\/p>\n","protected":false},"author":1,"featured_media":3421,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[],"tags":[14,12,11,23,20,21,22,17,7,8,6,10,18,19,15,24,16,5,13,9],"class_list":["post-2118","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","tag-cheapvps","tag-cloudvps","tag-hostingvps","tag-rds","tag-rdscal","tag-remotedesktop","tag-remotedesktopvps","tag-servervps","tag-ukvps","tag-virtualserver","tag-vpshosting","tag-vpsserver","tag-vpssolutions","tag-vpswindows","tag-vpswithwindows","tag-windowsrds","tag-windowsserver","tag-windowsvps","tag-windowsvpshosting","tag-windowsvpsuk"],"jetpack_publicize_connections":[],"_links":{"self":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/2118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/comments?post=2118"}],"version-history":[{"count":0,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/2118\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/media\/3421"}],"wp:attachment":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/media?parent=2118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/categories?post=2118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/tags?post=2118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}