Setting Up a Linux Server with Ubuntu, Nginx, PHP and MySQL

In this guide, we’ll set up a new Linux server running Ubuntu, configured with Nginx, PHP, and MySQL. This stack often called the LEMP stack (Linux, Nginx, MySQL, PHP), is a popular choice for deploying dynamic websites and applications.

Initial Server Setup

1. Access Your Server

First, access your server via SSH. Replace your_server_ip with your server’s IP address.

ssh root@your\_server\_ip

2. Create a New User

For security reasons, it’s best not to use the root account for all tasks. Create a new user:

adduser username

Replace username with your preferred user name. Then, grant administrative privileges to this user:

usermod -aG sudo username

3. Setup SSH for the New User

To use SSH for the new user, create an SSH directory for them and copy the root’s authorized_keys file to their directory:

rsync --archive --chown=username:username ~/.ssh /home/username

Installing Nginx

1. Install Nginx

Update your package lists and install Nginx:

sudo apt update
sudo apt install nginx

2. Adjust the Firewall

Allow Nginx through the firewall:

sudo ufw allow 'Nginx Full'

3. Check the Web Server

Verify Nginx is running:

systemctl status nginx

You should see an active (running) status.

Installing MySQL

1. Install MySQL

Install MySQL server:

sudo apt install mysql-server

2. Secure MySQL

Run the mysql_secure_installation script to address several security concerns in a default MySQL installation:

sudo mysql\_secure\_installation

Follow the prompts to configure your MySQL installation securely.

Configuring MySQL with User Credentials

1. Log into MySQL

First, log into the MySQL root administrative account:

sudo mysql -u root -p

You’ll be prompted to enter the root password you set during installation.

2. Create a New Database

Create a new database that your website or application will use:

CREATE DATABASE exampledb DEFAULT CHARACTER SET utf8 COLLATE utf8\_unicode\_ci;

Replace exampledb with the name of your database.

3. Creating a New User

Create a new user and grant them full privileges to your database:

CREATE USER 'exampleuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON exampledb.\* TO 'exampleuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Managing User Permissions for Multiple Users

If multiple users need access to different databases, you can create individual users for them and grant specific privileges as needed. I recommend creating specific users for every database you create. Never reuse database credentials. Here’s how:

CREATE USER 'userone'@'localhost' IDENTIFIED BY 'password';
CREATE DATABASE userone\_db;
GRANT ALL PRIVILEGES ON userone\_db.\* TO 'userone'@'localhost';
FLUSH PRIVILEGES;

Repeat this process for each artist, substituting userone, userone_db, and password with the respective usernames, database names, and strong passwords.

Installing PHP

1. Install PHP

Install PHP along with php-fpm (FastCGI Process Manager) and php-mysql to allow PHP to communicate with MySQL:

sudo apt install php-fpm php-mysql

Configuring Nginx to Use PHP Processing

1. Configure Nginx Server Block

Edit the default Nginx server block configuration:

sudo nano /etc/nginx/sites-available/default

Adjust the file to match the following, which configures Nginx to use PHP processing:

server {
    listen 80;
    server\_name your\_domain\_or\_IP;

    root /var/www/html;
    index index.php index.html index.htm;

    location / {
        try\_files $uri $uri/ =404;
    }

    location ~ \\.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi\_pass unix:/var/run/php/php8.3-fpm.sock;
        fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;
        include fastcgi\_params;
    }
}

Replace php8.3-fpm.sock with the version of PHP installed on your server. Adjust your_domain_or_IP accordingly.

2. Test Nginx Configuration

After making changes to the Nginx configuration, test to ensure there are no syntax errors:

sudo nginx -t

3. Restart Nginx

If the test is successful, restart Nginx to apply the changes:

sudo systemctl restart nginx

Supporting Multiple Websites with Nginx

Nginx makes it easy to host multiple websites on a single server using the sites-available and sites-enabled directories.

1. Create a New Configuration File

For each website, create a new configuration file in /etc/nginx/sites-available/:

sudo nano /etc/nginx/sites-available/example.com

Add a server block to this file. Here’s an example configuration:

server {
    listen 80;
    server\_name example.com www.example.com;

    root /var/www/example.com/html;
    index index.php index.html index.htm;

    location / {
        try\_files $uri $uri/ =404;
    }

    location ~ \\.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi\_pass unix:/var/run/php/php8.3-fpm.sock;
        fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;
        include fastcgi\_params;
    }
}

2. Enable the Website

Create a symbolic link to enable the site:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

3. Test and Reload Nginx

Always test your configuration for syntax errors:

sudo nginx -t

If the test passes, reload Nginx to apply the changes:

sudo systemctl reload nginx

Repeat these steps for each additional website you wish to host.

Testing PHP with Nginx

Create a PHP file to test PHP processing:

sudo nano /var/www/html/info.php

Add the following PHP code:

Visit http://your_server_ip/info.php in a web browser. You should see a page displaying information about your server’s PHP configuration.

Conclusion

You now have a LEMP stack server running on Ubuntu. This server is configured to serve dynamic content through Nginx, with PHP processing and a MySQL database. This setup is a solid foundation for hosting a variety of web applications. Remember to remove the info.php file when you’re done testing, as it can reveal sensitive information about your server.

sudo rm /var/www/html/info.php