웹사이트 검색

Nginx와 함께 Laravel PHP 프레임워크를 설치하는 방법 및 Free Lets Encrypt SSL on AlmaLinux 8


이 페이지에서

  1. 전제 조건
  2. LEMP 서버 설치
  3. 작성기 설치
  4. Alma Linux 8에 Laravel 설치
  5. Laravel용 Nginx 가상 호스트 만들기\n
  6. Laravel에 대한 방화벽 구성\n
  7. Laravel 웹 UI에 액세스
  8. Laravel 웹사이트에서 SSL 활성화\n
  9. 결론

Laravel은 PHP 기반 웹 애플리케이션 구축에 사용되는 무료 오픈 소스 경량 PHP 웹 프레임워크입니다. 우아한 구문, 고급 기능 및 강력한 도구 세트로 인해 인기가 있습니다. Symfony 프레임워크를 기반으로 하며 개발자가 웹 애플리케이션 개발을 단순화할 수 있도록 도와줍니다. 또한 애플리케이션에 대한 작업을 수행하기 위한 Artisan 명령줄 인터페이스를 제공합니다. Artisan, MVC 아키텍처, 개체 관계형 매핑, 템플릿 엔진, 단위 테스트 및 데이터베이스 마이그레이션 시스템을 포함한 강력한 기능을 제공합니다.

이 게시물에서는 Alma Linux 8에서 Nginx와 함께 Laravel을 설치하는 방법을 보여줍니다.

전제 조건

  • Alma Linux 8을 실행하는 서버.\n
  • 서버 IP를 가리키는 유효한 도메인 이름입니다.\n
  • 서버에 루트 암호가 구성되어 있습니다.\n

LEMP 서버 설치

먼저 서버에 Nginx, MariaDB, PHP 및 기타 필수 PHP 확장을 설치해야 합니다. 다음 명령을 실행하여 모두 설치할 수 있습니다.

dnf install nginx mariadb-server php php-fpm php-common php-xml php-mbstring php-json php-zip php-mysqlnd curl unzip -y

모든 패키지를 설치한 후 php-fpm 구성 파일을 편집하고 Nginx를 사용하도록 구성합니다.

nano /etc/php-fpm.d/www.conf

다음 줄을 변경합니다.

listen.owner = nginx
listen.group = nginx

파일을 저장하고 닫은 다음 PHP 구성 파일을 편집하고 기본값을 변경합니다.

nano /etc/php.ini

다음 줄을 변경합니다.

date.timezone = Asia/Kolkata
cgi.fix_pathinfo=1

파일을 저장하고 닫은 후 다음 명령을 사용하여 Nginx, MariaDB 및 PHP-FPM 서비스를 시작하고 활성화합니다.

systemctl start nginx
systemctl start mariadb
systemctl start php-fpm
systemctl enable nginx
systemctl enable mariadb
systemctl enable php-fpm

완료되면 다음 단계로 진행할 수 있습니다.

작곡가 설치

이 게시물에서는 Composer를 사용하여 Laravel을 설치합니다. 따라서 시스템에 Composer를 설치해야 합니다. 다음 명령을 실행하여 설치할 수 있습니다.

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

다음과 같은 결과가 표시됩니다.

All settings correct for using Composer
Downloading...

Composer (version 2.2.3) successfully installed to: /root/composer.phar
Use it: php composer.phar

그런 다음 Composer 바이너리를 시스템 경로로 이동하고 다음 명령을 사용하여 적절한 권한을 설정합니다.

mv composer.phar /usr/local/bin/composer
chmod +x /usr/local/bin/composer

그런 다음 다음 명령을 사용하여 Composer 버전을 확인합니다.

composer --version

다음과 같은 결과가 표시됩니다.

Composer version 2.2.3 2021-12-31 12:18:53

Alma Linux 8에 Laravel 설치

다음으로 디렉토리를 Nginx 웹 루트 디렉토리로 변경하고 Composer를 사용하여 Laravel을 설치합니다.

cd /var/www/html/
composer create-project --prefer-dist laravel/laravel laravel

다음과 같은 결과가 표시됩니다.

Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
69 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan key:generate --ansi
Application key set successfully.

다음으로 Laravel에 적절한 소유권과 권한을 설정합니다.

chown -R nginx:nginx /var/www/html/laravel/
chown -R nginx:nginx /var/www/html/laravel/storage/
chown -R nginx:nginx /var/www/html/laravel/bootstrap/cache/
chmod -R 0777 /var/www/html/laravel/storage/
chmod -R 0775 /var/www/html/laravel/bootstrap/cache/

완료되면 다음 단계로 진행할 수 있습니다.

Laravel용 Nginx 가상 호스트 생성

다음으로 Laravel용 Nginx 구성 파일을 만들어야 합니다. 다음 명령을 사용하여 만들 수 있습니다.

nano /etc/nginx/conf.d/laravel.conf

다음 줄을 추가합니다.

server {
       listen 80;
       server_name laravel.exampledomain.com;
       root        /var/www/html/laravel/public;
       index       index.php;
       charset utf-8;
       gzip on;
	gzip_types text/css application/javascript text/javascript application/x-javascript  image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
        location / {
        	try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php {
                include fastcgi.conf;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/run/php-fpm/www.sock;
        }
        location ~ /\.ht {
                deny all;
        }
}

파일을 저장하고 닫은 다음 Laravel에서 구성 오류가 있는지 확인합니다.

nginx -t

다음 출력이 표시되어야 합니다.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

다음으로 Nginx 및 PHP-FPM 서비스를 다시 시작하여 변경 사항을 적용합니다.

systemctl restart php-fpm
systemctl restart nginx

다음 명령을 사용하여 Nginx 상태를 확인할 수도 있습니다.

systemctl status nginx

다음과 같은 결과가 표시됩니다.

? nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/nginx.service.d
           ??php-fpm.conf
   Active: active (running) since Fri 2022-01-07 08:29:11 UTC; 4s ago
  Process: 8186 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 8184 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 8182 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 8188 (nginx)
    Tasks: 2 (limit: 11411)
   Memory: 3.7M
   CGroup: /system.slice/nginx.service
           ??8188 nginx: master process /usr/sbin/nginx
           ??8189 nginx: worker process

Jan 07 08:29:11 linux systemd[1]: nginx.service: Succeeded.
Jan 07 08:29:11 linux systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Jan 07 08:29:11 linux systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jan 07 08:29:11 linux nginx[8184]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jan 07 08:29:11 linux nginx[8184]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jan 07 08:29:11 linux systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jan 07 08:29:11 linux systemd[1]: Started The nginx HTTP and reverse proxy server.

Laravel에 대한 방화벽 구성

다음으로 방화벽 방화벽을 통해 포트 80 및 443을 허용해야 합니다. 다음 명령으로 허용할 수 있습니다.

firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --zone=public --permanent --add-service=https

다음으로 방화벽을 다시 로드하여 변경 사항을 적용합니다.

firewall-cmd --reload

Laravel 웹 UI에 액세스

이제 웹 브라우저를 열고 URL http://laravel.exampledomain.com을 사용하여 Laravel 웹 UI에 액세스하십시오. 다음 화면에 Laravel 기본 페이지가 표시되어야 합니다.

Laravel 웹사이트에서 SSL 활성화

보안 연결을 위해 Laravel 웹사이트에서 SSL을 활성화하는 것이 좋습니다. Lets Encrypt는 도메인에 대한 SSL/TLS 인증서를 획득, 갱신 및 관리할 수 있는 무료 SSL을 제공합니다. 먼저 다음 명령을 사용하여 Certbot 클라이언트를 설치합니다.

dnf install epel-release -y
dnf install certbot -y

다음으로 다음 명령을 실행하여 Laravel 도메인용 Lets Encrypt SSL을 다운로드합니다.

certbot --nginx -d laravel.exampledomain.com

유효한 이메일을 제공하고 아래와 같이 서비스 약관에 동의하라는 메시지가 표시됩니다.

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for laravel.exampledomain.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/laravel.conf

다음으로 HTTP 트래픽을 HTTPS로 리디렉션할지 여부를 선택합니다.

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

2를 입력하고 Enter 키를 눌러 프로세스를 시작합니다. 인증서가 설치되면 다음 출력이 표시됩니다.

Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/laravel.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://laravel.exampledomain.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=laravel.exampledomain.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/laravel.exampledomain.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/laravel.exampledomain.com/privkey.pem
   Your cert will expire on 2022-04-11. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

 - We were unable to subscribe you the EFF mailing list because your
   e-mail address appears to be invalid. You can try again later by
   visiting https://act.eff.org.

이 시점에서 Laravel 웹사이트는 Lets Encrypt SSL로 보호됩니다. 이제 URL https://laravel.exampledomain.com을 사용하여 안전하게 액세스할 수 있습니다.

결론

축하합니다! Alma Linux 8에 Nginx 및 Lets Encrypt SSL과 함께 Laravel을 성공적으로 설치했습니다. 이제 Laravel 프레임워크를 사용하여 PHP 기반 애플리케이션 개발을 시작할 수 있습니다. 궁금한 점이 있으면 언제든지 문의해 주세요.