Home  >  Article  >  Backend Development  >  Deploy Laravel instance method on cloud server

Deploy Laravel instance method on cloud server

小云云
小云云Original
2018-02-10 11:22:531424browse

I have been learning PHP and Laravel for a while, but all the code runs 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. This article mainly introduces how to deploy Laravel on the cloud server. It has certain reference value. Interested friends can refer to it. I hope it can help you.

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, which is the Linux + Nginx + Mysql + PHP environment.

Delete Apache

sudo service apache2 stop
update-rc.d -f apache2 remove
sudo apt-get remove apache2

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

sudo apt-get update

1. Install Nginx

sudo apt-get install nginx

After installing Nginx After that, you need to restart nginx

sudo service nginx start

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

2. Install Mysql

sudo apt-get install mysql-server mysql-client

During the process, 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

mysql -u root -p

If the login is successful, Mysql is installed correctly.

3. Install PHP

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

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

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

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

cgi.fix_pathinfo=0

5. Enable php5-mcrypt:

sudo php5enmod mcrypt

6. Restart php5-fpm:

sudo service php5-fpm restart

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

sudo mkdir -p /var/www

2. Configure the nginx server

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

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

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;
  }
}

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

root modification

root /var/www/laravel/public;

Here is to point the root directory of the nginx server to Laravel's public folder, for subsequent Laravel projects We will put the code in the /var/www/laravel directory we created before

index modification

index index.php index.html index.htm;

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

server_name modification

server_name server_domain_or_IP;

Modify server_domain_or_IP to your public IP

location modification

location / {
  try_files $uri $uri/ /index.php?$query_string;
}

The modification is like this:

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;
 }
}

Finally we You also need to configure Nginx to execute PHP files. Also in this file, add the following configuration under location:

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;
 }
}

Note that the bottom location ~ \.php$ is added by yourself:

Restart Nginx after configuration , to make the above configuration items take effect.

sudo service nginx restart

3. Create a Laravel project

After configuring nginx, how to obtain the Laravel project code? There are several methods:

(1). Direct composer installation

Install directly through composer. You can execute

cd ~
curl -sS https://getcomposer.org/installer | php

on the server. The above command will install composer.

Composer is used globally:

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

Then execute it directly in the /var/www directory

sudo composer create-project laravel/laravel laravel

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

(2). Upload the code directly

Use the following command to upload

scp -r laravel root@your_IP:

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

sudo mv laravel/ /var/www

(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 is updated and bugs occur, I can use Git's powerful version management capabilities to fix the bug. The process is roughly like this:

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

Since you want to use git, first install git on the cloud server :

sudo apt-get install git

After the installation is complete, you can use git, and then create a private project laravel on Github, which contains all the code required for the Laravel project.

Once the local code is pushed to Coding, then use

git clone your-project-git-link

replace your-project-git-link directly with your laravel project address on Github in the /var/www directory

5.BINGO

Enter in the browser: http://server_domain_or_IP

At this point, you can use Laravel on the server as you like, keep coding!

Related recommendations:

LNMP deployment of laravel and xhprof installation

Deploy Laravel project on Alibaba Cloud's ECS

Deploy Laravel project step by step

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

Statement:
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