{"id":16425,"date":"2024-11-01T09:56:49","date_gmt":"2024-11-01T09:56:49","guid":{"rendered":"https:\/\/truehost.com\/support\/?post_type=docs&#038;p=16425"},"modified":"2024-11-01T20:40:50","modified_gmt":"2024-11-01T20:40:50","password":"","slug":"setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux","status":"publish","type":"docs","link":"https:\/\/truehost.com\/support\/knowledge-base\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\/","title":{"rendered":"Setting Up a LEMP Stack (Linux, Nginx, MySQL, PHP) on Almalinux"},"content":{"rendered":"\n<p>The LEMP stack (Linux, Nginx, MySQL, PHP) is a collection of software used to serve dynamic web pages and PHP applications. <\/p>\n\n\n\n<p>It consists of a Linux operating system, the Nginx (pronounced &#8220;Engine-X&#8221;) web server, a MySQL database for data storage, and PHP for handling dynamic content. <\/p>\n\n\n\n<p>This guide will walk you through setting up a LEMP stack on a server running Almalinux. With Almalinux managing the Linux component, we\u2019ll cover how to configure each of the remaining parts of the stack.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Almalinux 8.x or 9.x<\/li>\n\n\n\n<li>A user account with admin or sudo access<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Installing the Nginx Web Server<\/h2>\n\n\n\n<p>Nginx is a popular, efficient web server that we&#8217;ll use to serve pages to our visitors. Begin by updating your server\u2019s package index:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo dnf update -y<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"877\" height=\"107\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image.png\" alt=\"\" class=\"wp-image-16383\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image.png 877w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-300x37.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-768x94.png 768w\" sizes=\"auto, (max-width: 877px) 100vw, 877px\" \/><\/figure>\n\n\n\n<p>Then install Nginx:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo dnf install -y nginx<\/code><\/pre>\n\n\n\n<p>After installing Nginx, start the service and enable it to start on boot:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl start nginx\nsudo systemctl enable nginx<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Allowing Nginx Through the Firewall<\/strong><\/h4>\n\n\n\n<p>If you\u2019re using Firewalld , you\u2019ll need to allow HTTP traffic through it. Check available UFW application profiles with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo firewall-cmd --permanent --zone=public --add-service=http\nsudo firewall-cmd --permanent --zone=public --add-service=https\nsudo firewall-cmd --reload<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"168\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-2-1024x168.png\" alt=\"\" class=\"wp-image-16386\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-2-1024x168.png 1024w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-2-300x49.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-2-768x126.png 768w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-2.png 1073w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Verify the firewall status to confirm:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl status firewalld<\/code><\/pre>\n\n\n\n<p>To verify that Nginx is running, open your server\u2019s public IP or domain in a web browser. If unsure about your public IP, run one of the following commands to see it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ip addr show\n$ hostname -I<\/code><\/pre>\n\n\n\n<p>In your browser, navigate to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http:&#47;&#47;server_domain_or_ip_address<\/code><\/pre>\n\n\n\n<p>This should display the Nginx default landing page.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"249\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-3-1024x249.png\" alt=\"\" class=\"wp-image-16387\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-3-1024x249.png 1024w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-3-300x73.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-3-768x187.png 768w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-3.png 1238w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: 3. Install MySQL (or MariaDB)<\/h2>\n\n\n\n<p>With Nginx installed, we\u2019ll move on to installing MySQL, which will manage and store site data. <\/p>\n\n\n\n<p>You can also use MariaDB. Please refer to <strong><a href=\"https:\/\/truehost.com\/support\/knowledge-base\/how-to-install-mariadb-on-almalinux\/\" target=\"_blank\" rel=\"noreferrer noopener\">these steps<\/a><\/strong> on how to install MariaDB database on Almalinux<\/p>\n\n\n\n<p>For MySQL, run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo dnf install mysql-server<\/code><\/pre>\n\n\n\n<p>After installing MySQL, it is a good idea to run a security script included with it<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mysql_secure_installation<\/code><\/pre>\n\n\n\n<p>You\u2019ll be prompted to configure the VALIDATE PASSWORD PLUGIN\u2014type Y for yes or any other key to continue without enabling it.<\/p>\n\n\n\n<p>If you select \u201cyes,\u201d you\u2019ll be prompted to choose a level of password validation. I prefer that you choose level 1. <\/p>\n\n\n\n<p>It is fine to select yes fort he rest of the prompts, but then you can also choose differently according to your preference.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"627\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-5.png\" alt=\"\" class=\"wp-image-16394\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-5.png 800w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-5-300x235.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-5-768x602.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<p>Once done, check if you can log in to the MySQL console:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mysql<\/code><\/pre>\n\n\n\n<p>This command connects to the MySQL server using the administrative database user root, indicated by sudo. The expected output is as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"909\" height=\"262\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-4.png\" alt=\"\" class=\"wp-image-16392\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-4.png 909w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-4-300x86.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-4-768x221.png 768w\" sizes=\"auto, (max-width: 909px) 100vw, 909px\" \/><\/figure>\n\n\n\n<p>You may then run the command below to exit the console;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exit<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"635\" height=\"114\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-6.png\" alt=\"\" class=\"wp-image-16395\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-6.png 635w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-6-300x54.png 300w\" sizes=\"auto, (max-width: 635px) 100vw, 635px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Installing PHP<\/h2>\n\n\n\n<p>Next, Install PHP and any necessary modules that work with Nginx.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo dnf install -y php php-fpm php-mysqlnd<\/code><\/pre>\n\n\n\n<p>Start the PHP-FPM service and enable it to start on boot:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl start php-fpm<br>sudo systemctl enable php-fpm<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Configuring Nginx to Use PHP<\/h2>\n\n\n\n<p>We\u2019ll configure Nginx to handle PHP requests. By default, Nginx uses the directory <code>\/var\/www\/html<\/code> for its root web directory. We\u2019ll create a new directory for our website and set up a configuration file.<\/p>\n\n\n\n<p>In the steps below, remember to replace <em>your_domain<\/em> with your actual domain or subdomain. I will use <em>doll.tke.co.ke<\/em><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create the web root directory:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mkdir \/var\/www\/your_domain<\/code><\/pre>\n\n\n\n<p>   2. Assign ownership of the directory to the current user:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo chown -R $USER:$USER \/var\/www\/your_domain<\/code><\/pre>\n\n\n\n<p>   3. Open a new configuration file in the <strong>sites-available<\/strong> directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vi \/etc\/nginx\/sites-available\/your_domain<\/code><\/pre>\n\n\n\n<p>  4. Add the following configuration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>server {\n    listen 80;\n    server_name your_domain www.your_domain;\n    root \/var\/www\/your_domain;\n\n    index index.html index.htm index.php;\n\n    location \/ {\n        try_files $uri $uri\/ =404;\n    }\n\n    location ~ \\.php$ {\n        include snippets\/fastcgi-php.conf;\n        fastcgi_pass unix:\/var\/run\/php\/php8.1-fpm.sock;\n    }\n\n    location ~ \/\\.ht {\n        deny all;\n    }\n}\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"966\" height=\"386\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-7.png\" alt=\"\" class=\"wp-image-16396\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-7.png 966w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-7-300x120.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-7-768x307.png 768w\" sizes=\"auto, (max-width: 966px) 100vw, 966px\" \/><\/figure>\n\n\n\n<p>Below is what each of the directives do<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>listen<\/strong> specifies the port.<\/li>\n\n\n\n<li><strong>root<\/strong> sets the document root directory.<\/li>\n\n\n\n<li><strong>index<\/strong> prioritizes <code>index.html<\/code> over <code>index.php<\/code>.<\/li>\n\n\n\n<li><strong>server_name<\/strong> specifies the domain names.<\/li>\n\n\n\n<li><strong>location \/<\/strong> verifies file existence, returning a 404 if not found.<\/li>\n\n\n\n<li><strong>location ~ .php$<\/strong> handles PHP files via FastCGI.<\/li>\n\n\n\n<li><strong>location ~ \/.ht<\/strong> denies access to <code>.htaccess<\/code> files.<\/li>\n<\/ul>\n\n\n\n<p>5. Save and close the file (in <strong>vi<\/strong>, press <strong>Esc<\/strong>, type <code>:wq<\/code>, and press <strong>Enter<\/strong>).<\/p>\n\n\n\n<p>6. Enable your configuration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ln -s \/etc\/nginx\/sites-available\/your_domain \/etc\/nginx\/sites-enabled\/<\/code><\/pre>\n\n\n\n<p>7. Unlink the default configuration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo unlink \/etc\/nginx\/sites-enabled\/default<\/code><\/pre>\n\n\n\n<p>8.  Test your configuration for syntax errors:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nginx -t<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"918\" height=\"79\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-8.png\" alt=\"\" class=\"wp-image-16399\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-8.png 918w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-8-300x26.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-8-768x66.png 768w\" sizes=\"auto, (max-width: 918px) 100vw, 918px\" \/><\/figure>\n\n\n\n<p>9. Reload Nginx to apply the changes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl reload nginx<\/code><\/pre>\n\n\n\n<p>10. Add a sample <strong>index.html<\/strong> file to verify that your new server block works:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>vi \/var\/www\/your_domain\/index.html<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"797\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-9-1024x797.png\" alt=\"\" class=\"wp-image-16400\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-9-1024x797.png 1024w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-9-300x234.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-9-768x598.png 768w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-9.png 1246w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>11. Visit <code>http:\/\/your_domain<\/code> in your browser to confirm.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"702\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-10-1024x702.png\" alt=\"\" class=\"wp-image-16401\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-10-1024x702.png 1024w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-10-300x206.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-10-768x526.png 768w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-10.png 1261w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Testing PHP with Nginx<\/h2>\n\n\n\n<p>To test PHP, create a file called <strong>info.php<\/strong> in your document root:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vi \/var\/www\/your_domain\/info.php<\/code><\/pre>\n\n\n\n<p>Add the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nphpinfo();\n?&gt;<\/code><\/pre>\n\n\n\n<p>Access this page by navigating to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http:&#47;&#47;your_domain\/info.php<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"590\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-11-1024x590.png\" alt=\"\" class=\"wp-image-16403\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-11-1024x590.png 1024w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-11-300x173.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-11-768x443.png 768w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-11.png 1275w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>After confirming, delete the file to avoid exposing sensitive information:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo rm \/var\/www\/your_domain\/info.php<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 6: Testing Database Connection from PHP<\/h2>\n\n\n\n<p>Create a test database and user in MySQL:<\/p>\n\n\n\n<p><strong>Log in to MySQL:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mysql<\/code><\/pre>\n\n\n\n<p><strong>Create a new database:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Replace test_db with your preferred database name<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE DATABASE test_db;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"850\" height=\"54\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-14.png\" alt=\"\" class=\"wp-image-16409\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-14.png 850w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-14-300x19.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-14-768x49.png 768w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/figure>\n\n\n\n<p><strong>Add a new user:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Replace test_user with your preferred user name<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE USER 'test_user'@'%' IDENTIFIED WITH mysql_native_password BY 'password';<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"893\" height=\"44\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-13.png\" alt=\"\" class=\"wp-image-16408\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-13.png 893w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-13-300x15.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-13-768x38.png 768w\" sizes=\"auto, (max-width: 893px) 100vw, 893px\" \/><\/figure>\n\n\n\n<p><strong>Note:<br><\/strong>When running the command you may get this error ERROR 1819 (HY000): Your password does not satisfy the current policy requirements<br>In that case, just ensure that your password has a mixture of special characters, numbers, uppercase and lowercase characters, then re-run it.<\/p>\n\n\n\n<p><strong>Grant the user privileges on the new database:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GRANT ALL ON test_db.* TO 'test_user'@'%';<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"552\" height=\"102\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-12.png\" alt=\"\" class=\"wp-image-16406\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-12.png 552w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-12-300x55.png 300w\" sizes=\"auto, (max-width: 552px) 100vw, 552px\" \/><\/figure>\n\n\n\n<p><strong>Exit MySQL:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exit<\/code><\/pre>\n\n\n\n<p>To confirm that the new user has the correct permissions, log into the MySQL console again using the custom user credentials. <\/p>\n\n\n\n<p>Be sure to include the <code>-p<\/code> flag in the command, as it will prompt for the password set when creating the &#8220;test_user&#8221; user.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql -u test_user -p<\/code><\/pre>\n\n\n\n<p>Enter your password and confirm to access test_DB.<\/p>\n\n\n\n<p>You can then check the databases available;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW DATABASES;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"433\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-15-1024x433.png\" alt=\"\" class=\"wp-image-16412\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-15-1024x433.png 1024w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-15-300x127.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-15-768x325.png 768w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-15.png 1093w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Let\u2019s now create a test table named \u201c<strong>tech_list<\/strong>.\u201d Execute the following statement from the MySQL console:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE test_db.tech_list (tech_id INT AUTO_INCREMENT,technology VARCHAR(255),PRIMARY KEY(tech_id));<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"187\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-16-1024x187.png\" alt=\"\" class=\"wp-image-16413\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-16-1024x187.png 1024w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-16-300x55.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-16-768x140.png 768w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-16.png 1081w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>You may now insert values into the table by running multiple inatances of the command below<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO test_db.tech_list (technology) VALUES (\"IOT\");\nINSERT INTO test_db.tech_list (technology) VALUES (\"Cloud\");\nINSERT INTO test_db.tech_list (technology) VALUES (\"DevOPs\");\nINSERT INTO test_db.tech_list (technology) VALUES (\"ML\");\nINSERT INTO test_db.tech_list (technology) VALUES (\"Networking\");<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"258\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-17-1024x258.png\" alt=\"\" class=\"wp-image-16414\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-17-1024x258.png 1024w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-17-300x76.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-17-768x194.png 768w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-17.png 1213w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>You could  check the entries using the command below;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM test_db.tech_list;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"1009\" height=\"256\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-18.png\" alt=\"\" class=\"wp-image-16419\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-18.png 1009w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-18-300x76.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-18-768x195.png 768w\" sizes=\"auto, (max-width: 1009px) 100vw, 1009px\" \/><\/figure>\n\n\n\n<p>You may exit the MySQL console using the <strong>exit<\/strong> command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Test MySQL Connection<\/h2>\n\n\n\n<p>To test, create a PHP script to connect to MySQL:<\/p>\n\n\n\n<p>Open a file called <strong>testlist.php<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vi \/var\/www\/your_domain\/testlist.php<\/code><\/pre>\n\n\n\n<p>Add PHP code to connect and query. In the code below, remember to replace the dummy database details with your own.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$conn = new mysqli(\"localhost\", \"test_user\", \"password\", \"test_db\");\n\nif ($conn-&gt;connect_error) {\n    die(\"Connection failed: \" . $conn-&gt;connect_error);\n}\n\n$sql = \"SELECT * FROM tech_list\";\n$result = $conn-&gt;query($sql);\n\nif ($result-&gt;num_rows &gt; 0) {\n    while($row = $result-&gt;fetch_assoc()) {\n        echo $row&#91;\"tech_id\"] . \": Technology: \" . $row&#91;\"technology\"] . \"&lt;br&gt;\";\n    }\n} else {\n    echo \"0 results\";\n}\n\n$conn-&gt;close();\n?&gt;\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"965\" height=\"373\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-21.png\" alt=\"\" class=\"wp-image-16422\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-21.png 965w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-21-300x116.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-21-768x297.png 768w\" sizes=\"auto, (max-width: 965px) 100vw, 965px\" \/><\/figure>\n\n\n\n<p>Access this script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http:&#47;&#47;your_domain\/testlist.php<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"1008\" height=\"366\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-22.png\" alt=\"\" class=\"wp-image-16423\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-22.png 1008w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-22-300x109.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image-22-768x279.png 768w\" sizes=\"auto, (max-width: 1008px) 100vw, 1008px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Author&#8217;s final word<\/h2>\n\n\n\n<p>Congratulations! You\u2019ve successfully set up a LEMP stack on Ubuntu 20.04, creating a powerful foundation to host PHP-based applications. This configuration, combining Nginx and MySQL, is optimized for speed and security in dynamic web hosting environments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The LEMP stack (Linux, Nginx, MySQL, PHP) is a collection of software used to serve dynamic web pages and PHP applications. It consists of a Linux operating system, the Nginx (pronounced &#8220;Engine-X&#8221;) web server, a MySQL database for data storage, and PHP for handling dynamic content. This guide will walk you through setting up a [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_eb_attr":"","_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"doc_category":[1820,1824,1879,2128],"doc_tag":[],"class_list":["post-16425","docs","type-docs","status-publish","hentry","doc_category-cloud-servers-in-kenya","doc_category-dedicated-servers","doc_category-servers","doc_category-vps-servers"],"year_month":"2026-07","word_count":1318,"total_views":0,"reactions":{"happy":0,"normal":0,"sad":0},"author_info":{"name":"Eugene","author_nicename":"eugene","author_url":"https:\/\/truehost.com\/support\/author\/eugene\/"},"doc_category_info":[{"term_name":"Cloud servers in Kenya","term_url":"https:\/\/truehost.com\/support\/docs-category\/cloud-servers-in-kenya\/"},{"term_name":"dedicated servers","term_url":"https:\/\/truehost.com\/support\/docs-category\/dedicated-servers\/"},{"term_name":"Servers","term_url":"https:\/\/truehost.com\/support\/docs-category\/servers\/"},{"term_name":"VPS-Servers","term_url":"https:\/\/truehost.com\/support\/docs-category\/vps-servers\/"}],"doc_tag_info":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Setting Up a LEMP Stack (Linux, Nginx, MySQL, PHP) on Almalinux -<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/truehost.com\/support\/knowledge-base\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Setting Up a LEMP Stack (Linux, Nginx, MySQL, PHP) on Almalinux -\" \/>\n<meta property=\"og:description\" content=\"The LEMP stack (Linux, Nginx, MySQL, PHP) is a collection of software used to serve dynamic web pages and PHP applications. It consists of a Linux operating system, the Nginx (pronounced &#8220;Engine-X&#8221;) web server, a MySQL database for data storage, and PHP for handling dynamic content. This guide will walk you through setting up a [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/truehost.com\/support\/knowledge-base\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T20:40:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\\\/\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\\\/\",\"name\":\"Setting Up a LEMP Stack (Linux, Nginx, MySQL, PHP) on Almalinux -\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/truehost.com\\\/support\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/image.png\",\"datePublished\":\"2024-11-01T09:56:49+00:00\",\"dateModified\":\"2024-11-01T20:40:50+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\\\/#primaryimage\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/image.png\",\"contentUrl\":\"https:\\\/\\\/truehost.com\\\/support\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/image.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/truehost.com\\\/support\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Setting Up a LEMP Stack (Linux, Nginx, MySQL, PHP) on Almalinux\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#website\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/\",\"name\":\"\",\"description\":\"Help In a Click\",\"publisher\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/truehost.com\\\/support\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#organization\",\"name\":\"Truehost Kenya\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/cropped-image_2026-04-16_174808866.png\",\"contentUrl\":\"https:\\\/\\\/truehost.com\\\/support\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/cropped-image_2026-04-16_174808866.png\",\"width\":240,\"height\":48,\"caption\":\"Truehost Kenya\"},\"image\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#\\\/schema\\\/logo\\\/image\\\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Setting Up a LEMP Stack (Linux, Nginx, MySQL, PHP) on Almalinux -","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/truehost.com\/support\/knowledge-base\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\/","og_locale":"en_US","og_type":"article","og_title":"Setting Up a LEMP Stack (Linux, Nginx, MySQL, PHP) on Almalinux -","og_description":"The LEMP stack (Linux, Nginx, MySQL, PHP) is a collection of software used to serve dynamic web pages and PHP applications. It consists of a Linux operating system, the Nginx (pronounced &#8220;Engine-X&#8221;) web server, a MySQL database for data storage, and PHP for handling dynamic content. This guide will walk you through setting up a [&hellip;]","og_url":"https:\/\/truehost.com\/support\/knowledge-base\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\/","article_modified_time":"2024-11-01T20:40:50+00:00","og_image":[{"url":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/truehost.com\/support\/knowledge-base\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\/","url":"https:\/\/truehost.com\/support\/knowledge-base\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\/","name":"Setting Up a LEMP Stack (Linux, Nginx, MySQL, PHP) on Almalinux -","isPartOf":{"@id":"https:\/\/truehost.com\/support\/#website"},"primaryImageOfPage":{"@id":"https:\/\/truehost.com\/support\/knowledge-base\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\/#primaryimage"},"image":{"@id":"https:\/\/truehost.com\/support\/knowledge-base\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\/#primaryimage"},"thumbnailUrl":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image.png","datePublished":"2024-11-01T09:56:49+00:00","dateModified":"2024-11-01T20:40:50+00:00","breadcrumb":{"@id":"https:\/\/truehost.com\/support\/knowledge-base\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/truehost.com\/support\/knowledge-base\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/truehost.com\/support\/knowledge-base\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\/#primaryimage","url":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image.png","contentUrl":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/11\/image.png"},{"@type":"BreadcrumbList","@id":"https:\/\/truehost.com\/support\/knowledge-base\/setting-up-a-lemp-stack-linux-nginx-mysql-php-on-almalinux\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/truehost.com\/support\/"},{"@type":"ListItem","position":2,"name":"Setting Up a LEMP Stack (Linux, Nginx, MySQL, PHP) on Almalinux"}]},{"@type":"WebSite","@id":"https:\/\/truehost.com\/support\/#website","url":"https:\/\/truehost.com\/support\/","name":"","description":"Help In a Click","publisher":{"@id":"https:\/\/truehost.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/truehost.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/truehost.com\/support\/#organization","name":"Truehost Kenya","url":"https:\/\/truehost.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/truehost.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2026\/04\/cropped-image_2026-04-16_174808866.png","contentUrl":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2026\/04\/cropped-image_2026-04-16_174808866.png","width":240,"height":48,"caption":"Truehost Kenya"},"image":{"@id":"https:\/\/truehost.com\/support\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs\/16425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/comments?post=16425"}],"version-history":[{"count":10,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs\/16425\/revisions"}],"predecessor-version":[{"id":16485,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs\/16425\/revisions\/16485"}],"wp:attachment":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/media?parent=16425"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/doc_category?post=16425"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/doc_tag?post=16425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}