How to launch Laravel App on digital ocean?

Launching a Laravel App on DigitalOcean: A Step-by-Step Guide

Step 1: Setting Up Your DigitalOcean Account

Before you can launch your Laravel app on DigitalOcean, you need to set up an account. Here’s how:

  • Go to the DigitalOcean website (www.digitalocean.com) and sign up for an account.
  • Fill out the registration form with your email address, password, and other details.
  • Verify your email address by clicking on the link sent by DigitalOcean.

Step 2: Creating a New Droplet

Once you’ve set up your account, you can create a new droplet. Here’s how:

  • Log in to your DigitalOcean account and click on the "Droplets" tab.
  • Click on the "Create Droplet" button.
  • Choose the operating system (e.g. Ubuntu, CentOS) and the size of your droplet (e.g. 1 vCPU, 2 vCPU).
  • Choose the RAM and storage options (e.g. 1GB RAM, 20GB storage).
  • Click on the "Create Droplet" button.

Step 3: Installing PHP and Composer

To run a Laravel app, you need to install PHP and Composer. Here’s how:

  • Log in to your DigitalOcean account and click on the "Droplets" tab.
  • Click on the droplet you created in Step 2.
  • Click on the "Advanced" tab.
  • Scroll down to the "Software" section and click on the "Install PHP" button.
  • Follow the installation instructions to install PHP 7.4.
  • Click on the "Install Composer" button.
  • Follow the installation instructions to install Composer.

Step 4: Setting Up Your Laravel Project

Now that you have PHP and Composer installed, you can set up your Laravel project. Here’s how:

  • Create a new directory for your project and navigate to it in your terminal.
  • Initialize a new Laravel project using the following command:
    composer create-project --prefer-dist laravel/laravel project-name

    Replace "project-name" with the name of your project.

Step 5: Configuring Your Database

To connect to your database, you need to configure it in your Laravel project. Here’s how:

  • Open the .env file in your project directory.
  • Add the following lines to the file:
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=your-database-name
    DB_USERNAME=your-database-username
    DB_PASSWORD=your-database-password

    Replace "your-database-name", "your-database-username", and "your-database-password" with the actual values for your database.

Step 6: Creating a New User

To create a new user for your Laravel app, you need to create a new user account. Here’s how:

  • Open the database.php file in your project directory.
  • Add the following lines to the file:
    'users' => [
    'create' => [
    'table' => 'users',
    'fillable' => ['name', 'email', 'password'],
    'rules' => [
    'name' => 'required|string',
    'email' => 'required|email|unique:users',
    'password' => 'required|string',
    ],
    ],
    ],

    This will create a new user table with the following columns: id, name, email, and password.

Step 7: Creating a New Route

To create a new route for your Laravel app, you need to create a new route file. Here’s how:

  • Create a new file called routes/web.php in your project directory.
  • Add the following lines to the file:

    Route::get('/', function () {
    return view('welcome');
    });

Route::get(‘/users’, ‘UserController@index’);

This will create a new route for the root URL (`/`) and a new route for the `/users` URL.

**Step 8: Creating a New Controller**

To create a new controller for your Laravel app, you need to create a new controller file. Here's how:

* Create a new file called `app/Http/Controllers/UserController.php` in your project directory.
* Add the following lines to the file:

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppModelsUser;

class UserController extends Controller
{
public function index()
{
$users = User::all();
return view(‘users’, compact(‘users’));
}

public function create()
{
return view('create-user');
}

public function store(Request $request)
{
$user = new User();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = bcrypt($request->input('password'));
$user->save();
return redirect()->route('users.index');
}

public function show($id)
{
$user = User::find($id);
return view('user', compact('user'));
}

public function edit($id)
{
$user = User::find($id);
return view('edit-user', compact('user'));
}

public function update(Request $request, $id)
{
$user = User::find($id);
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = bcrypt($request->input('password'));
$user->save();
return redirect()->route('users.index');
}

public function destroy($id)
{
$user = User::find($id);
$user->delete();
return redirect()->route('users.index');
}

}

This will create a new controller for the user route.

**Step 9: Creating a New Model**

To create a new model for your Laravel app, you need to create a new model file. Here's how:

* Create a new file called `app/Models/User.php` in your project directory.
* Add the following lines to the file:

namespace AppModels;

use IlluminateDatabaseEloquentModel;

class User extends Model
{
protected $fillable = [
‘name’,
’email’,
‘password’,
];
}

This will create a new model for the user table.

**Step 10: Creating a New Migration**

To create a new migration for your Laravel app, you need to create a new migration file. Here's how:

* Create a new file called `database/migrations/2023_11_12_000000_create_users_table.php` in your project directory.
* Add the following lines to the file:

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;

class CreateUsersTable extends Migration
{
public function up()
{
Schema::create(‘users’, function (Blueprint $table) {
$table->id();
$table->string(‘name’);
$table->string(’email’)->unique();
$table->string(‘password’);
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('users');
}

}

This will create a new migration for the users table.

**Step 11: Running Migrations**

To run the migrations, you need to run the following command:

php artisan migrate

This will create the users table in your database.

**Step 12: Testing Your App**

To test your app, you need to run the following command:

php artisan serve


This will start the development server and open a web browser at `http://localhost:8000`.

**Conclusion**

Launching a Laravel app on DigitalOcean is a straightforward process that involves setting up a new droplet, installing PHP and Composer, setting up your database, creating a new user, and creating a new route. You can then test your app by running the `php artisan serve` command and opening a web browser at `http://localhost:8000`. With these steps, you can create a fully functional Laravel app on DigitalOcean.

**Important Notes**

* Make sure to replace the placeholders in the `database.php` file with your actual database credentials.
* Make sure to replace the placeholders in the `routes/web.php` file with your actual route names.
* Make sure to replace the placeholders in the `app/Http/Controllers/UserController.php` file with your actual controller names.
* Make sure to replace the placeholders in the `app/Models/User.php` file with your actual model names.
* Make sure to replace the placeholders in the `database/migrations/2023_11_12_000000_create_users_table.php` file with your actual migration names.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top