将Laravel部署到DigitalOcean

In this tutorial, I’ll be showing you how to deploy a Laravel application to DigitalOcean. For the purpose of this tutorial, I have created a demo Laravel application, which we’ll be deploying to DigitalOcean. The demo application can be found on GitHub.

在本教程中,我将向您展示如何将Laravel应用程序部署到DigitalOcean 。 出于本教程的目的,我创建了一个演示Laravel应用程序,并将其部署到DigitalOcean 。 该演示应用程序可以在GitHub找到

先决条件 ( Prerequisites )

This tutorial assumes the following:

本教程假定以下内容:

创建一个液滴 ( Create a Droplet )

We’ll start by spinning up a new server, which is referred to as droplet by DigitalOcean. Login to your DigitalOcean account and create a new droplet.

我们将从启动一个新服务器开始,该服务器被DigitalOcean称为Droplet。 登录到您的DigitalOcean帐户并创建一个新的Droplet。

Create droplet

We’ll be making use of DigitalOcean's 1-Click Apps to quickly spin up our server. So under Choose an image, click on the Marketplace tab and type LEMP then select it from the result as shown below:

我们将利用DigitalOcean的1 - Click Apps快速启动我们的服务器。 因此,在“选择图像”下 ,单击“ 市场”选项卡,然后键入“ LEMP”,然后从结果中选择它,如下所示:

LEMP

Next, we’ll choose the $5/month plan, which is suitable for our demo application.

接下来,我们将选择每月5美元的计划,该计划适用于我们的演示应用程序。

Choose plan

For the datacenter region, we’ll go with the default. Next, we’ll add our SSH key. If you have already added SSH keys to DigitalOcean before, you can choose from those:

对于数据中心区域,我们将使用默认值。 接下来,我们将添加我们的SSH密钥。 如果您之前已经将SSH密钥添加到DigitalOcean ,则可以从以下选项中进行选择:

Select SSH key

If not, you’ll need to click on the New SSH Key button to add a new SSH key. You can get your SSH key by running the command below on your local computer:

如果没有,则需要单击“ 新建SSH密钥”按钮以添加新的SSH密钥。 您可以通过在本地计算机上运行以下命令来获取SSH密钥:

cat ~/.ssh/id_rsa.pub

The command above will print your SSH key on the terminal, which you can then copy and paste in the SSH Key Content field and give your SSH key a name.

上面的命令将在终端上打印SSH密钥,然后您可以将其复制并粘贴到“ SSH密钥内容”字段中,并为SSH密钥命名。

Add SSH key

Finally, choose a hostname (if you don’t want the randomly generated name) for the droplet and click the Create Droplet button. We should now have a server up and running on Ubuntu 18.04 with the following software installed and configured:

最后,为小滴选择一个主机名(如果不想使用随机生成的名称),然后单击创建小滴按钮。 现在,我们应该在Ubuntu 18.04上启动并运行服务器,并安装并配置以下软件:

LEMP softwares

Take note of the IP address of the server as we’ll be using it to access the server in subsequent sections.

记下服务器的IP地址,因为我们将在随后的部分中使用它来访问服务器。

配置服务器 ( Configuring The Server )

As a security measure, it is recommended to carry out tasks on a server as a non-root user with administrative privileges. So before we start configuring our server, let’s create a non-root user which we’ll use to administer our server for the rest of the tutorial. First, we need to login to the server as root. We can do that using the server’s IP address. Enter the command below on your local computer:

作为安全措施,建议以具有管理特权的非root用户身份在服务器上执行任务。 因此,在开始配置服务器之前,让我们创建一个非root用户,在本教程的其余部分中,我们将使用该非root用户来管理我们的服务器。 首先,我们需要以root身份登录到服务器。 我们可以使用服务器的IP地址来实现。 在本地计算机上输入以下命令:

ssh root@IP_ADDRESS

Once we are logged in to the server, we can move on to create a new user:

登录到服务器后,我们可以继续创建新用户:

adduser mezie

This will create a new user called mezie, you can name the user whatever you like. You will be asked a few questions, starting with the account password. Having created the new user, we need to give it administrative privileges. That is, the user will be able to perform administrative tasks by using sudo command:

这将创建一个名为mezie的新用户,您可以根据需要命名该用户。 从帐户密码开始,系统将询问您几个问题。 创建新用户后,我们需要为其赋予管理权限。 也就是说,用户将能够使用sudo命令执行管理任务:

usermod -aG sudo mezie

The command above adds the user mezie to sudo group. Now, the user can run commands with superuser privileges. Next, let’s set up SSH key for the newly created user. You need to copy your public key to your new server. Enter the command below on your local computer:

上面的命令将用户mezie添加到sudo组。 现在,用户可以使用超级用户权限运行命令。 接下来,让我们为新创建的用户设置SSH密钥。 您需要将公钥复制到新服务器。 在本地计算机上输入以下命令:

cat ~/.ssh/id_rsa.pub

Copy the SSH key printed to the terminal. For the new user to login to the server with an SSH key, we must add the public key to a special file in the user's home directory. Still logged in as root on the server, run the command below:

将打印的SSH密钥复制到终端。 为了使新用户使用SSH密钥登录服务器,我们必须将公共密钥添加到用户主目录中的特殊文件中。 仍以root身份登录到服务器,运行以下命令:

su - mezie

This will temporarily switch to the new user. Now, you’ll be in your new user's home directory. Next, we need to create a new directory called .ssh and restrict its permission:

这将临时切换到新用户。 现在,您将位于新用户的主目录中。 接下来,我们需要创建一个名为.ssh的新目录并限制其权限:

mkdir ~/.ssh
chmod 700 ~/.ssh

Within the .ssh directory, create a new file called authorized_keys:

.ssh目录中,创建一个名为authorized_keys的新文件:

touch ~/.ssh/authorized_keys

Open the file:

打开文件:

vim ~/.ssh/authorized_keys

and paste your public key (copied above) into the file. To save the file, hit esc to stop editing, then :wq and press ENTER. Next, restrict the permissions of the authorized_keys file with this command:

并将您的公共密钥(上面复制)粘贴到文件中。 要保存文件,请按esc停止编辑,然后按:wq并按ENTER 。 接下来,使用以下命令限制authorized_keys文件的权限:

chmod 600 ~/.ssh/authorized_keys

Type the command below to return to the root user:

键入以下命令以返回到root用户:

exit

Now, your public key is installed, and you can use SSH keys to log in as the new user. To make sure you can log in as the new user with SSH. Enter the command below in a new terminal on your local computer:

现在,您的公共密钥已安装,并且您可以使用SSH密钥以新用户身份登录。 为了确保您可以使用SSH作为新用户登录。 在本地计算机的新终端中输入以下命令:

ssh mezie@IP_ADDRESS

If all went well, you’ll be logged in to the server as the new user with SSH. The rest of the tutorial assumes you are logged in to the server with the newly created user (mezie in my case). Because we chose 1-Click App while creating our droplet, ufw firewall is enabled, which keeps our server secured. Now, we need to open the firewall for only HTTP since we are not concerned with SSL in this tutorial:

如果一切顺利,您将以SSH新用户身份登录到服务器。 本教程的其余部分假定您使用新创建的用户(在我的情况下为mezie )登录服务器。 由于我们在创建ufw时选择了1- 单击应用程序 ,因此启用了ufw防火墙,从而确保了服务器的安全。 现在,我们只需要为HTTP打开防火墙,因为在本教程中我们不关心SSL:

sudo ufw allow 'Nginx HTTP'

安装和配置MySQL ( Installing and Configuring MySQL )

The demo application we’re deploying in this tutorial makes use of MySQL, so we need to install and configure MySQL on our server:

我们将在本教程中部署的演示应用程序使用MySQL,因此我们需要在服务器上安装和配置MySQL:

sudo apt install mysql-server

Next, let’s configure MySQL:

接下来,让我们配置MySQL:

sudo mysql_secure_installation

Enter a root password when prompted, then answer the necessary options when prompted. With the MySQL configured, we need to create a database and a user. First, log in to the MySQL server:

在提示时输入root密码,然后在提示时回答必要的选项。 配置了MySQL后,我们需要创建一个数据库和一个用户。 首先,登录到MySQL服务器:

mysql -u root -p

Provide the root password you enter above while installing MySQL. Once you are logged in, create a new user:

提供您在安装MySQL时在上方输入的root密码。 登录后,创建一个新用户:

CREATE USER'YOUR_USERNAME'@'localhost' IDENTIFIED BY 'YOUR_PASSWORD';

Replace YOUR_USERNAME and YOUR_PASSWORD with your user and password respectively. Then execute the following command:

分别用您的用户名和密码替换YOUR_USERNAMEYOUR_PASSWORD 。 然后执行以下命令:

ALTER USER'YOUR_USERNAME'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YOUR_PASSWORD';

This will allow us to use a password when connecting to MySQL as the created user. Remember to change YOUR_USERNAME and YOUR_PASSWORD to your database user and password respectively. Next, create a new database:

这将允许我们以创建的用户身份连接到MySQL时使用密码。 切记分别将YOUR_USERNAMEYOUR_PASSWORD更改为您的数据库用户和密码。 接下来,创建一个新的数据库:

CREATE DATABASE laravel-deploy-demo;

Then we need to grant the new user privileges to the tables on the new database:

然后,我们需要向新数据库上的表授予新用户特权:

GRANT ALL ON laravel-deploy-demo.* TO'YOUR_USERNAME'@'localhost';

For the changes to take effect, run:

为使更改生效,请运行:

FLUSH PRIVILEGES;

Finally, exit the MySQL server:

最后,退出MySQL服务器:

exit;

安装Laravel所需PHP扩展 ( Installing Laravel Required PHP Extensions )

Laravel requires some PHP extensions to be installed on our server. As it stands our server already have most of them installed, we just need to install the remaining ones ourselves:

Laravel需要在我们的服务器上安装一些PHP扩展。 就目前而言,我们的服务器已经安装了大多数服务器,我们只需要自己安装其余的服务器即可:

sudo apt update && sudo apt install php-bcmath php-mbstring php-xml

安装作曲家 ( Installing Composer )

We need to Composer to install PHP packages, so let's make sure it's installed on our server. First, the let's install unzip, which is required by Composer for extracting packages:

我们需要Composer来安装PHP软件包,因此,请确保将其安装在我们的服务器上。 首先,让我们安装unzip ,这是Composer提取软件包所必需的:

sudo apt update && sudo apt install unzip

Now, we can install Composer:

现在,我们可以安装Composer:

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

Next, let'ts make sure Composer is installed system-wide command by move the Composer file to /usr/local/bin directory:

接下来,通过将Composer文件移动到/usr/local/bin目录中,以确保Composer已在系统范围内安装:

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

Now, we can access Composer from any using composer.

现在,我们可以使用composer从任何对象访问Composer。

引入我们的应用程序 ( Pulling In Our Application )

For the tutorial, I have created a demo Laravel application that we'll be deploying. We’ll be pulling in our application to our server using Git. We are going to clone the app unto the server inside /var/www directory:

对于本教程,我创建了一个演示的Laravel应用程序,我们将对其进行部署。 我们将使用Git将应用程序拉入服务器。 我们将把应用程序克隆到/var/www目录中的服务器:

cd /var/www
git clone https://github.com/ammezie/laravel-deploy-demo.git

Once cloned, run the following command to install the necessary packages:

克隆后,运行以下命令以安装必要的软件包:

cd laravel-deploy-demo
composer install

Laravel reads most of its configuration settings from environment variables, which are store inside a .env file located in the root of every Laravel application. This makes it easier to make use of different configuration settings depending on the environment (development, staging, production, etc.) the application is running. By default, Laravel ships a .env.example file, which can serve as a template for creating our application actual .env file. Let’s create the .env file:

Laravel从环境变量读取大多数配置设置,这些环境变量存储在每个Laravel应用程序根目录下的.env文件中。 这使得根据应用程序正在运行的环境(开发,登台,生产等)使用不同的配置设置变得更加容易。 默认情况下,Laravel附带一个.env.example文件,该文件可以用作创建应用程序实际.env文件的模板。 让我们创建.env文件:

cp .env.example .env

Next, we need to generate an APP_KEY:

接下来,我们需要生成一个APP_KEY

php artisan key:generate

Open .env:

打开.env

vim .env

and paste the following into it:

并将以下内容粘贴到其中:

// .env

APP_NAME=laravel-deploy-demo
APP_ENV=production
APP_DEBUG=false
APP_URL=http://DOMAIN_NAME_OR_IP_ADDRESS

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel-deploy-demo
DB_USERNAME=YOUR_DATABASE_USERNAME
DB_PASSWORD=YOUR_DATABASE_PASSWORD

Since we on production, we set APP_DEBUG to false. Also, we set APP_URL to our domain name or server IP address (if no domain name has been set up). Remember to update YOUR_DATABASE_USERNAME and YOUR_DATABASE_PASSWORD with your database details from the previous section. Now, we can run the migration:

从生产开始,我们将APP_DEBUG设置为false 。 另外,我们将APP_URL设置为我们的域名或服务器IP地址(如果未设置任何域名)。 请记住使用上一部分中的数据库详细信息更新YOUR_DATABASE_USERNAMEYOUR_DATABASE_PASSWORD 。 现在,我们可以运行迁移了:

php artisan migration:run --force

Because we are on production, we have to use the --force flag, otherwise, the migration will not run.

因为我们正在生产中,所以必须使用--force标志,否则,迁移将不会运行。

设置Nginx ( Setting Up Nginx )

Next, let's configure Nginx to serve our application. For that, we'll create a new virtual host configuration file inside /etc/nginx/sites-available:

接下来,让我们配置Nginx服务我们的应用程序。 为此,我们将在/etc/nginx/sites-available内部创建一个新的虚拟主机配置文件:

sudo vim /etc/nginx/sites-available/laravel-deploy-demo

and pasting in the following code:

并粘贴以下代码:

// /etc/nginx/sites-available/laravel-deploy-demo

server{
    listen 80;
    server_name DOMAIN_NAME_OR_IP_ADDRESS;
    root /var/www/laravel-deploy-demo/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ .php$ {
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /.(?!well-known).* {
        deny all;
    }
}

We set the server_name to that of our server IP address or domain name (if it’s set up). Also, we set the root to point the public directory of our application, since it contains the index.php file, which serves as the entry file for a Laravel application. Next, we need to activate the newly created virtual host configuration file by creating a symbolic link:

我们将server_name设置为服务器IP地址或域名(如果已设置)。 另外,我们将root目录设置为指向应用程序的public目录,因为它包含index.php文件,该文件用作Laravel应用程序的入口文件。 接下来,我们需要通过创建符号链接来激活新创建的虚拟主机配置文件:

sudo ln -s /etc/nginx/sites-available/laravel-deploy-demo /etc/nginx/sites-enabled/

To make sure there are no errors in the configuration, run the command below:

要确保配置中没有错误,请运行以下命令:

sudo nginx -t

Then we can restart Nginx for our changes to take effect:

然后,我们可以重新启动Nginx来使更改生效:

sudo service nginx restart

Next, we need to give the web server write permissions to the storage and bootstrap/cache directories respectively:

接下来,我们需要为Web服务器分别授予对storagebootstrap/cache目录的写权限:

sudo chown -R www-data.www-data /var/www/laravel-deploy-demo/storage
sudo chown -R www-data.www-data /var/www/laravel-deploy-demo/bootstrap/cache

Now, we should be able to access our application with our server’s IP_ADDRESS. If a domain name has been set up for our server, then we should be able to access our application using the domain name as well.

现在,我们应该能够使用服务器的IP_ADDRESS访问我们的应用程序。 如果已经为我们的服务器设置了域名,那么我们也应该能够使用该域名访问我们的应用程序。

You should get something similar to the image below:

您应该获得类似于下图的内容:

Demo

结论 ( Conclusion )

In this tutorial, we have seen how to deploy a Laravel application to DigitalOcean. Also, we saw how easy it is to spin up a new server with DigitalOcean’s 1-Click App.

在本教程中,我们已经看到了如何将Laravel应用程序部署到DigitalOcean。 此外,我们看到了使用DigitalOcean的1 单击应用启动新服务器是多么容易。

Now, that we have successfully deployed our application, we can check out the optimization section of the Laravel docs on some optimization techniques we can take to improve our application's performance on production.

现在,我们已经成功部署了应用程序,我们可以查看Laravel文档的优化部分 ,其中介绍了一些可以用来提高应用程序生产性能的优化技术。

翻译自: https://scotch.io/tutorials/deploying-laravel-to-digitalocean

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值