본문 바로가기

리눅스

[리눅스] Ubuntu 22.04 LTS에 laravel 9을 설치하는 방법

728x90

Ubuntu 22.04 LTS에 laravel 9을 설치하는 방법

테스트 환경

$ cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04 (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

도커 컨테이너 실행 및 컨테이너 진입

도커 컨테이너 실행

docker run -d --privileged -p 80:80 --name ubuntu --hostname ubuntu anti1346/ubuntu-init:22.04 /sbin/init

ubuntu 컨테이너에 진입

docker exec -it ubuntu bash

nginx, php-fpm 설치

우분투 업데이트

apt update && apt upgrade -y
apt install -y wget gnupg2 ca-certificates lsb-release ubuntu-keyring software-properties-common

nginx 설치 및 설정

nginx GPG Key 다운로드 및 추가

curl -Ss https://nginx.org/keys/nginx_signing.key | gpg --dearmor | tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

nginx stable 리포지토리 추가

echo deb [arch=amd64,arm64 signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu `lsb_release -cs` nginx | tee /etc/apt/sources.list.d/nginx-stable.list
apt update

nginx 설치

apt install -y nginx

nginx 버전 확인

nginx -v
$ nginx -v
nginx version: nginx/1.20.2

nginx 데몬 활성화 및 nginx 데몬 시작 

systemctl --now enable nginx

nginx 데몬 재시작

$ systemctl --now enable nginx
Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable nginx

http://localhost

php-fpm 설치 및 설정

PPA 추가

add-apt-repository ppa:ondrej/php -y
apt update

php(php-fpm) 설치

apt install -y php-fpm

php 버전 확인

$ php --version
PHP 8.1.6 (cli) (built: May 17 2022 16:46:54) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.6, Copyright (c), by Zend Technologies

php-fpm 데몬 활성화 및 php-fpm 데몬 시작 

systemctl --now enable php8.1-fpm

php 모듈 추가 설치

apt install -y php-cli php-curl php-gd php-zip php-intl php-common php-bcmath php-imagick php-xmlrpc php-readline php-redis php-mbstring php-xml
php -m | egrep 'redis|imagick'
$ php -m | egrep 'redis|imagick'
imagick
redis

phpinfo 페이지 생성

echo "<?php phpinfo(); ?>" > phpinfo.php

nginx default.conf 편집

vim /etc/nginx/conf.d/default.conf
$ vim /etc/nginx/conf.d/default.conf
server {
    listen 80;
    server_name localhost;

    access_log /var/log/nginx/host.access.log  main;

    root /var/www/html;

    location / {
        index index.html index.htm index.php;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass   unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include        fastcgi_params;
    }

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

nginx, php-fpm 데몬 재시작

systemctl restart nginx php8.1-fpm

http://localhost/phpinfo.php

composer 설치

curl, unzip 패키지 설치

apt install -y curl unzip

php 모듈 추가 설치

apt install -y php php-curl
apt install -y php-common php-json php-mbstring php-zip php-xml php-tokenizer

composer 다운로드

curl -sS https://getcomposer.org/installer -o composer-setup.php

php를 사용하여 composer 설치

php composer-setup.php --install-dir=/usr/local/bin --filename=composer

composer 버전 확인

composer -V
$ composer -V
Composer version 2.3.5 2022-04-13 16:43:00

라라벨 설치

composer global require laravel/installer
$ composer global require laravel/installer
Changed current directory to /root/.config/composer
Info from https://repo.packagist.org: #StandWithUkraine
Using version ^4.2 for laravel/installer
./composer.json has been created
Running composer update laravel/installer
Loading composer repositories with package information
Updating dependencies
Lock file operations: 10 installs, 0 updates, 0 removals
  - Locking laravel/installer (v4.2.10)
  - Locking psr/container (2.0.2)

...

  - Installing symfony/console (v6.0.8): Extracting archive
  - Installing laravel/installer (v4.2.10): Extracting archive
4 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
8 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

환경변수(PATH) 등록

echo 'PATH=$PATH:$HOME/.config/composer/vendor/bin' >> ~/.bashrc
source ~/.bashrc

 


라라벨로 프로젝트(helloworld) 생성

 


컴포즈로 프로젝트((helloworld) 생성

composer create-project --prefer-dist laravel/laravel helloworld
$ cd /var/www


$ composer create-project --prefer-dist laravel/laravel helloworld
Creating a "laravel/laravel" project at "./helloworld"
Installing laravel/laravel (v9.1.8)
  - Installing laravel/laravel (v9.1.8): Extracting archive
Created project in /var/www/helloworld
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies
Lock file operations: 108 installs, 0 updates, 0 removals
  - Locking brick/math (0.9.3)
  - Locking dflydev/dot-access-data (v3.0.1)
  
...

  - Installing spatie/ignition (1.3.1): Extracting archive
  - Installing spatie/laravel-ignition (1.2.3): Extracting archive
54 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: laravel/sail
Discovered Package: laravel/sanctum
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Discovered Package: spatie/laravel-ignition
Package manifest generated successfully.
78 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force
No publishable resources for tag [laravel-assets].
Publishing complete.
> @php artisan key:generate --ansi
Application key set successfully.
root@ubuntu:/var/www$ cd helloworld


root@ubuntu:/var/www/helloworld$ ls -l
total 348
-rw-r--r--  1 root root   3958 May  6 04:52 README.md
drwxr-xr-x  7 root root   4096 May  6 04:52 app
-rwxr-xr-x  1 root root   1686 May  6 04:52 artisan
drwxr-xr-x  3 root root   4096 May  6 04:52 bootstrap
-rw-r--r--  1 root root   1707 May  6 04:52 composer.json
-rw-r--r--  1 root root 284049 May 21 23:51 composer.lock
drwxr-xr-x  2 root root   4096 May  6 04:52 config
drwxr-xr-x  5 root root   4096 May  6 04:52 database
drwxr-xr-x  3 root root   4096 May  6 04:52 lang
-rw-r--r--  1 root root    473 May  6 04:52 package.json
-rw-r--r--  1 root root   1175 May  6 04:52 phpunit.xml
drwxr-xr-x  2 root root   4096 May  6 04:52 public
drwxr-xr-x  5 root root   4096 May  6 04:52 resources
drwxr-xr-x  2 root root   4096 May  6 04:52 routes
drwxr-xr-x  5 root root   4096 May  6 04:52 storage
drwxr-xr-x  4 root root   4096 May  6 04:52 tests
drwxr-xr-x 42 root root   4096 May 21 23:51 vendor
-rw-r--r--  1 root root    559 May  6 04:52 webpack.mix.js

라라벨 버전 확인(Laravel Framework Version)

php artisan --version
$ php artisan --version
Laravel Framework 9.13.0

라라벨 디렉토리 권한 설정

cd /var/www/helloworld
chown -R :www-data ./{storage,bootstrap/cache}
chmod -R 777 storage
chmod -R 775 bootstrap/cache

키 생성

php artisan key:generate
$ php artisan key:generate


$ cat .env | grep APP_KEY
APP_KEY=base64:UlnaO7fCvK9PvzSVlie8BCfkjBSc6Llxtn0HUkP0Gfs=
PUSHER_APP_KEY=
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"

nginx, php(php-fpm) 설정

default.conf 편집

server {
    listen 80;
    server_name _;
    root /var/www/helloworld/public;

    access_log  /var/log/nginx/host.access.log  main;

    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;

    #error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    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; }

    location ~ \.php$ {
	try_files $uri =404;
	fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    	include fastcgi_params;
    }

    location ~ /\.ht {
	deny all;
    }
}

fastcgi_params 편집

cat <<'EOF' >> /etc/nginx/fastcgi_params

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
fastcgi_param  PATH_INFO          $fastcgi_path_info;
EOF
$ vim /etc/nginx/fastcgi_params
...
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
fastcgi_param  PATH_INFO          $fastcgi_path_info;

nginx, php-fpm 데몬 재시작

systemctl restart nginx php8.1-fpm

http://localhost


helloworld 페이지

1안)

web.php 편집

 

$ vim routes/web.php
<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('helloworld', function () {
    return '<h1>Hello World!!</h1>';
});

http://localhost/helloworld


2안)

web.php 편집

$ vim routes/web.php
<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('helloworld', function () {
    return view('helloworld');
});

helloworld.blade.php 생성 및 편집

vim resources/views/helloworld.blade.php
$ vim resources/views/helloworld.blade.php
<html>
  <body>
    <h1> Hello World!! </h1>
    <?php
      echo "Hello World!!";
      echo "<br/>";
      echo "<br/>";
      date_default_timezone_set('Asia/Seoul');
      $formatted = date('Y-m-d H:i:s T');
      echo "The date and time is $formatted.";
    ?>
  </body>
</html>

http://localhost/helloworld

728x90