LoginSignup
8
7

More than 5 years have passed since last update.

Ubuntu18.04にLaravelの環境を構築する

Last updated at Posted at 2018-07-18

AWS上に自分のメモようにLaravelの環境を構築してみました。
利用したubuntuのイメージはこちらです。
スクリーンショット 2018-07-18 10.02.26.png

Ubuntuにパッケージをインストールする

sudo apt-get install -y language-pack-ja-base
sudo LC_ALL=ja_JP.UTF-8 add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install zip unzip
sudo apt-get install -y php7.2 php7.2-fpm php7.2-mysql php7.2-zip php7.2-gd
sudo apt-get install nginx git
sudo apt-get install mcrypt php7.1-mcrypt
sudo apt-get install -y php7.2-mbstring php7.2-xml --force-yes
sudo apt-get install php7.2-curl php7.2-json

php.iniを編集する

次のコマンドを利用してphp.iniを編集します。

sudo vi /etc/php/7.2/fpm/php.ini

修正する箇所はcgi.fix_pathinfoです。

cgi.fix_pathinfo=0

PHP 7.2 FPMの再起動。

sudo service php7.2-fpm restart

Nginxの設定

Laravelを設置する場所を/var/www/laravelにします。
ターミナルで以下のコマンドを実行し、フォルダを作成します。

sudo mkdir -p /var/www/laravel

ファイルを編集する前に、nignixのデフォルトの設定箇所/etc/nginx/sites-available/defaultのバッグアップをとります。
ターミナルで以下のコマンドを実行し、バッグアップをとります。

sudo mkdir ~/Backups
sudo cp /etc/nginx/sites-available/default ~/Backups/default

続いて、以下のコマンドを実行してファイルを編集します。

sudo vi /etc/nginx/sites-available/default

編集後

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/laravel/public;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm;

        server_name 50.50.50.50;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php?$query_string;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        # 末尾が.phpの要求を対象とする設定
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                # FastCGIサーバは自動index付与をサポートしていない
                # その為、Nginx側が末尾が"/"だった場合fastcgi_indexの値を末尾に付与する
                fastcgi_index index.php;
                # PHP-FPMに渡されるドキュメントパス
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                # その他のFastCGI関連の設定は以下に追加
                include fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

nginxの再起動

sudo service nginx restart

Laravelのプロジェクトを作成

最初にcomposerの環境を作成します。

cd ~
// 管理者になりcomposerをインストールする
sudo su
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

Laravelのプロジェクトの作成

sudo composer create-project laravel/laravel /var/www/laravel

作成したプロジェクトの権限まわりの設定

sudo chown -R :www-data /var/www/laravel
sudo chmod -R 775 /var/www/laravel/storage

追記
プロジェクトを作り直したいときは以下のコマンドで削除して、再作成します。

sudo rm -rf /var/www/laravel
sudo mkdir /var/www/laravel

確認

作成したプロジェクトをhttp://server_domain_or_IPで確認してみてください。
ページにLaravelと表示されるはずです。

参考元
https://gist.github.com/santoshachari/87bf77baeb45a65eb83b553aceb135a3
*こちらをほぼそのまま利用させていただきました。ただ、PHPのパッケージは最新のものに変更しています。

8
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7