Centos7下如何搭建Laravel環(huán)境(非docker)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

之前一直用docker搭建服務(wù)器運行環(huán)境,最近有個朋友來找我在服務(wù)器上手工搭建個php環(huán)境,搞了大半天,快玩不轉(zhuǎn)了。

最近呢有個朋友找我?guī)兔Υ罱▊€PHP的環(huán)境,本來想讓他直接用docker的。但是他買的是聚石塔的服務(wù)器,那邊不給數(shù)據(jù)庫開外網(wǎng),只能內(nèi)網(wǎng)去訪問。用docker搭載宿主機的網(wǎng)絡(luò)去訪問的時候老是時不時的找不到地址,整個服務(wù)老是中斷,無奈只能遷出來,一個個的安裝重新搭建環(huán)境。我也是菜鳥一個折騰了半天總算搞定了。

準(zhǔn)備工作

更換阿里源

服務(wù)器安裝的centos7系統(tǒng),先來換下阿里源。

$ 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 社區(qū)打造,為 RHEL 及衍生發(fā)行版如 CentOS等提供高質(zhì)量軟件包的項目。就是一個高質(zhì)量的白嫖軟件源。

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

$ yum install epel-release

安裝REMI源

CentOS下除了EPEL源之外還有REMI的源,REMI源保證了軟件的最新版,但是不保證軟件是穩(wěn)定版。主要拿來安裝最新的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:    412remi-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

設(shè)置默認安裝的版本,這里我選擇了php7.2

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

安裝PHP擴展

# 查找php對應(yīng)版本的擴展,我這里是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 # 設(shè)置開機啟動 $ 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 查看下啟動狀態(tài)

配置文件

php-fpm.conf 文件默認在 /etc/php-fpm.conf

php.ini 文件默認在 /etc/php.ini

php.ini文件里有一些參數(shù)需要修改,編輯進入搜索 max 帶這個關(guān)鍵字的一些配置自己看描述修改吧,也就是一些限制內(nèi)存啦,文件大小啦,執(zhí)行時間啦之類的。

修改完 systemctl reload php-fpm 執(zhí)行下即可。

安裝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 # 設(shè)置nginx服務(wù)器開機自啟動 $ systemctl enable nginx.service # 檢查開機自動是否設(shè)置成功 $ systemctl list-dependencies | grep nginx

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

可以通過 service nginx status 查看nginx的狀態(tài)。

Nginx的默認配置目錄都在 /etc/nginx/ 下,默認錯誤日志在 /var/log/nginx/error.log,主要服務(wù)器配置文件在 /etc/nginx/conf.d/*.conf

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

server {     # 這里做了Https的相關(guān)配置     listen 443 ssl http2;     server_name your_server_name;     # 項目訪問根目錄     root /xxx/www/public;     # ssl證書相關(guān)     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;         # 添加幾條有關(guān)安全的響應(yīng)頭;與 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」是否存在,不存在則繼續(xù)嘗試「站點根目錄/index.htm」,以此類推...     index index.html index.htm index.php;     # 指定字符集為 UTF-8     charset utf-8;     # Laravel 默認重寫規(guī)則;刪除將導(dǎo)致 Laravel 路由失效且 Nginx 響應(yīng) 404。     location / {         try_files $uri $uri/ /index.php?$query_string;     }     # 關(guān)閉 [/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 服務(wù)地址,可以為 IP:端口,也可以為 Unix socket。         fastcgi_pass 127.0.0.1:9000;         # 配置 FastCGI 的主頁為 index.php。         fastcgi_index index.php;         # 配置 FastCGI 參數(shù) SCRIPT_FILENAME 為 $realpath_root$fastcgi_script_name。         fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;         # 引用更多默認的 FastCGI 參數(shù)。         include fastcgi_params;     }     # 通俗地說,以上配置將所有 URI 以 .php 結(jié)尾的請求,全部交給 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,其實是不需要安裝數(shù)據(jù)庫的了,不過還是簡單寫下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 # 設(shè)置mysqld服務(wù)開機自啟動 $ systemctl enable mysqld.service # 使用yum安裝Mysql,啟動會系統(tǒng)會自動生成一個隨機的數(shù)據(jù)庫密碼,需要修改下 # 查看mysql的隨機密碼 $ grep 'temporary password' /var/log/mysqld.log # 終端登錄Mysql $ mysql -hlocalhost -u root -p xxx # 修改密碼 $ ALTER USER 'root'@'localhost' IDENTIFIED BY 'Yourpassword';

Laravel權(quán)限

安裝完 Laravel 后,你可能需要給這兩個文件配置讀寫權(quán)限:storage 目錄和 bootstrap/cache 目錄應(yīng)該允許 Web 服務(wù)器寫入。

這里需要給PHP-FPM運行角色的權(quán)限。

$ 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 用戶在執(zhí)行,所以需要把上面2個目錄改成 apache(我也不知道這個鬼用戶那里創(chuàng)建來的,可能默認安裝php-fpm的時候創(chuàng)建的吧…)。

找到對應(yīng)的目錄

# 暴力一些 $ chown -R apache:apache /xxx/storage/$ chown -R apache:apache /xxx/bootstrap/cache/# 在給個755權(quán)限 $ chmod -R 755 /xxx/storage/$ chmod -R 755 /xxx/bootstrap/cache/

相關(guān)推薦:最新的五個Laravel視頻教程? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點贊10 分享