Laravel 6 部署到阿里云 Ubuntu 18.04

Linux Laravel   后端笔记   1个月前

  • 先更新一下包管理器索引并升级旧包。
sudo apt update && sudo apt upgrade
  • 安装 Nginx
sudo apt install nginx -y
  • 看一下 Nginx 的状态
systemctl status nginx
  • 安装 PHP
sudo apt install php7.2 php7.2-curl php7.2-common php7.2-cli php7.2-mysql php7.2-mbstring php7.2-fpm php7.2-xml php7.2-zip
  • 安装 MariaDB
sudo apt install mariadb-server
  • 看一下安装成功没有
mysql -V
  • 一路 Y 下来
sudo mysql_secure_installation
Set root password? [Y/n] Y
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
  • 安装一下 Composer
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
  • 配置阿里云的镜像
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
  • 安装一下 unzip
sudo apt install unzip
  • 来到 /var/www/ 准备安装 Laravel
cd /var/www/
composer create-project --prefer-dist laravel/laravel laravel
  • 给一下权限
sudo chown -R www-data:www-data /var/www/laravel/
sudo chmod -R 755 /var/www/laravel/
  • 创建数据库和用户
$ mysql -u root -p
mysql> CREATE DATABASE laravel;
mysql> GRANT ALL ON laravel.* to 'laravel'@'localhost' IDENTIFIED BY 'password';
mysql> FLUSH PRIVILEGES;
mysql> quit
  • 修改一下 .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=password
  • 配置一下 Nginx
cd /etc/nginx/
vim sites-available/laravel.conf
server {
    listen 80;
    listen [::]:80 ipv6only=on;

    # Log files for Debugging
    access_log /var/log/nginx/laravel-access.log;
    error_log /var/log/nginx/laravel-error.log;

    # Webroot Directory for Laravel project
    root /var/www/example.com/public;
    index index.php index.html index.htm;

    # Your Domain Name
    server_name example.com;

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

    # PHP-FPM Configuration Nginx
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
  • 打个软连接到 sites-enabled
ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
  • 测试一下 Nginx
nginx -t
  • 重启 Nginx
systemctl restart nginx

安装成功




如果你不行动
最好的情况就只是现在
如果你行动了
最坏的情况也不过是现在
所以,你在担心什么?