Laravel là một khuôn khổ web PHP miễn phí và mã nguồn mở do Taylor Otwell tạo ra. Nó dựa trên Symfony và tuân theo mô hình kiến trúc model–view–controller. Nó được thiết kế để xây dựng các ứng dụng web cao cấp bằng cách sử dụng các cú pháp quan trọng và duyên dáng của nó. Nó có nhiều tính năng tích hợp giúp phát triển các ứng dụng web dễ dàng và nhanh hơn. Laravel trở nên phổ biến hơn sau khi phát hành phiên bản 3, bao gồm các tính năng tiện dụng, bao gồm dòng lệnh Artisan và cơ sở dữ liệu hỗ trợ, đồng thời giới thiệu một hệ thống đóng gói có tên là bundles.
Hướng dẫn này sẽ chỉ cho bạn cách cài đặt Laravel PHP Framework với máy chủ web Nginx trên Ubuntu 22.04.
Sau khi tất cả các gói được cài đặt, hãy xác minh phiên bản PHP bằng lệnh sau lệnh:
Bạn sẽ thấy kết quả sau:
Tiếp theo, cài đặt PHP Composer bằng lệnh sau:
Sau khi cài đặt Composer, bạn sẽ nhận được thông báo sau đầu ra:
Tiếp theo, xác minh phiên bản Composer bằng lệnh sau:
Bạn sẽ nhận được thông báo sau đầu ra:
Bạn sẽ nhận được thông tin sau đầu ra:
Tiếp theo, hãy thay đổi thư mục thành Laravel và khởi chạy Laravel bằng lệnh sau:
Nếu mọi thứ ổn, bạn sẽ nhận được thông báo sau đầu ra:
Nhấn CTRL+C để dừng Laravel. Tiếp theo, thay đổi quyền sở hữu và quyền của Laravel:
Thêm các dòng sau:
Lưu và đóng tệp khi bạn hoàn tất, sau đó xác minh Nginx xem có lỗi cú pháp nào không bằng lệnh sau:
Bạn sẽ nhận được thông báo sau đầu ra:
Tiếp theo, khởi động lại dịch vụ Nginx và PHP-FPM để áp dụng các thay đổi:
Bạn cũng có thể kiểm tra trạng thái của Nginx bằng cách sử dụng lệnh sau lệnh:
Bạn sẽ thấy kết quả sau:
data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20width=%22750%22%20height=%22375%22%3E%3C/svg%3E
Trước tiên, hãy cài đặt Certbot bằng lệnh sau lệnh:
Sau khi cài đặt hoàn tất, hãy chạy lệnh sau để cài đặt Let's Encrypt SSL trên trang web của bạn:
Bạn sẽ được yêu cầu cung cấp địa chỉ email hợp lệ và chấp nhận điều khoản dịch vụ như hiển thị bên dưới:
Tiếp theo, chọn có chuyển hướng lưu lượng HTTP sang HTTPS hay không như hiển thị bên dưới:
Nhập 2 và nhấn Enter để hoàn tất quá trình cài đặt. Bạn sẽ thấy kết quả sau:
Hướng dẫn này sẽ chỉ cho bạn cách cài đặt Laravel PHP Framework với máy chủ web Nginx trên Ubuntu 22.04.
Điều kiện tiên quyết
- Một máy chủ chạy Ubuntu 22.04.
- Một tên miền hợp lệ được trỏ bằng IP máy chủ của bạn.
- Một mật khẩu gốc được cấu hình trên máy chủ.
Cài đặt LEMP Server
Trước khi bắt đầu, bạn sẽ cần cài đặt máy chủ web Nginx, hệ thống cơ sở dữ liệu MariaDB, PHP và các phụ thuộc bắt buộc khác trên máy chủ của mình. Bạn có thể cài đặt tất cả chúng bằng cách chạy lệnh sau:
Mã:
apt install -y nginx mariadb-server php php-fpm php-common php-cli php-gd php-mysqlnd php-curl php-intl php-mbstring php-bcmath php-xml php-zip wget git
Mã:
php -v
Mã:
PHP 8.1.2 (cli) (built: Apr 7 2022 17:46:26) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies
Cài đặt PHP Composer
Composer là trình quản lý phụ thuộc cho PHP được sử dụng để quản lý phụ thuộc PHP. Để cài đặt Composer, bạn sẽ cần cài đặt gói curl vào máy chủ của mình.
Mã:
apt install -y curl
Mã:
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
Mã:
All settings correct for using Composer
Downloading...
Composer (version 2.3.5) successfully installed to: /usr/bin/composer
Use it: php /usr/bin/composer
Mã:
composer --version
Mã:
Composer version 2.3.5 2022-04-13 16:43:00
Cài đặt Laravel trên Ubuntu 22.04
Đầu tiên, hãy điều hướng đến thư mục gốc web Nginx và tải xuống phiên bản Laravel mới nhất bằng lệnh Composer:
Mã:
cd /var/www/html
composer create-project laravel/laravel laravel
Mã:
55 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.
Mã:
cd laravel
php artisan serve --host 0.0.0.0 --port 8000
Mã:
Starting Laravel development server: http://0.0.0.0:8000
[Sun May 22 08:17:45 2022] PHP 8.1.2 Development Server (http://0.0.0.0:8000) started
Mã:
chown -R www-data:www-data /var/www/html/laravel
chmod -R 0777 /var/www/html/laravel
Cấu hình Nginx cho Laravel
Tiếp theo, tạo tệp cấu hình máy chủ ảo Nginx cho Laravel bằng lệnh sau:
Mã:
nano /etc/nginx/conf.d/laravel.conf
Mã:
server { listen 80; server_name laravel.example.com; root /var/www/html/laravel/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/php8.1-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.(?!well-known).* { deny all; }
}
Mã:
nginx -t
Mã:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Mã:
systemctl restart php8.1-fpm nginx
Mã:
systemctl status nginx
Mã:
? nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2022-05-22 08:19:20 UTC; 17s ago Docs: man:nginx(8) Process: 16865 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 16866 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 16867 (nginx) Tasks: 2 (limit: 2292) Memory: 2.6M CPU: 33ms CGroup: /system.slice/nginx.service ??16867 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;" ??16868 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
May 22 08:19:20 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server...
May 22 08:19:20 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.
Truy cập Giao diện Web Laravel
Tại thời điểm này, Laravel đã được cài đặt và cấu hình với Nginx. Bây giờ bạn có thể truy cập Laravel Web UI bằng URL . Bạn sẽ thấy Laravel Dashboard trên trang sau:data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20width=%22750%22%20height=%22375%22%3E%3C/svg%3E
Bảo mật Laravel bằng Let's Encrypt
Tiếp theo, bạn sẽ cần cài đặt gói máy khách Certbot để cài đặt và quản lý Let's Encrypt SSL.Trước tiên, hãy cài đặt Certbot bằng lệnh sau lệnh:
Mã:
apt-get install certbot python3-certbot-nginx -y
Mã:
certbot --nginx -d laravel.example.com
Mã:
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): [emailprotected]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/laravel.conf
Mã:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
Mã:
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/laravel.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://laravel.example.com
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=laravel.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/laravel.example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/laravel.example.com/privkey.pem Your cert will expire on 2022-08-22. 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.