目錄:
1)LNMP簡介
2)安裝nginx1.4
3)安裝php7.2
4)安裝mariadb10.2
5)總結(jié)
一、LNMP簡介
L:linux,是目前最流行的免費操作系統(tǒng),版本有很多,rehat,debian,ubuntu,centos等等,我用的是centos7.5。
N:nginx,是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP代理服務器。nginx市場份額越來越大,這里也可以選擇Apache,老大哥,寶刀未老。
M:Mysql是一個小型關系型數(shù)據(jù)庫管理系統(tǒng),我覺得已經(jīng)不小了,對于絕大多數(shù)的情況,夠用了,這里我用的mariadb10.2,mariadb和mysql都是出自同個作者,這里不講它們的故事。
P:世界上最好的編程語言,一種在服務器端執(zhí)行的嵌入HTML文檔的腳本語言。
linux+nginx+mysql/mariadb+php,這四個開源項目組在一起,成為一個免費、高效、擴展性強的網(wǎng)站服務系統(tǒng),這是一個網(wǎng)站服務器架構,學會它就能打通任督二脈,無忌,跟著為師走。
二、安裝nginx
- 防火墻設置,允許http,https通信。
firewall-cmd --zone=public --add-service=http --permanent //允許http通信
firewall-cmd --zone=public --add-service=https --permanent //允許https通信
firewall-cmd --zone-public --add-port=80/tcp --permanent 打開80端口
firewall-cmd --zone-public --add-port=443/tcp --permanent 打開443端口
firewall-cmd --zone-public --add-port=8080/tcp --permanent 打開8080端口
firewall-cmd --reload //重新加載配置
- 下載基本的庫文件
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl-devel
- 配置nginx官方源,下載和安裝
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
//安裝nginx
yum install nginx
- 了解基本配置文件
主配置文件:/etc/nginx/nginx.conf
默認配置文件:/etc/nginx/conf.d/default.conf
我們編輯一下default.conf這個文件。
注意,這里server_name 我寫的是我這臺電腦的局域網(wǎng)地址及訪問端口,你可以設置其它或者默認不變
簡單設置
- 局域網(wǎng)訪問測試一下
systemctl start nginx
到這里nginx安裝使用成功,如果你這里無法訪問,絕大多數(shù)情況下是SELINUX和防火墻的問題。注意,如果你是云服務器的話是需要在服務商后臺那里添加安全組策略的。
三、安裝php7.2
- 配置php7.2yum源
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
- 安裝php7.2及一些擴展工具
yum install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml
- 啟動php-fpm
systemctl start php-fpm //啟動
systemctl enable php-fpm //設置開機啟動
注意,nginx是通過php-fpm處理php文件的。
- 讓nginx支持php
vim /etc/nginx/conf.d/default.conf
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
systemctl restart nginx //重啟服務

- 運行一個php文件
vim /usr/share/nginx/html/index.php //編輯文件
代碼如下:
<?php
phpinfo();
?>
保存,在瀏覽器訪問試一下。
四、安裝數(shù)據(jù)庫mariadb10.2
- 卸載系統(tǒng)自帶的數(shù)據(jù)庫
首先,centos7.5里面自帶的mariadb5.5版本的,所以第一步,我先卸載掉很老老老老老老的版本。
rpm -qa|grep mariadb
yum remove mariadb-*文件
它會刪除一些依賴文件,不用擔心。
- 配置mariadb10.2yum源
vim /etc/yum.repos.d/MariaDB.repo
# MariaDB 10.2 CentOS repository list - created 2019-01-08 08:30 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
以上配置的源是mariadb官方源,下載速度,一個星期的時間是完全可以下載下來的。
如果你想一根煙的時間就能完成下載,替換為國內(nèi)源:
[mariadb]
name = MariaDB
baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.2/centos7-amd64
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1
保存退出。
yum clean&&yum update //更新一下yum源
yum install MariaDB-client MariaDB-server //安裝
systemctl start mariadb
systemctl enable mariadb //設置開機啟動
OK,mariadb安裝完成。
- 初始化mariadb
首先使用mysql_secure_installation命令進行配置。
根據(jù)自己的要求配置,可以按照我的圖片走。
Set root password? [Y/n] <– 是否設置root用戶密碼,輸入y并回車或直接回車
New password: <– 設置root用戶的密碼
Re-enter new password: <– 再輸入一次你設置的密碼
其他配置
Remove anonymous users? [Y/n] <– 是否刪除匿名用戶,回車
Disallow root login remotely? [Y/n] <–是否禁止root遠程登錄,回車,
Remove test database and access to it? [Y/n] <– 是否刪除test數(shù)據(jù)庫,回車
Reload privilege tables now? [Y/n] <– 是否重新加載權限表,回車

登錄
mysql -uroot -p //本地登錄

- 簡單配置mariadb
先配置服務端,配置文件:/etc/my.cnf.d/server.cnf
vim /etc/my.cnf.d/server.cnf
在[mysqld]標簽下添加以下內(nèi)容
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
保存退出
再配置客戶端,配置文件:/etc/my.cnf.d/mysql-clients.cnf
vim /etc/my.cnf.d/mysql-clients.cnf
在[mysql]中添加
default-character-set=utf8
保存退出
重啟mariadb服務
systemctl restart mariadb
之后登錄mariadb,輸入下面代碼,查看一下我們的設置有沒有成功。
show variables like "%character%";show variables like "%collation%";

- 測試mariadb
現(xiàn)在nginx,php,mariadb都有了,我們來做個簡單的測試,寫個簡單的代碼,訪問數(shù)據(jù)庫。
vim /usr/share/nginx/html/index.php
代碼:
<?php
$link=mysqli_connect("localhost","賬號","密碼","數(shù)據(jù)庫");
if(!$link){
echo "Game Over." .PHP_EOL;
echo "Debugging errno:" .mysqli_connect_errno() .PHP_EOL;
echo "Debugging error:" .mysqli_connect_error() .PHP_EOL;
exit;
}
else{
echo "Very Good" .PHP_EOL;
echo "Host information:" .mysqli_get_host_info($link) .PHP_EOL;
}
mysqli_close($link);
?>
我們在瀏覽器訪問看看。
有些朋友寫的不是localhost,而是127.0.0.1,可能會出現(xiàn)Permission denied權限問題,如下圖。
這是因為selinux攔截了,我們設置以下就可以。
setsebool httpd_can_network_connect_db = on
用localhost比127.0.0.1在安全及性能上都要好,感興趣的朋友可以閱讀相關文檔。
五、總結(jié)
到這里,LNMP就搭建成功了,我第一次配置的時候遇到很多權限問題,其實也是好事。從問出現(xiàn),到分析問題,再到解決問題,你會收獲很多知識,那些問題讓你的腦袋不斷的思考,最后,你打通了任督二脈。
