Laravel is a powerful PHP framework designed to make web development a smooth and enjoyable process. Coupled with Nginx, a high-performance web server, you can create robust and scalable web applications. This detailed guide will walk you through the steps to install Laravel PHP Framework with Nginx on Ubuntu 22.04 using root access.
Step 1: Initiating Instance Creation
- To create a new instance, click on the blue “Create” button located in the top-right corner of the page.
- Selecting Instance Type
- You’ll be presented with options to create different types of resources. Click on “Add New Instance” to begin setting up a new virtual machine.
Step 2: Choosing a Location
- On the “Create New Instance” page, you’ll see a list of available locations. Select your preferred data center location. In this example, “Amsterdam, Netherlands” is highlighted.
Step 3: Selecting a Plan
- Choose a plan that fits your needs. The screenshot shows various options ranging from $3.5 to $22 per month, with different specifications for CPU, memory, storage, and bandwidth.
- Choosing an Operating System
- Scroll down to the “Choose an image” section. You can select from various distributions such as AlmaLinux, CentOS, Debian, Fedora, FreeBSD, RockyLinux, Ubuntu, Windows, and Windows Server. In this case, Ubuntu 22.04 is selected.
Step 4: Authentication Method
Choose between SSH keys (recommended for better security) or a password for root access. The password option is selected in the image.
To complete the process, you would click the “Create Instance” button at the bottom of the page, which is not visible in the provided screenshots.
Step 5: Connecting to Your Instance
- Retrieve SSH Credentials:
- Note the IP address of your newly created instance from the Shape.host dashboard.
- Connect via SSH:
- Open a terminal on your local machine.
- Use the following command to connect to your instance:
ssh root@your_instance_ip
- Replace
your_instance_ip
with the actual IP address of your instance.
Before we begin, ensure you have:
- A fresh installation of Ubuntu 22.04.
- Root access to the server.
Step 6: Update Your Server
First, update your package lists to make sure you have the latest versions of your installed packages.
apt update && apt upgrade -y
Step 7: Install Nginx
Install Nginx, which will serve as our web server.
apt install nginx -y
Once installed, start and enable Nginx to run on boot.
systemctl start nginx
systemctl enable nginx
You can check the status to ensure it’s running:
systemctl status nginx
Step 8: Install PHP and Required Extensions
Laravel requires PHP and several PHP extensions. Install them with the following command:
apt install php-fpm php-cli php-mbstring php-xml php-bcmath php-mysql php-json php-curl php-zip php-gd php-pear -y
Step 9: Install Composer
Composer is a dependency manager for PHP, which you will need to install Laravel. Download and install Composer with the following commands:
cd ~
curl -sS https://getcomposer.org/installer -o composer-setup.php
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Verify the installation:
composer --version
Step 10: Install Laravel
Navigate to the web root directory and use Composer to create a new Laravel project.
cd /var/www
composer create-project --prefer-dist laravel/laravel my_laravel_app
Set the appropriate permissions for the Laravel directory:
chown -R www-data:www-data /var/www/my_laravel_app
chmod -R 755 /var/www/my_laravel_app
Step 11: Configure Nginx
Create a new Nginx server block for your Laravel application. Open a new configuration file in Nginx:
nano /etc/nginx/sites-available/my_laravel_app
Add the following configuration to the file:
server {
listen 80;
server_name your_domain_or_IP;
root /var/www/my_laravel_app/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Enable the configuration by creating a symbolic link to sites-enabled
directory:
ln -s /etc/nginx/sites-available/my_laravel_app /etc/nginx/sites-enabled/
Test the configuration to make sure there are no syntax errors:
nginx -t
If the test is successful, reload Nginx to apply the changes:
systemctl reload nginx
Step 12: Adjust PHP Settings
Open the PHP-FPM configuration file to adjust PHP settings for Laravel:
nano /etc/php/8.1/fpm/php.ini
Ensure the following settings are configured appropriately (you can use Ctrl+W
in nano to search for these directives):
file_uploads = On
allow_url_fopen = On
short_open_tag = Off
memory_limit = 256M
upload_max_filesize = 100M
post_max_size = 100M
max_execution_time = 300
Restart PHP-FPM to apply these changes:
systemctl restart php8.1-fpm
Step 13: Configure Laravel Environment
Navigate to your Laravel project directory and copy the .env.example
file to .env
:
cd /var/www/my_laravel_app
cp .env.example .env
Generate the application key:
php artisan key:generate
Step 14: Set Up Database (Optional)
If your application requires a database, you can set up MySQL or any other database supported by Laravel. Here’s a quick example using MySQL:
Install MySQL:
apt install mysql-server -y
Secure the MySQL installation:
mysql_secure_installation
Log in to MySQL and create a database and user for Laravel:
mysql -u root -p
Inside the MySQL shell, run the following commands:
CREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Update your .env
file to include your database credentials:
nano .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=password
Run the migrations to set up your database schema:
php artisan migrate
Accessing Your Application
At this point, you should be able to access your Laravel application by navigating to your server’s IP address or domain in your web browser.
If you’re looking for reliable and scalable hosting solutions for your Laravel applications, consider using Shape.host’s Linux SSD VPS services. They offer high-performance, fully managed cloud servers that ensure your applications run smoothly and efficiently. Check out Shape.host for more information on their offerings and how they can support your development needs.