search
HomeBackend DevelopmentPHP TutorialHow to deploy Laravel's parsing on a cloud 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 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.

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 laborious, so I wanted to record the process of deploying the Laravel project.

PS: Linux is really a system that becomes more enjoyable 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

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

sudo apt-get update

1. Install Nginx

sudo apt-get install nginx

After installing Nginx, 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 The interface is

2. During the installation of Mysql

sudo apt-get install mysql-server mysql-client

, 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 A piece, it 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;
  }
}

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 the public folder of Laravel. We will put the code of the subsequent Laravel project 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 at the top

Modify server_name

server_name server_domain_or_IP;

Modify server_domain_or_IP to you The public IP

location modification

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

The modification is as follows:

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 need to configure Nginx to let it execute PHP document. 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$ was added by yourself:

After configuring, restart Nginx 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 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:

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

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 it directly in the /var/www directory

git clone your-project-git-link

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

5.BINGO

在浏览器输入:http://server_domain_or_IP

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

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

如何利用php和Laravel实现部署自动化

关于PHP管理依赖工具 Composer 安装与使用

The above is the detailed content of How to deploy Laravel's parsing on a 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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

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 Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)