在阿里云的ECS上部署Laravel项目
在阿里云的ECS上部署Laravel项目
在搭建完LEMP环境之后,首先要明确两个重要目录
Nginx的默认root文件夹
/usr/share/nginx/html
Nginx的服务器配置文件所在目录
/etc/nginx/sites-available/
上面两个目录记住就好,很常用,先摆出来
下面一步一步在阿里云ECS上部署Laravel
1.创建网站的根目录
sudo mkdir -p /var/www
2.配置nginx服务器
sudo vim /etc/nginx/sites-available/default
打开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; }}
其中root,index ,server_name和location这几行需要稍微修改一下
root修改
root /var/www/laravel/public;
这里就是将nginx服务器的根目录指向Laravel的public文件夹下,后续的Laravel项目的代码我们会放在我们之前创建的/var/www/laravel目录下
index修改
index index.php index.html index.htm;
这里需要注意的是,将index.php排在最前面
server_name修改
server_name server_domain_or_IP;
将server_domain_or_IP修改为你的公网IP
location修改
location / { try_files $uri $uri/ /index.php?$query_string;}
修改完是这样的:
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; }}
最后我们还需要配置一下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; }}
注意,这一块是自己加上去的:
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; }
配置完之后重启Nginx,使上面的配置项生效。
sudo service nginx restart
3.创建Laravel项目
在配置完nginx后,怎么获取Laravel的项目代码呢?有以下几种方法:
(1).直接composer安装
直接通过composer来安装,你可以在CES上通过执行
cd ~curl -sS https://getcomposer.org/installer | php
上面命令会安装composer
composer全局使用:
sudo mv composer.phar /usr/local/bin/composer
然后在/var/www目录下直接执行
sudo composer create-project laravel/laravel laravel
因为我们之前创建/var/www目录,你可以直接cd /var/www然后执行上面的命令。然后坐等安装完成。
(2).直接上传代码
使用下面命令上传
scp -r laravel root@your_IP:
然后在阿里云的ECS上将laravel移动到/var/www目录下
sudo mv laravel/ /var/www
(3).使用Git和Coding平台
个人比较喜欢使用git来上传代码,可以很方便的更新代码和进行回滚,一旦版本更新出Bug我可以借助Git的强大版本管理能力来修复Bug。流程大概是这样:
本地代码---->Coding---->阿里云ECS
既然要使用git,那么先在ECS上安装git:
sudo apt-get install git
安装完成就可以使用git了,然后在Coding上创建一个私有项目laravel,里面包含所有该Laravel项目所需代码。
一旦本地代码都推送到Coding,然后在/var/www目录下直接使用
git clone your-project-git-link
your-project-git-link替换为你Coding上的laravel项目地址
4.最后的最后
不管哪种方式安装的代码,/var/www/都是属于root用户的,而访问网站的用户则需要正确的权限和访问限制,我们可以通过下面的命令来实现。
sudo chown -R :www-data /var/www/laravel
根据Laravel的官方文档,/var/www/laravel/app/storage目录需要给网站的用户写权限
sudo chmod -R 775 /var/www/laravel/app/storage
5.BINGO
在浏览器输入:
http://server_domain_or_IP
至此,你可以在阿里云的ECS上随意地用Laravel了,Happy Hacking

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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,

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.

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.

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.

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.

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.

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.

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.
