Centos7 搭配 LEMP(Nginx + MariaDB + PHP 7.1 + phpMyAdmin)
参考的文章
http://blog.itist.tw/2016/01/installing-lemp-stack-with-centos-7-nginx-mariadb-php-7.html
事前准备
加入 EPEL 套件庫
sudo yum -y install epel-release
Remi 套件庫
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
以及 Nginx 套件庫,复制下面到 nginx.repo
sudo vi /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1
更新yum
sudo yum -y update
安裝主要套件
安装php-fpm
sudo yum -y install nginx php71-php-fpm mariadb-server
開始設定及實測
先允許防火牆讓 HTTP、HTTPS 封包通過。
sudo firewall-cmd --permanent --zone=public --add-service=http sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reload
直接啟動服務,並讓它在開機後自動啟動。
sudo systemctl restart nginx sudo systemctl enable nginx
再修改預設主站台設定檔。
sudo vi /etc/nginx/conf.d/default.conf
server { listen 80; server_name localhost; charset utf-8; access_log /var/log/nginx/access.log main; root /usr/share/nginx/html; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { try_files $uri =404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
另外,因為 SELinux 會限制 Nginx 寫入動作,所以還要打開主目錄的存取權限。
sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html
最後,讓新的設定值生效。
sudo systemctl reload nginx
访问当前ip就可以访问了
PHP-FPM
首先修改一下 PHP 的主設定檔。
sudo vi /etc/opt/remi/php71/php.ini
找到 ;cgi.fix_pathinfo=1,將 1 改成 0,並把註解拿掉。
接著,修改 PHP-FPM 的主設定檔。
sudo vi /etc/opt/remi/php71/php-fpm.d/www.conf
配合 Nginx 修改下列設定值。
user = nginx group = nginx listen.owner = nobody listen.group = nobody
啟動 PHP-FPM,並讓它在開機後自動啟動。
sudo systemctl restart php71-php-fpm sudo systemctl enable php71-php-fpm
測試
sudo vi /usr/share/nginx/html/info.php
就加入這一行指令即可。
<?php phpinfo(); ?>
MariaDB安装
啟動 MariaDB Server,並讓它在開機後自動啟動。
sudo systemctl restart mariadb sudo systemctl enable mariadb
進行一些安全性的調整。
mysql_secure_installation
完成之後,試著連線到 MariaDB Server,用 root 帳號的新密碼登入測試一下。
mysql -u root -p
此外,為了讓 PHP 可以連接 MariaDB,要再額外安裝 PHP 的模組。
sudo yum -y install php71-php-mysqlnd
裝好之後,重新啟動 PHP-FPM。
sudo systemctl restart php71-php-fpm
重新打开info.php,查看mysql模块是否成功加载
安裝 phpMyAdmin
phpMyAdmin 會使用到 php-mcrypt 這個加密套件,如未安裝須安裝
安装 php-mcrypt
yum search php-mcrypt (选择自己正确的版本)
安装PHPMyAdmin(本人建议到官网下载放进访问的目录)
sudo pacman -S phpmyadmin
修改 phpMyAdmin 認證方式:
[smalljacky@alarmpi ~]$ sudo vi /usr/share/webapps/phpMyAdmin/config.inc.php # 預設為 cookie $cfg['Servers'][$i]['auth_type'] = 'http';
重啟 Nginx:
[smalljacky@alarmpi ~]$ sudo systemctl restart nginx.service
输入 IP/phpMyAdmin 就能成功访问。
如果浏览器打开出现如下错误:
session_start(): open(SESSION_FILE, O_RDWR) failed: Permission de
则修改报错信息文件的所有权,如下
chown nginx:nginx /var/lib/php/session