LNMP 架設

基本環境

  1. CentOS 7

安裝

Nginx

1
2
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx

MariaDB

1
yum install mariadb-server mariadb

PHP-FPM

1
yum install php php-mysql php-fpm php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel

設定

PHP-FPM

開啟 cgi.fix_pathinfo=0

/etc/php.ini
1
cgi.fix_pathinfo=0

開啟 unix socket

/etc/php-fpm.d/www.conf
1
listen = /var/run/php-fpm/php-fpm.sock

Nginx 連接 PHP-FPM

/etc/nginx/conf.d/default.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
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;

location / {
index index.php index.html index.htm;

#===PHP===
location ~ [^/]\.php(/|$) {
include fastcgi_params;
fastcgi_index index.php;
if (!-f $request_filename) {
return 404;
}
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
}
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

Nginx 使用者網站

/etc/nginx/conf.d/default.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;

location / {
index index.php index.html index.htm;

# ===User Home===
location ~ ^/~(?<userdir_user>[\w-]+)(?<userdir_uri>/.*)?$ {
alias /home/$userdir_user/Sites$userdir_uri;

#===PHP===
location ~ [^/]\.php(/|$) {
include fastcgi_params;
fastcgi_index index.php;
if (!-f $request_filename) {
return 404;
}
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
}
}

#===PHP===
location ~ [^/]\.php(/|$) {
include fastcgi_params;
fastcgi_index index.php;
if (!-f $request_filename) {
return 404;
}
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
}
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

MariaDB 帳號密碼

1
/usr/bin/mysql_secure_installation

開啟防火牆http服務

1
2
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --reload

列出防火牆 public 的設定

1
firewall-cmd --list-all --zone=public


啟動

Nginx

1
2
systemctl start nginx
systemctl enable nginx

MariaDB

1
2
systemctl start mariadb
systemctl enable mariadb

PHP-FPM

1
2
systemctl start php-fpm
systemctl enable php-fpm