Setting up Nginx Web Server on Ubuntu for Single Laravel Application
INTRODUCTION
Hello and welcome! In this article we’ll learn how to set up the nginx web server on Linux OS. We’ll be using ubuntu to carry on with the process.
Nginx is an open source software that is mostly used as a web server. Basically we configure the nginx server to host different applications. We’ll be seeing how to do that shortly.
PREREQUISITES
- We have an ubuntu instance on any of the cloud platform(AWS, GCP, DigitalOcean, etc)
- We have our laravel application up and running on Ubuntu. To learn more about setting up Laravel application on ubuntu, refer the following link — Setting up Laravel on Ubuntu.
To set up nginx server there are many processes that can be set up in the configuration file. We will se the necessary commands that are required, additionally we will see some of the optional commands that can be put in configuration file.
STEPS
It’s better to complete the below steps as “root” user, to checkout as a root user, run the command -
sudo -i
Step 1: Installing Nginx and configuring firewall
First, we will install Nginx on our server, to do that run the following command —
apt-get install nginx
To check if nginx is installed successfully, run the command —
systemctl status nginx
We have nginx up and running.
Next, we have to set up firewall options for nginx. To see the available options, run the command —
ufw app list
We should get the above output.
Since, we don’t have an SSL certificate yet, we will open port 80 to access our server. To do that we run the command —
ufw allow ‘Nginx HTTP’
We can also confirm the same by running —
ufw status
If we get error as “status:inactive”, first enable the firewall by running-
ufw enable
and check the status again
To check if our server is open on port 80, we can go to a browser and search for -
http://<our_ubuntu_instance_ip_address>
Note: We will need a reserved IP address for Ubuntu instance to set it on Nginx
Step 2: Setting up php-fpm
We will use php-fpm for processing our php files. We need to put the php version in the command while installing fpm, to check the version type -
php -v
In our case it’s 8.2 so while installing fpm, we run the following command
apt-get install php8.2-fpm
Note: Make sure to add the extension in “php ini” file in the location “/etc/php/8.2/fpm” if you’re using mongodb
Step 3: Setting up configuration file for Nginx
Go to the nginx directory and list files and folders -
cd /etc/nginx/; ls
To read the configuration file, run-
cat nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
This is the original configuration file of nginx.
Now we will do the necessary changes in the file as well as some of the additional commands that are good to be executed.
Necessary changes—
Inside the “http” code block we will add a “server” code block which will contain the information regarding our Laravel application.
http{
.
.
server {
listen 80;
root /path/to/laravel/public/folder;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
}
After adding the above code, make sure you change the path to the public folder of your laravel application.
Optional changes —
Inside the “http” code block add the following -
http{
client_max_body_size 1G;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
}
Save and exit the file, and check the configuration is successful or not by running —
nginx -T
Step 4: Changing permissions
In order to give access to out Laravel Application we need to change the permissions for the user-
chown -R www-data:www-data /path/to/your/laravel/root/directory
Now, we go the root directory of our laravel application and run the following command-
chmod -R 775 ./storage
chmod -R 775 ./bootstrap/cache/
Step 5: Restarting the services
After the successful configuration of nginx server, we need to restart all services-
systemctl restart nginx.service
systemctl restart php8.2-fpm.service
Step 6: Allow ssh connection to nginx
Lastly, we need to allow ssh connection in the firewall rules in order to access the ubuntu instance through ssh
ufw allow ‘OpenSSH’
CONCLUSION
These are the few necessary and optional steps that are required for setting up single Laravel application using Nginx on Ubuntu instance. By following these steps, we can establish a robust and secure web server environment, leveraging the power and flexibility of Nginx and Ubuntu to deliver efficient and reliable web services.
Feel free to leave a comment :)