1. 程式人生 > 實用技巧 >LNMP架設動態網站

LNMP架設動態網站

1.安裝Nginx

1)使用Nginx官方的yum源

[root@localhost ~]# vim /etc/yum.repos.d/nginx.repo 
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

2)安裝Nginx

[root@localhost ~]# yum install nginx -y
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl enable nginx

2.使用第三方源安裝php7.2

1)移除舊版php

[root@localhost ~]# yum list installed | grep php
[root@localhost ~]# yum remove php*

2)安裝php源

[root@localhost ~]# yum install epel-release -y
[root@localhost ~]# yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y

3)安裝php-fpm

[root@localhost ~]# yum install php72w-fpm -y

4)安裝php的擴充套件

[root@localhost ~]# yum -y install  php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-pear php72w-devel

其他的擴充套件如下,按照自己的需求安裝

php72w-cli 
php72w-common 
php72w-devel 
php72w-embedded 
php72w-fpm 
php72w-gd 
php72w-mbstring 
php72w-mysqlnd 
php72w-opcache 
php72w-pdo 
php72w-xml 
php72w 
php72w-bcmath 
php72w-dba 
php72w-enchant 
php72w-imap 
php72w-interbase
php72w-intl 
php72w-ldap 
php72w-mcrypt 
php72w-odbc 
php72w-pdo_dblib 
php72w-pear 
php72w-pecl-apcu 
php72w-pecl-imagick 
php72w-pecl-xdebug 
php72w-pgsql 
php72w-phpdbg 
php72w-process 
php72w-pspell 
php72w-recode 
php72w-snmp 
php72w-soap 
php72w-tidy 
php72w-xmlrpc 
php72w-pecl-igbinary 
php72w-intl 
php72w-memcached 
php72w-pecl-mongodb

5)檢視是否成功安裝

[root@localhost ~]# php -v #檢視是否成功安裝
[root@localhost ~]# php -m #檢視所有的擴充套件

6)配置檔案所在的位置

php.ini                   /etc/php.ini
phpize                   /usr/bin/phpize
php-config             /usr/bin/php-config
php-fpm.conf         /etc/php-fpm.conf
php-fpm.pid           /var/run/php-fpm/php-fpm.pid

7)啟動php-fpm  

[root@localhost ~]# systemctl start php-fpm
[root@localhost ~]# systemctl enable php-fpm

3.安裝MariaDB

1)使用MariaDB官方的yum源

[root@localhost ~]# vim /etc/yum.repos.d/mariadb.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4.0/centos74-aarch64/ #這個連結換成你要的版本地址
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

2)安裝並啟動MariaDB

[root@localhost ~]# yum install mariadb-server -y
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# systemctl enable mariadb

4.配置LNMP架構

1)配置Nginx實現動態請求轉發至php

[root@localhost ~]# vim /etc/nginx/conf.d/php.conf 
server {
listen 80;
server_name default_server;
root /usr/share/nginx/html;
index index.php index.html;

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /soft/code$fastcgi_script_name;
include fastcgi_params;
}
}

2)新增php測試頁面

測試phpinfo

[root@nginx ~]# cat /usr/share/nginx/html/info.php
<?php
phpinfo();
?>

使用mysqli模組測試連線mysql

[root@nginx ~]# cat /usr/share/nginx/html/mysqli.php
<?php
$servername = "localhost";
$username = "root";
$password = "";

// 建立連線
$conn = mysqli_connect($servername, $username, $password);

// 檢測連線
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "連線成功";
?>

使用pdo模組測試連線mysql

[root@nginx ~]# cat /usr/share/nginx/html/mysqlpdo.php
<?php
$servername = "localhost";
$username = "root";
$password = "";

try {
$conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
echo "連線成功";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>

5.PHP配置檔案優化

1)php-ini優化

//開啟php的安全模式,控制php執行危險函式, 預設是Off,改為On
sql.safe_mode = Off
//關閉php頭部資訊, 隱藏版本號, 預設是On,該為Off
expose_php = On
//錯誤資訊輸出控制
display_error = Off
error_reporting = E_WARNING & E_ERROR
//記錄錯誤日誌至後臺, 方便追溯
log_errors = On
error_log = /var/log/php_error.log
//每個指令碼時間最大記憶體
memory_limit = 128M
//上傳檔案最大許可,預設2M, 建議調整為16,32M
upload_max_filesize = 2M
//禁止遠端執行phpshell,預設On, 建議Off
allow_url_fopen = On
//時區調整,預設PRC, 建議調整為Asia/Shanghai
date.timezone = PRC


//整體優化後配置檔案
sql.safe_mode = Off
expose_php = Off
display_error = Off
error_reporting = E_WARNING & E_ERROR
log_errors = On
error_log = /var/log/php_error.log
upload_max_filesize = 50M
allow_url_fopen = Off
date.timezone = Asia/Shanghai

2)php-fpm優化
PHP-FPM配置檔案 4核16G、8核16G

[root@localhost ~]# cat /etc/php-fpm.d/www.conf
[global]
pid = /var/run/php-fpm.pid
#php-fpm程式錯誤日誌
error_log = /var/log/php/php-fpm.log
log_level = warning
rlimit_files = 655350
events.mechanism = epoll

[www]
user = nginx
group = nginx
listen = 127.0.0.1:9000
listen.owner = www
listen.group = www
listen.mode = 0660

listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 512
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.process_idle_timeout = 15s;

pm.max_requests = 2048

#php-www模組錯誤日誌
php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/php/php-www.log
php_admin_flag[log_errors] = on

#php慢查詢日誌
request_slowlog_timeout = 5s
slowlog = /var/log/php/php-slow.log
PHP5-FPM配置詳解釋

[global]
#pid設定, 記錄程式啟動後pid
pid = /var/run/php-fpm.pid
#php-fpm程式啟動錯誤日誌路徑
error_log = /soft/log/php/php-fpm_error.log
# 錯誤級別. 可用級別為: alert(必須立即處理),error(錯誤情況), warning(警告情況), notice(一般重要資訊), debug(除錯資訊). 預設: notice.
log_level = warning
#設定檔案開啟描述符的rlimit限制.
rlimit_files = 65535
events.mechanism = epoll

#啟動程序的使用者和組
[www]
user = www
group = www

# fpm監聽埠
listen = 127.0.0.1:9000
# unix socket設定選項,如果使用tcp方式訪問,這裡註釋即可。
listen.owner = www
listen.group = www
# 允許訪問FastCGI程序的IP,any不限制
listen.allowed_clients = 127.0.0.1

# pm設定動態排程
pm = dynamic
# 同一時刻最大的php-fpm子程序數量
pm.max_children = 200
# 動態方式下的起始php-fpm程序數量
pm.start_servers = 20
# 動態方式下伺服器空閒時最小php-fpm程序數量
pm.min_spare_servers = 10
# 動態方式下伺服器空閒時最大php-fpm程序數量
pm.max_spare_servers = 30
# 最大請求
pm.max_requests = 1024
pm.process_idle_timeout = 15s;

# FPM狀態頁面,用於監控php-fpm狀態使用
pm.status_path = /status
# 錯誤日誌
php_flag[display_errors] = off
php_admin_value[error_log] = /soft/log/php/php-www_error.log
php_admin_flag[log_errors] = on

# 配置php慢查詢, 以及慢查詢記錄日誌位置
request_slowlog_timeout = 5s
slowlog = /soft/log/php/php-slow.log