Centos7下搭建Laravel環境(非docker)

reggie發表於2021-07-07

之前一直用docker搭建伺服器執行環境,最近有個朋友來找我在伺服器上手工搭建個PHP環境,搞了大半天,快玩不轉了。

最近呢有個朋友找我幫忙搭建個PHP的環境,本來想讓他直接用docker的。但是他買的是聚石塔的伺服器,那邊不給資料庫開外網,只能內網去訪問。用docker搭載宿主機的網路去訪問的時候老是時不時的找不到地址,整個服務老是中斷,無奈只能遷出來,一個個的安裝重新搭建環境。我也是菜鳥一個折騰了半天總算搞定了。

準備工作

更換阿里源

伺服器安裝的centos7系統,先來換下阿里源。

$ cd /etc/yum.repos.d/
$ cp CentOS-Base.repo CentOS-Base.repo.bak
$ wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
$ yum clean all
$ yum update
$ yum makecache
# 檢視已安裝的PHP,有的話rpm -e解除安裝
$ yum list installed | grep php

安裝EPEL

EPEL(Extra Packages for Enterprise Linux)是由 Fedora 社群打造,為 RHEL 及衍生發行版如 CentOS等提供高質量軟體包的專案。就是一個高質量的白嫖軟體源。

安裝後可以使用 yum install packageName,即可安裝很多以前需要編譯安裝的軟體、常用的軟體或一些比較流行的軟體,比如Nginx之類。

$ yum install epel-release

安裝REMI源

CentOS下除了EPEL源之外還有REMI的源,REMI源保證了軟體的最新版,但是不保證軟體是穩定版。主要拿來安裝最新的PHP版本。

$ rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm

安裝PHP7+

選擇PHP版本

先查詢下可以安裝的PHP版本

$ yum repolist all | grep php
 * remi-php72: mirrors.tuna.tsinghua.edu.cn
remi-php70                          Remi's PHP 7.0 RPM repositor disabled
remi-php70-debuginfo/x86_64         Remi's PHP 7.0 RPM repositor disabled
remi-php70-test                     Remi's PHP 7.0 test RPM repo disabled
remi-php70-test-debuginfo/x86_64    Remi's PHP 7.0 test RPM repo disabled
remi-php71                          Remi's PHP 7.1 RPM repositor disabled
remi-php71-debuginfo/x86_64         Remi's PHP 7.1 RPM repositor disabled
remi-php71-test                     Remi's PHP 7.1 test RPM repo disabled
remi-php71-test-debuginfo/x86_64    Remi's PHP 7.1 test RPM repo disabled
!remi-php72                         Remi's PHP 7.2 RPM repositor enabled:    412
remi-php72-debuginfo/x86_64         Remi's PHP 7.2 RPM repositor disabled
remi-php72-test                     Remi's PHP 7.2 test RPM repo disabled
remi-php72-test-debuginfo/x86_64    Remi's PHP 7.2 test RPM repo disabled
remi-php73                          Remi's PHP 7.3 RPM repositor disabled
remi-php73-debuginfo/x86_64         Remi's PHP 7.3 RPM repositor disabled
remi-php73-test                     Remi's PHP 7.3 test RPM repo disabled
remi-php73-test-debuginfo/x86_64    Remi's PHP 7.3 test RPM repo disabled
remi-php74                          Remi's PHP 7.4 RPM repositor disabled
remi-php74-debuginfo/x86_64         Remi's PHP 7.4 RPM repositor disabled
remi-php74-test                     Remi's PHP 7.4 test RPM repo disabled
remi-php74-test-debuginfo/x86_64    Remi's PHP 7.4 test RPM repo disabled
remi-php80                          Remi's PHP 8.0 RPM repositor disabled
remi-php80-debuginfo/x86_64         Remi's PHP 8.0 RPM repositor disabled
remi-php80-test                     Remi's PHP 8.0 test RPM repo disabled
remi-php80-test-debuginfo/x86_64    Remi's PHP 8.0 test RPM repo disabled

設定預設安裝的版本,這裡我選擇了php7.2

$ yum-config-manager --enable remi-php72
# 注意 如果提示 yum-config-manager 找不到的話需要先安裝下 yum-utils,在來執行上面的配置
$ yum -y install yum-utils
# 最後選擇自己想安裝的PHP版本即可
$ yum -y install php
$ php -v
$ php -m

安裝PHP擴充套件

# 查詢php對應版本的擴充套件,我這裡是7.2就寫的php72-php
$ yum search php72-php
 * base: mirrors.cloud.aliyuncs.com
 * extras: mirrors.cloud.aliyuncs.com
 * remi-php72: mirror.innosol.asia
 * remi-safe: mirror.innosol.asia
 * updates: mirrors.cloud.aliyuncs.com
============================================================================================================= N/S matched: php72-php ==============================================================================================================
php72-php-pecl-handlebars-devel.x86_64 : php72-php-pecl-handlebars developer files (header)
php72-php-pecl-http-message-devel.x86_64 : php72-php-pecl-http-message developer files (headers)
php72-php-pecl-propro-devel.x86_64 : php72-php-pecl-propro developer files (header)
php72-php-pecl-psr-devel.x86_64 : php72-php-pecl-psr developer files (header)
php72-php-pecl-raphf-devel.x86_64 : php72-php-pecl-raphf developer files (header)
php72-php-pecl-swoole-devel.x86_64 : php72-php-pecl-swoole developer files (header)
...
# 安裝擴充套件,注意副檔名不需要帶上PHP的版本號,下面是我在Laravel裡需要的一些擴充套件
$ yum -y install php-fpm php-bcmath php-mbstring php-mysqli php-mysqlnd php-pdo php-pdo_mysql php-posix php-shmop php-simplexml php-sodium php-xml php-xmlreader php-xmlwriter php-xsl php-zip php-opcache
# 然後檢視擴充套件是否安裝成功
$ php -m

PHP-FPM

# 啟動
$ systemctl start php-fpm
# 停止or重啟
$ systemctl stop php-fpm
$ systemctl restart php-fpm
# 過載
$ systemctl reload php-fpm
# 設定開機啟動
$ systemctl enable php-fpm
# 禁止開機啟動
$ systemctl disable php-pfm

最後檢視下PHP-FPM的程式

$ ps aux | grep php
root      1728  0.0  0.1 455280 12780 ?        Ss   Sep07   0:01 php-fpm: master process (/etc/php-fpm.conf)
apache    1998  0.0  0.6 558512 48084 ?        S    Sep07   0:15 php-fpm: pool www
apache    2873  0.0  0.4 542200 36012 ?        S    Sep07   0:19 php-fpm: pool www
apache    2874  0.0  0.3 536164 29212 ?        S    Sep07   0:19 php-fpm: pool www
apache    2875  0.0  0.4 542200 34832 ?        S    Sep07   0:21 php-fpm: pool www

也可以透過 service php-fpm status 檢視下啟動狀態

配置檔案

php-fpm.conf 檔案預設在 /etc/php-fpm.conf

php.ini 檔案預設在 /etc/php.ini

php.ini檔案裡有一些引數需要修改,編輯進入搜尋 max 帶這個關鍵字的一些配置自己看描述修改吧,也就是一些限制記憶體啦,檔案大小啦,執行時間啦之類的。

修改完 systemctl reload php-fpm 執行下即可。

安裝Nginx

# 安裝Nginx最新源
$ yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
$ yum repolist enabled | grep "nginx*"
# 安裝nginx
$ yum -y install nginx
# 啟動nginx
$ service nginx start
# 測試nginx配置檔案是否正常
$ nginx -t
# 平滑載入
$ nginx -s reload
# 設定nginx伺服器開機自啟動
$ systemctl enable nginx.service
# 檢查開機自動是否設定成功
$ systemctl list-dependencies | grep nginx

如果遇到nginx重啟之類提示說PID為空或者找不到的,可以看下是否有殘留進行殺掉,然後 nginx -c /etc/nginx/nginx.conf 指定好配置檔案。

可以透過 service nginx status 檢視nginx的狀態。

Nginx的預設配置目錄都在 /etc/nginx/ 下,預設錯誤日誌在 /var/log/nginx/error.log,主要伺服器配置檔案在 /etc/nginx/conf.d/*.conf

下面是一份簡單的Server配置, 做了Https的301 跳轉,不需要的話就把ssl相關部分去掉,301跳轉去掉即可。

server {
    # 這裡做了Https的相關配置
    listen 443 ssl http2;
    server_name your_server_name;
    # 專案訪問根目錄
    root /xxx/www/public;
    # ssl證照相關
    ssl_certificate  /xxx/certs/xxx.pem;
    ssl_certificate_key /xxx/certs/xxx.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;    
    # 新增幾條有關安全的響應頭;與 Google+ 的配置類似,詳情參見文末。
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
    # 站點預設頁面;可指定多個,將順序查詢。
    # 例如,訪問 http://example.com/ Nginx 將首先嚐試「站點根目錄/index.html」是否存在,不存在則繼續嘗試「站點根目錄/index.htm」,以此類推...
    index index.html index.htm index.php;
    # 指定字符集為 UTF-8
    charset utf-8;
    # Laravel 預設重寫規則;刪除將導致 Laravel 路由失效且 Nginx 響應 404。
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    # 關閉 [/favicon.ico][/robots.txt] 的訪問日誌。
    # 並且即使它們不存在,也不寫入錯誤日誌。
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    # 將 [404] 錯誤交給 [/index.php] 處理,表示由 Laravel 渲染美觀的錯誤頁面。
    error_page 404 /index.php;
    # URI 符合正規表示式 [\.php$] 的請求將進入此段配置
    location ~ \.php$ {
        # 配置 FastCGI 服務地址,可以為 IP:埠,也可以為 Unix socket。
        fastcgi_pass 127.0.0.1:9000;
        # 配置 FastCGI 的主頁為 index.php。
        fastcgi_index index.php;
        # 配置 FastCGI 引數 SCRIPT_FILENAME 為 $realpath_root$fastcgi_script_name。
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        # 引用更多預設的 FastCGI 引數。
        include fastcgi_params;
    }
    # 通俗地說,以上配置將所有 URI.php 結尾的請求,全部交給 PHP-FPM 處理。
    # 除符合正規表示式 [/\.(?!well-known).*] 之外的 URI,全部拒絕訪問
    # 也就是說,拒絕公開以 [.] 開頭的目錄,[.well-known] 除外
    location ~ /\.(?!well-known).* {
        deny all;
    }
}
server {
    listen 80;
    server_name your_server_name;
    return 301 https://your_server_name$request_uri;
}

最後重啟下nginx讓配置生效 nginx -s reload, 如果中間有問題記得檢視下nginx的錯誤日誌檔案,很有用,基本坑都會記錄在裡面 /var/log/nginx/error.log

安裝Mysql

因為朋友這邊是使用的聚石塔的RDS,其實是不需要安裝資料庫的了,不過還是簡單寫下Mysql的安裝把,這裡使用Mysql5.7為例,想要安裝Mysql8的自行換下。

# 安裝mysql源
$ yum -y localinstall  http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
$ yum repolist enabled | grep "mysql.*-community.*"
# 安裝Mysql
$ yum -y install mysql-community-server install mysql-community-devel
# 啟動mysql
$ service mysqld start
# 檢查mysql啟動是否正常
$ service mysqld status 或者 ps -ef | grep mysql
# 設定mysqld服務開機自啟動
$ systemctl enable mysqld.service
# 使用yum安裝Mysql,啟動會系統會自動生成一個隨機的資料庫密碼,需要修改下
# 檢視mysql的隨機密碼
$ grep 'temporary password' /var/log/mysqld.log
# 終端登入Mysql
$ mysql -hlocalhost -u root -p xxx
# 修改密碼
$ ALTER USER 'root'@'localhost' IDENTIFIED BY 'Yourpassword';

Laravel許可權

安裝完 Laravel 後,你可能需要給這兩個檔案配置讀寫許可權:storage 目錄和 bootstrap/cache 目錄應該允許 Web 伺服器寫入。

這裡需要給PHP-FPM執行角色的許可權。

$ ps aux | grep php-fpm
root      1728  0.0  0.1 455280 12780 ?        Ss   Sep07   0:01 php-fpm: master process (/etc/php-fpm.conf)
apache    1998  0.0  0.4 542128 31616 ?        S    Sep07   0:17 php-fpm: pool www
apache    2873  0.0  0.5 552440 46508 ?        S    Sep07   0:22 php-fpm: pool www
apache    2874  0.0  0.6 560740 52748 ?        S    Sep07   0:22 php-fpm: pool www
...

可以看到我這裡子程式是一個 apache 使用者在執行,所以需要把上面2個目錄改成 apache(我也不知道這個鬼使用者那裡建立來的,可能預設安裝php-fpm的時候建立的吧…)。

找到對應的目錄

# 暴力一些
$ chown -R apache:apache /xxx/storage/
$ chown -R apache:apache /xxx/bootstrap/cache/
# 在給個755許可權
$ chmod -R 755 /xxx/storage/
$ chmod -R 755 /xxx/bootstrap/cache/
本作品採用《CC 協議》,轉載必須註明作者和本文連結
微信公眾號:碼咚沒 ( ID: codingdongmei )

相關文章