Table of Contents
下面一步一步在阿里云ECS上部署Laravel
1.创建网站的根目录
2.配置nginx服务器
3.创建Laravel项目
4.最后的最后
5.BINGO
Home Backend Development PHP Tutorial 在阿里云的ECS上部署Laravel项目

在阿里云的ECS上部署Laravel项目

Jun 23, 2016 pm 01:31 PM

在阿里云的ECS上部署Laravel项目

在搭建完LEMP环境之后,首先要明确两个重要目录

Nginx的默认root文件夹

/usr/share/nginx/html
Copy after login

Nginx的服务器配置文件所在目录

/etc/nginx/sites-available/
Copy after login

上面两个目录记住就好,很常用,先摆出来

下面一步一步在阿里云ECS上部署Laravel

1.创建网站的根目录

sudo mkdir -p /var/www
Copy after login

2.配置nginx服务器

sudo vim /etc/nginx/sites-available/default
Copy after login

打开nginx的配置文件之后,找到server这一块,大概是长这个样子的

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

其中root,index ,server_name和location这几行需要稍微修改一下

root修改

root /var/www/laravel/public;
Copy after login

这里就是将nginx服务器的根目录指向Laravel的public文件夹下,后续的Laravel项目的代码我们会放在我们之前创建的/var/www/laravel目录下

index修改

index index.php index.html index.htm;
Copy after login

这里需要注意的是,将index.php排在最前面

server_name修改

server_name server_domain_or_IP;
Copy after login

将server_domain_or_IP修改为你的公网IP

location修改

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

修改完是这样的:

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

最后我们还需要配置一下Nginx,让其执行PHP文件。同样是在这个文件里,在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

注意,这一块是自己加上去的:

 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

配置完之后重启Nginx,使上面的配置项生效。

sudo service nginx restart
Copy after login

3.创建Laravel项目

在配置完nginx后,怎么获取Laravel的项目代码呢?有以下几种方法:

(1).直接composer安装

直接通过composer来安装,你可以在CES上通过执行

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

上面命令会安装composer

composer全局使用:

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

然后在/var/www目录下直接执行

sudo composer create-project laravel/laravel laravel
Copy after login

因为我们之前创建/var/www目录,你可以直接cd /var/www然后执行上面的命令。然后坐等安装完成。

(2).直接上传代码

使用下面命令上传

scp -r laravel root@your_IP:
Copy after login

然后在阿里云的ECS上将laravel移动到/var/www目录下

sudo mv laravel/ /var/www
Copy after login

(3).使用Git和Coding平台

个人比较喜欢使用git来上传代码,可以很方便的更新代码和进行回滚,一旦版本更新出Bug我可以借助Git的强大版本管理能力来修复Bug。流程大概是这样:

本地代码---->Coding---->阿里云ECS
Copy after login

既然要使用git,那么先在ECS上安装git:

sudo apt-get install git
Copy after login

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

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

git clone your-project-git-link
Copy after login

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

4.最后的最后

不管哪种方式安装的代码,/var/www/都是属于root用户的,而访问网站的用户则需要正确的权限和访问限制,我们可以通过下面的命令来实现。

sudo chown -R :www-data /var/www/laravel
Copy after login

根据Laravel的官方文档,/var/www/laravel/app/storage目录需要给网站的用户写权限

sudo chmod -R 775 /var/www/laravel/app/storage
Copy after login

5.BINGO

在浏览器输入:

http://server_domain_or_IP
Copy after login

至此,你可以在阿里云的ECS上随意地用Laravel了,Happy Hacking

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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What is REST API design principles? What is REST API design principles? Apr 04, 2025 am 12:01 AM

RESTAPI design principles include resource definition, URI design, HTTP method usage, status code usage, version control, and HATEOAS. 1. Resources should be represented by nouns and maintained at a hierarchy. 2. HTTP methods should conform to their semantics, such as GET is used to obtain resources. 3. The status code should be used correctly, such as 404 means that the resource does not exist. 4. Version control can be implemented through URI or header. 5. HATEOAS boots client operations through links in response.

How do you handle exceptions effectively in PHP (try, catch, finally, throw)? How do you handle exceptions effectively in PHP (try, catch, finally, throw)? Apr 05, 2025 am 12:03 AM

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

What are anonymous classes in PHP and when might you use them? What are anonymous classes in PHP and when might you use them? Apr 04, 2025 am 12:02 AM

The main function of anonymous classes in PHP is to create one-time objects. 1. Anonymous classes allow classes without names to be directly defined in the code, which is suitable for temporary requirements. 2. They can inherit classes or implement interfaces to increase flexibility. 3. Pay attention to performance and code readability when using it, and avoid repeatedly defining the same anonymous classes.

See all articles