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

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

sudo apt-get update
Copy after login

1. Install Nginx

sudo apt-get install nginx
Copy after login

After installing Nginx, you need to restart nginx

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

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

mysql -u root -p
Copy after login

If the login is successful, Mysql is installed correctly.

3. Install PHP

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

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

cgi.fix_pathinfo=0
Copy after login

5. Enable php5-mcrypt:

sudo php5enmod mcrypt
Copy after login

6.Restart php5-fpm:

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

sudo mkdir -p /var/www
Copy after login

2. Configure the nginx server

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

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

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

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

server_name server_domain_or_IP;
Copy after login

Modify server_domain_or_IP to your public IP

location modification

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

After modification, it will look 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;
 }
}
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:

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.

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

cd ~
curl -sS getcomposer.org/installer | php
Copy after login

on the server. The above command will install composer.

Composer is used globally:

sudo mv composer.phar /usr/local/bin/composer
Copy after login

Then execute it directly in the /var/www directory

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

scp -r laravel root@your_IP:
Copy after login

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

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:

sudo apt-get install git
Copy after login

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

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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 in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

See all articles