When deploying a web server on Ubuntu, you often need to configure Nginx as the web server, PHP for dynamic content processing, MySQL for database management, and SSL certificates for security. Additionally, tools like Composer for PHP dependency management and Adminer for database administration can significantly improve your development process. This article provides a step-by-step guide on setting up an Ubuntu server with these technologies.
Step 1: Installing the Nginx Web Server
Nginx is a popular web server known for its performance and low resource consumption. The first step in setting up your Ubuntu server is to install Nginx.
- Update the package index to ensure your server has the latest package listings:
sudo apt update
- Install Nginx:
sudo apt install nginx
- After installation, Nginx will automatically start. To verify that it's running, use:
sudo systemctl status nginx
Configure Firewall (Optional)
If your server has UFW (Uncomplicated Firewall) enabled, you need to allow Nginx traffic:
sudo ufw allow 'Nginx Full'
Nginx is now installed and ready for configuration.
Step 2: Installing PHP
PHP is a server-side scripting language used for developing dynamic web applications. In this section, we’ll install PHP 8.1 (or the latest version) along with necessary PHP extensions.
- Install PHP and common extensions:
For PHP 8.1 (latest version):
sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https curl git -y
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php8.1 php8.1-fpm php8.1-mysql php8.1-curl php8.1-mbstring php8.1-xml -y
- Check PHP version:
After installation, verify the PHP version by running:
php -v
- Configure PHP with Nginx:
Open the Nginx configuration file:
sudo nano /etc/nginx/sites-available/default
Find the PHP section and modify it as follows:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
Save and close the file, then reload Nginx to apply the changes:
sudo systemctl reload nginx
Step 3: Installing MySQL
MySQL is a widely-used database management system. Here’s how you can install and secure MySQL.
- Install MySQL along with PHP MySQL extensions:
sudo apt install mysql-client mysql-server php8.1-mysql
- Secure MySQL Installation:
After installation, secure your MySQL server by running the following command:
sudo mysql_secure_installation
Follow the prompts to set up root password security, remove anonymous users, disallow remote root login, and remove the test database.
- Log in to MySQL:
Once secured, log in as the root user:
sudo mysql -u root -p
- Set a Password for MySQL Root User:
After logging in, switch to the MySQL database and update the root password for additional security:
USE mysql;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourStrongPassword';
FLUSH PRIVILEGES;
Replace YourStrongPassword
with a secure password of your choice. Then, exit the MySQL console:
exit;
Step 4: Installing and Securing Nginx with Let’s Encrypt (SSL)
To secure your website with SSL (HTTPS), you can use Let's Encrypt, a free and automated certificate authority.
- Install Certbot and the Nginx plugin:
sudo apt install certbot python3-certbot-nginx
- Obtain and Install SSL Certificate:
Run the Certbot command to automatically obtain and install an SSL certificate for your domain:
sudo certbot --nginx -d your_domain -d www.your_domain
Certbot will prompt you to enter an email address and agree to the terms of service. It will automatically configure Nginx to redirect HTTP traffic to HTTPS.
- Test SSL Renewal:
Certbot renews SSL certificates automatically. To test the renewal process, you can run:
sudo certbot renew --dry-run
Step 5: Installing Adminer (Database Management Interface)
Adminer is a lightweight database management tool that can replace phpMyAdmin. It is easy to install and supports MySQL, PostgreSQL, and other databases.
- Create a directory for Adminer:
sudo mkdir /var/www/adminer
- Download Adminer:
Download the latest Adminer file using wget
:
sudo wget "https://www.adminer.org/latest.php" -O /var/www/adminer/index.php
- Configure Nginx for Adminer:
Create a new Nginx server block for Adminer:
sudo nano /etc/nginx/sites-available/adminer
Add the following configuration:
server {
listen 80;
server_name adminer.your_domain;
root /var/www/adminer;
index index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
}
Enable the site by creating a symbolic link:
sudo ln -s /etc/nginx/sites-available/adminer /etc/nginx/sites-enabled/
- Test and reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
Now, you can access Adminer via http://adminer.your_domain
.
Step 6: Installing Composer (PHP Dependency Manager)
Composer is the most commonly used dependency manager for PHP, making it essential for modern PHP development.
- Install Composer:
First, update your package list and install the required dependencies:
sudo apt update
sudo apt install wget php8.1-zip unzip
Then, download and install Composer:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
HASH="$(wget -q -O - https://composer.github.io/installer.sig)"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
- Check Composer Installation:
Verify that Composer was installed successfully by checking its version:
composer --version
Step 7: Managing Nginx Domains
Adding a New Domain
To host multiple websites, you can add additional domains by creating separate server blocks in Nginx.
- Create a directory for the new domain:
sudo mkdir -p /var/www/your_domain/html
sudo chown -R $USER:$USER /var/www/your_domain/html
sudo chmod -R 755 /var/www/your_domain
- Create a new Nginx server block:
sudo nano /etc/nginx/sites-available/your_domain
Add the following content:
server {
listen 80;
server_name your_domain www.your_domain;
root /var/www/your_domain/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.1-fpm.sock;
}
}
- Enable the site and reload Nginx:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Removing a Domain
To remove a domain from your Nginx server:
- Disable the server block:
sudo rm /etc/nginx/sites-enabled/your_domain
- Remove the configuration and website files:
sudo rm /etc/nginx/sites-available/your_domain
sudo rm -rf /var/www/your_domain
- Reload Nginx:
sudo systemctl reload nginx
Conclusion
By following these steps, you will have a fully functional Ubuntu server with Nginx, PHP, MySQL, SSL encryption, Composer for PHP dependency management, and Adminer for database administration. This setup provides a secure, modern, and robust environment for deploying PHP-based web applications.
Top comments (0)