How to Deploy Laravel Project to Live Nginx Server
In this tutorial, we will learn how to deploy a Laravel application to a live Nginx server. It assumes you already have Ngnix installed and configured.
Step 1 Create Directory for App & Upload Files
The first step is to create a directory for your Laravel app inside the /var/www directory. Replace example.com with your domain.
sudo mkdir /var/www/example.com
Now (as none root) take ownership and group permissions for this file.
sudo chown your_username -R /var/www/example.com/
sudo chgrp your_username -R /var/www/example.com/
Now upload your files to the /var/www/example.com
directory using scp (ADD_LINK
).
The SCP command will be something like:
scp -r * your_username@server_ip:/var/www/example.com/
Set File Permissions
cd
into the app root and set the storage
and bootstrap/cache
directories to have these permissions for www-data:
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache
Create an Nginx Config File
The next step is to create an Nginx config file for the app in the /etc/nginx/sites-available
directory.
sudo nano /etc/nginx/sites-available/example.com
Add the following contents, replacing example
:
Note – you might need to comment out the ssl lines if you have not created an SSL Certificate yet.
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
root /var/www/html/quickstart/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
location ~ /.well-known {
allow all;
}
}
Create SymLink to Sites Enabled
Now, create a symbolic link for the new config file in /etc/nginx/sites/enabled
:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Test the nginx configuration
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload Nginx:
sudo systemctl reload nginx
Create MySQL Database, User and Grant Permissions
Begin by logging into the mysql
server, suppling the mysql
root password when required:
mysql -u root -p
Now create a database with the following command, replacing exampledb
with the name of the database to create:
CREATE DATABASE exampledb DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Now create a new MySQL user with the following command, replacing newuser
and user_password
:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'user_password';
Next, grant all privileges for the new user to the new database:
GRANT ALL PRIVILEGES ON database_name.* TO 'database_user'@'localhost';
Add DB Credientials to the .env File
Now, open the .env
file for your app and add the credentials for the database you just created like this:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=exampledb
DB_USERNAME=newuser
DB_PASSWORD=user_password
Conclusion
You will also need to change the Laravel public path if you are using a directory other than public_html
to serve public files.