Introduction
Whenever I set up a web server either using a self-hosted computer or a cloud-hosted solution like Amazon AWS I almost always use the LEMP stack for my setup.
- L - Linux
- E - Nginx
- M - MySQL
- P - PHP
For the Linux distribution, I use Ubuntu which is currently the same OS for my desktop except for servers I use the more stable LTS (Long-term support) edition which is Ubuntu server 22.04 and my desktop will use the latest desktop edition.
Nginx is my choice of web server compared with Apache because overall it's more efficient and scalable.
As for database solutions, MySQL is the default choice as it's almost always paired with PHP development.
This guide includes the setup for the above software plus additional software required to manage PHP websites in web servers such as Composer, NPM, and Git. In most cases when you're developing with PHP you will need these, especially with more modern PHP applications, more in-depth details about each application will be shown below.
Finally, there is also an example setup of a website that will be gone through, this website was built with the Laravel framework so the setup should be easy to follow whereby you can do the same thing when building your own Laravel applications.
Server setup
The first thing to do when setting up a server is to install the operating system. Typically cloud-based solutions already provide images with a few operating systems which get automatically installed during the creation of the cloud server. If you have a self-hosted server the installation would need to be done manually. The official Ubuntu website has an installation guide on installing the server edition.
After installation of the Ubuntu OS, you should be able to log in, you can now begin setting up the server.
The first thing to do is run the update and upgrade commands. The update command will update the repository lists for software packages and the upgrade will check and upgrade them.
sudo apt update -y && sudo apt upgrade -y
Nginx
Ubuntu comes with a repository for Nginx so the install command can be run.
sudo apt install nginx -y
After installation use the service command to check that Nginx is running.
sudo service nginx status
If you go to the IP address of the server you should see the default Nginx page.
PHP-FPM is required to use PHP with Nginx, the setup for that will be covered in the next section.
PHP
PHP 8.3
Add the ondrej repository to get access to the latest PHP version. Press the Enter key to continue after running the below command.
sudo add-apt-repository ppa:ondrej/php
Now run the update command to add the newly added repository.
sudo apt update -y
After the update, you can now install PHP 8.3.
sudo apt install php8.3 -y
Check that PHP has been installed by checking the version.
php -v
PHP Modules
In terms of PHP modules, the default PHP modules required to run Laravel applications will be used, the list of modules is available in their deployment guide.
sudo apt install openssl php8.3-bcmath php8.3-curl php8.3-mbstring php8.3-mysql php8.3-tokenizer php8.3-xml php8.3-zip -y
While deploying websites you may encounter PHP errors that require a PHP module, once you encounter the error then install it as required.
You can view the list of installed modules with the following command.
php -m
PHP-FPM
Another application that is required to use PHP with Nginx is PHP-FPM. Run the install command for PHP-FPM.
sudo apt install php8.3-fpm -y
Check that it's running, it will be used when setting up a virtual host for the test website in the section below.
sudo service php8.3-fpm status
MySQL
The database solution used is MySQL, use this command to install the MySQL server.
sudo apt install mysql-server -y
Verify that the MySQL server is running.
sudo service mysql status
To access MySQL log in using the default root user, there shouldn't be a password set up for the root user so it will be blank.
sudo mysql -u root
Composer
Composer is required for modern PHP applications such as Laravel to install PHP packages that can be used in your PHP websites.
To begin use the curl command to download the setup PHP file, this will then be placed in the tmp folder.
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
Run the setup PHP file with the install directory set to /usr/local/bin.
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
After installation run the composer command to check that it is working.
composer
Node.js / Node package manager
As Javascript has become more advanced so too has its integration with PHP. More modern PHP applications will now require the Node package manager (NPM) to be installed.
NPM comes bundled with Node.js so this section will detail how to install it. Use curl to download the install script for the Node version manager.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Use the following command to load nvm.
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
Ensure that nvm is working by checking the version.
command -v nvm
Use nvm to install Node.js, latest version as of writing this article is 20.11.
nvm install 20.11.0
After installation npm should be installed, use the version command to verify.
npm -v
Git
By default, git should already be pre-installed in Ubuntu. You can verify that git is installed by using the command to show the installation directory of git.
which git
Hosting a website
Now that all the required software has been installed it's time to add a website to host on the web server.
Get PHP code
The devsites project in GitHub is a PHP Laravel project so this would be ideal to set up with the Nginx and PHP-FPM setup.
Change to the /var/www directory which is typically used to store websites in Linux servers.
cd /var/www
Set the permissions to this directory so you can add files and those files are accessible with Nginx, by default the permissions are set to root.
sudo chown -R $USER:www-data /var/www
Add the devsites project to the /var/www directory.
git clone https://github.com/devpushprojects/devsites.git
Also, set the group to www-data to the devsites project so Nginx can access the devsites website.
sudo chgrp -R www-data devsites
Add the devsites project to the /var/www directory.
cd devsites
Use composer to install PHP packages.
composer install
Copy the .env-example file so the environment variables can be set.
cp .env.example .env
Laravel requires an App Key to be in the .env file, use the following command to generate and add it.
php artisan key:generate
Setup virtual host
Now that the website is setup the next task is to create a virtual host so Nginx will direct requests to the PHP website.
Firstly go to the sites-available folder inside the /etc/nginx directory.
cd /etc/nginx/sites-available
Use the nano command to open the text editor to create a file and add text representing the virtual host.
sudo nano devsites
After the nano text editor appears then copy the below code to paste into the open file.
Change the line server_name to the URL you want to use for the test site.
server {
listen 80;
listen [::]:80;
server_name server.devpush.io;
root /var/www/devsites/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
}
location ~ /\.ht {
deny all;
}
}
Now the virtual host file needs to be available in the sites-enabled folder. The following ln -s command will create a symbolic link to the original file in the sites-available folder.
sudo ln -s /etc/nginx/sites-available/devsites /etc/nginx/sites-enabled/devsites
Restart Nginx to apply the new changes.
sudo service nginx restart
Now set the domain you have used for the server_name entry and point the server IP to the domain.
Remove Apache
After restarting Nginx and checking the server IP address the following Apache2 default page showed up, this meant Apache2 was still installed and active.
Use the service command to verify that Apache2 is running.
sudo service apache2 status
Use the remove command to uninstall Apache2.
sudo apt remove apache2
Delete the apache2 folder
sudo rm -R /usr/sbin/apache2
If the page is still showing then it's because the index.html file that the above Apache2 Default Page refers to file exists in /var/www/html/index.html so you can leave it as it is since you will mainly be using domains to access your server rather than the direct IP.
Conclusion
This guide has shown how easy it is to use Linux to set up a web server for your PHP projects. Adding your websites enables them to be available on the internet for anyone to access.
This is a great way to showcase your portfolio projects, add an e-commerce website to support a business or start writing with a personal blog.