Benjaming 🔨

記錄自己的學習旅程

0%

Laravel CentOS 測試環境建置

此篇文章目的是想練習使用centOS7來架設laravel伺服器,網上查了很多文章但照做幾乎都失敗,這邊提供終於成功的方法。
(安裝系統部分省略)


安裝ssh
我是在本機使用vm架設,這邊安裝ssh方便我在本機遠端操作。

1
2
yum install openssh-server
vim /etc/ssh/sshd_config

將port設定為22,並將PermitRootLogin設為yes
(若是正式伺服器請不要設定,這邊為方便測試)

開啟ssh功能

1
service sshd start

關閉防火牆

1
2
systemctl stop firewalld
systemctl disable firewalld

此時在本機使用ssh連到虛擬機的ip應可以正常登入了

安裝EPEL

1
yum -y install epel-release

安裝nginx

1
2
3
4
5
6
yum -y install nginx
systemctl start nginx
systemctl enable nginx
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install -y php71w php71w-curl php71w-common php71w-cli php71w-mysql php71w-mbstring php71w-fpm php71w-xml php71w-pdo php71w-zip
vim /etc/php.ini

將php.ini中的 cgi.fix_pathinfo=0 這行註解取消

1
vim /etc/php-fpm.d/www.conf

設定修改以下參數:

1
2
3
4
5
6
7
8
9
10
11
12
user = nginx
group = nginx
listen = /run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

安裝php-fpm

1
2
3
systemctl start php-fpm
systemctl enable php-fpm
netstat -pl | grep php-fpm.sock

安裝MariaDB

1
2
3
4
yum -y install mariadb mariadb-server
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation

安裝composer

1
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/bin --filename=composer

配置nginx設定檔

1
2
3
mkdir -p /var/www/laravel
cd /etc/nginx
vim conf.d/laravel.conf

修改設定檔如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
server {
listen 8080;
listen [::]:8080 ipv6only=on;

# Log files for Debugging
access_log /var/log/nginx/laravel-access.log;
error_log /var/log/nginx/laravel-error.log;

# Webroot Directory for Laravel project
root /var/www/laravel/public;
index index.php index.html index.htm;

# Your Domain Name
server_name laravel.hakase-labs.co;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

# PHP-FPM Configuration Nginx
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

重新讀取設定檔 重啟nginx

1
2
nginx -t
systemctl restart nginx

配置SELinux

1
2
3
4
5
6
7
8
9
10
11
12
13
yum -y install policycoreutils-python
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/laravel(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/laravel/public(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/laravel/storage(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/laravel/app(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/laravel/bootstrap(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/laravel/config(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/laravel/database(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/laravel/resources(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/laravel/routes(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/laravel/vendor(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/laravel/tests(/.*)?'
restorecon -Rv '/var/www/laravel/'

此時在本機瀏覽器輸入瀏覽器ip port號(照我設定檔是設8080port)
若出現502 bad gateway error
可到php-fpm資料夾 將php-fpm.sock檔案給予權限(我是給777)