Setting Up a LEMP Stack on Ubuntu
Introduction to LEMP Stack
LEMP is a popular web development stack that consists of the following components:
- Linux: The operating system.
- Nginx: The web server.
- MySQL: The database management system.
- PHP: The server-side scripting language.
This guide focuses on setting up a LEMP stack on Ubuntu.
1. Installing Nginx
Nginx is a high-performance web server that will serve as the backbone of our LEMP stack. Install Nginx using the following command:
1user@machine:~$ sudo apt install nginx
After installation, ensure that the Nginx service is started and enabled so it will start on boot:
1user@machine:~$ sudo systemctl start nginx
2user@machine:~$ sudo systemctl enable nginx
Create directory to store site files, in this case for example.com
:
1user@machine:~$ sudo mkdir /var/www/example.com
2. Installing MySQL
MySQL is a powerful relational database management system. Install MySQL server using the following command:
1user@machine:~$ sudo apt install mysql-server
Once installed, run the MySQL security script to secure your installation. Follow the prompts to set a root password and configure other security options:
1user@machine:~$ sudo mysql_secure_installation
After installation, ensure that mysql service is started and enabled:
1user@machine:~$ sudo systemctl start mysql
2user@machine:~$ sudo systemctl enable mysql
3. Installing PHP
PHP is a popular server-side scripting language used for dynamic web content. Install PHP along with necessary extensions:
1user@machine:~$ sudo apt install php-fpm php-mysql
4. Configuring Nginx to Use PHP
Next, configure Nginx to process PHP files. Create a new server block configuration file for your website:
1user@machine:~$ sudo nano /etc/nginx/sites-available/example.com
Replace example.com
with your domain name. Paste the following configuration into the file:
1server {
2 listen 80;
3 server_name example.com www.example.com;
4 root /var/www/example.com;
5 index index.php index.html index.htm;
6
7 location / {
8 try_files $uri $uri/ =404;
9 }
10
11 location ~ \.php$ {
12 include snippets/fastcgi-php.conf;
13 fastcgi_pass unix:/var/run/php/php-fpm.sock;
14 }
15
16 location ~ /\.ht {
17 deny all;
18 }
19}
Save and close the file. Then, create a symbolic link to enable the new site configuration and unlink the default site to disable it:
1user@machine:~$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
2user@machine:~$ sudo unlink /etc/nginx/sites-enabled/default
5. Create an Example Index
Create an index.php
which will display your PHP details. This is useful as it will give nginx something to load as well as making sure that PHP is being processed:
1user@machine:~$ sudo nano /var/www/example.com/index.php
1<?php
2phpinfo();
3?>
6. Testing Configuration and Restarting Nginx
Test the Nginx configuration for syntax errors:
1user@machine:~$ sudo nginx -t
If no errors are reported, restart Nginx to apply the changes:
1user@machine:~$ sudo systemctl restart nginx
Depending on your setup you should now be able to access http://example.com
, http://127.0.0.1
, or http://server_ip_address
in your web browser. If everything worked correctly you should see the PHP information page.
Conclusion
You have successfully set up a LEMP stack on your Ubuntu server. You can now deploy and host your web applications using Nginx, MySQL, and PHP, taking advantage of the robust and efficient LEMP stack for web development.