Home Backend Development PHP Tutorial Example tutorial of deploying Laravel on cloud server

Example tutorial of deploying Laravel on cloud server

Jul 02, 2017 am 11:28 AM
laravel php server

This article mainly introduces in detail how to deploy Laravel on the cloud server, which has certain reference value. Interested friends can refer to it

It has been a while since I learned PHP and Laravel, but All the code ran on the local virtual host, so I went to Tencent Cloud to apply for a free cloud host for one month, and wanted to deploy the project to the cloud server.

I have to say that there are a lot of pitfalls here, which makes me, a novice who comes into contact with the server for the first time, confused. After configuring the server, deploying a Laravel project is even more difficult, so I wanted to record the process of deploying the Laravel project.

PS: Linux is really a system that feels better the more you use it. You should install Linux on your desktop computer to type code when you go home.

Environment Introduction

In terms of the choice of operating system, I chose the Linux ubuntu16.04 system and used the LNMP environment, that is, Linux + Nginx + Mysql + PHP environment.

Delete Apache

1

2

3

sudo service apache2 stop

update-rc.d -f apache2 remove

sudo apt-get remove apache2

Copy after login

Use these three commands to delete Apaceh first and then update the package list

1

sudo apt-get update

Copy after login

1. Install Nginx

1

sudo apt-get install nginx

Copy after login

After installing Nginx, you need to restart nginx

1

sudo service nginx start

Copy after login

After execution, enter the public IP assigned to you by the cloud server in the browser, and you can see the welcome to nginx The interface is

2. During the installation of Mysql

1

sudo apt-get install mysql-server mysql-client

Copy after login

, you will be prompted to set the Mysql password, just like the usual password settings, enter it once and confirm it once. After the password is confirmed, the installation will basically take a while. Try

1

mysql -u root -p

Copy after login

If the login is successful, Mysql is installed correctly.

3. Install PHP

1

sudo apt-get install php5-fpm php5-cli php5-mcrypt

Copy after login

Only through php5-fpm, PHP can run normally under Nginx, so install it.

As for php5-mcrypt, some PHP frameworks will depend on this, such as Laravel, so it is also installed.

Off topic, I installed php7 myself during deployment of php5 here. If you want to try it, you can also try it.

4. Configure PHP

1

sudo vim /etc/php5/fpm/php.ini

Copy after login

Open the PHP configuration file, find the cgi.fix_pathinfo option, remove the comment semicolon; in front of it, and then Set its value to 0, as follows

1

cgi.fix_pathinfo=0

Copy after login

5. Enable php5-mcrypt:

1

sudo php5enmod mcrypt

Copy after login

6.Restart php5-fpm:

1

sudo service php5-fpm restart

Copy after login

After setting up the LEMP environment, you must first clarify two important directories

Nginx’s default root folder

##/usr/ share/nginx/html

The directory where Nginx’s server configuration file is located

/etc/nginx/sites-available/

Just remember the above two directories, they are very commonly used, let’s put them out first

The following is a step-by-step deployment of Laravel on the cloud server

1. Create the root directory of the website

1

sudo mkdir -p /var/www

Copy after login

2. Configure the nginx server

1

sudo vim /etc/nginx/sites-available/default

Copy after login

Open the nginx configuration After the file, find the server section, which probably looks like this

1

2

3

4

5

6

7

8

9

10

11

12

13

server {

  listen 80 default_server;

  listen [::]:80 default_server ipv6only=on;

 

  root /usr/share/nginx/html;

  index index.html index.htm;

 

  server_name localhost;

 

  location / {

    try_files $uri $uri/ =404;

  }

}

Copy after login

The lines of root, index, server_name and location need to be slightly modified

root modification

1

root /var/www/laravel/public;

Copy after login

Here is to point the root directory of the nginx server to Laravel's public folder. We will place the subsequent Laravel project code in the /var/www/laravel directory we created before.

index modification

1

index index.php index.html index.htm;

Copy after login

What needs to be noted here is that index.php is ranked first

server_name modification

1

server_name server_domain_or_IP;

Copy after login

Modify server_domain_or_IP to your public IP

location modification

1

2

3

location / {

  try_files $uri $uri/ /index.php?$query_string;

}

Copy after login

After modification, it will look like this:

1

2

3

4

5

6

7

8

9

10

11

12

13

server {

 listen 80 default_server;

 listen [::]:80 default_server ipv6only=on;

 

 root /var/www/laravel/public;

 index index.php index.html index.htm;

 

 server_name server_domain_or_IP;

 

 location / {

   try_files $uri $uri/ /index.php?$query_string;

 }

}

Copy after login

Finally we still need to configure it Nginx, let it execute the PHP file. Also in this file, add the following configuration under location:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

server {

 listen 80 default_server;

 listen [::]:80 default_server ipv6only=on;

 

 root /var/www/laravel/public;

 index index.php index.html index.htm;

 

 server_name server_domain_or_IP;

 

 location / {

  try_files $uri $uri/ /index.php?$query_string;

 }

 

 location ~ \.php$ {

  try_files $uri /index.php =404;

  fastcgi_split_path_info ^(.+\.php)(/.+)$;

  fastcgi_pass unix:/var/run/php5-fpm.sock;

  fastcgi_index index.php;

  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

  include fastcgi_params;

 }

}

Copy after login

Note that the bottom

location ~ \.php$ was added by yourself:

After configuring, restart Nginx to make the above configuration items take effect.

1

sudo service nginx restart

Copy after login

3. Create a Laravel project

After configuring nginx, how to

get the Laravel project code? There are several methods:

(1). Direct composer installation

Install directly through composer. You can execute

1

2

cd ~

curl -sS getcomposer.org/installer | php

Copy after login

on the server. The above command will install composer.

Composer is used globally:

1

sudo mv composer.phar /usr/local/bin/composer

Copy after login

Then execute it directly in the /var/www directory

1

sudo composer create-project laravel/laravel laravel

Copy after login

Because we created the /var/www directory before, you can directly cd /var /www and then execute the above command. Then wait for the installation to complete.

(2). Upload the code directly

Use the following command to upload

1

scp -r laravel root@your_IP:

Copy after login

Then move laravel to the /var/www directory on the server

1

sudo mv laravel/ /var/www

Copy after login

(3). Use Git and Coding platform

I personally prefer to use git to upload code, which can easily update the code and roll back. Once the version When bugs are updated, I can use Git's powerful version management capabilities to fix them. The process is roughly like this:

Local code---->Github---->Cloud server


既然要使用git,那么先在云服务器上安装git:

1

sudo apt-get install git

Copy after login

安装完成就可以使用git了,然后在Github上创建一个私有项目laravel,里面包含所有该Laravel项目所需代码。

一旦本地代码都推送到Coding,然后在/var/www目录下直接使用

1

git clone your-project-git-link

Copy after login

your-project-git-link替换为你Github上的laravel项目地址

5.BINGO

在浏览器输入:server_domain_or_IP

至此,你可以在服务器上随意地用Laravel了,keep coding!

The above is the detailed content of Example tutorial of deploying Laravel on cloud server. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

See all articles