溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

CentOS7中LNMP環(huán)境怎么搭建

發(fā)布時(shí)間:2022-04-14 14:23:28 來源:億速云 閱讀:144 作者:iii 欄目:編程語言

本文小編為大家詳細(xì)介紹“CentOS7中LNMP環(huán)境怎么搭建”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“CentOS7中LNMP環(huán)境怎么搭建”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。

進(jìn)入命令行后,先使用su命令切換到root權(quán)限。

首先配置防火墻

centos 7.0默認(rèn)使用的是firewall作為防火墻

1.關(guān)閉firewall:

systemctl stop firewalld.service #停止firewall 
systemctl disable firewalld.service #禁止firewall開機(jī)啟動(dòng)

2.關(guān)閉selinux:

vi /etc/selinux/config

#selinux=enforcing #注釋掉

selinux=disabled #增加

:wq! #保存退出

setenforce 0 #使配置立即生效

CentOS7中LNMP環(huán)境怎么搭建

3.安裝priorities與wget

yum install yum-priorities -y 
yum -y install wget

CentOS7中LNMP環(huán)境怎么搭建

1.安裝mysql

下載mysql源安裝包

復(fù)制代碼 代碼如下:

wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

安裝mysql源

復(fù)制代碼 代碼如下:

yum localinstall mysql57-community-release-el7-8.noarch.rpm

檢查mysql源是否安裝成功,注意命令里的點(diǎn)號(hào)。

復(fù)制代碼 代碼如下:

yum repolist enabled | grep "mysql.-community."

安裝mysql

復(fù)制代碼 代碼如下:

yum install mysql-community-server

啟動(dòng)mysql服務(wù),啟動(dòng)服務(wù)時(shí)可能會(huì)慢一些,因電腦配置各異。

復(fù)制代碼 代碼如下:

systemctl start mysqld

查看mysql的啟動(dòng)狀態(tài)

復(fù)制代碼 代碼如下:

systemctl status mysqld

CentOS7中LNMP環(huán)境怎么搭建

開機(jī)啟動(dòng)

systemctl enable mysqld 
systemctl daemon-reload

查看root本地登錄密碼(這條命令會(huì)查出mysql設(shè)置的默認(rèn)隨機(jī)密碼,如下圖,我的隨機(jī)密碼為t3e4woyyi=:y)

grep 'temporary password' /var/log/mysqld.log

CentOS7中LNMP環(huán)境怎么搭建

通過隨機(jī)密碼登陸mysql(隨機(jī)密碼比較難辨認(rèn),多幾次,我在登陸的時(shí)候就因?yàn)榭村e(cuò)密碼試了兩次才成功)

mysql -u root -p

CentOS7中LNMP環(huán)境怎么搭建

修改mysql登陸密碼(注意不要漏掉分號(hào),這是mysql的語句,修改完成后使用exit退出后再次登陸)

set password for 'root'@'localhost'="chen123456.";
exit;

注意:mysql5.7默認(rèn)安裝了密碼安全檢查插件(validate_password),默認(rèn)密碼檢查策略要求密碼必須包含:大小寫字母、數(shù)字和特殊符號(hào),并且長度不能少于8位。否則會(huì)提示error 1819 (hy000): your password does not satisfy the current policy requirements錯(cuò)誤,如下所示:

alter user ‘root'@'localhost' identified by ‘mynewpass4!';
set password for ‘root'@'localhost'=password(‘mynewpass4!');
通過msyql環(huán)境變量可以查看密碼策略的相關(guān)信息:

mysql> show variables like ‘%password%';

CentOS7中LNMP環(huán)境怎么搭建

如果上面的方式不能修改可以使用下面安全模式修改root:
關(guān)閉服務(wù)
systemctl stop mysqld.service
vi /etc/my.cnf
mysqld下面添加skip-grant-tables 保存退出啟動(dòng)服務(wù)
systemctl start mysqld.service
mysql -u root 不用密碼直接回車
use mysql
update user set authentication_string=password(‘root-123') where user='root'and host='localhost';
flush privileges;
exit;
vi /etc/my.cnf 把 skip-grant-tables 一句刪除保存退出重啟mysql服務(wù)
systemctl restart mysqld.service
再次登錄即可
mysql -u root -proot-123

如果進(jìn)行操作出現(xiàn)下面的提示:
you must reset your password using alter user statement before executing thisstatement.
就再設(shè)置一遍密碼
set password = password(‘root-123');

開放3306端口(允許使用用戶名root密碼root-123456從任何主機(jī)連接到mysql服務(wù)器

mysql>grant all on root.* to root@'%' identified by 'vmroot!@#456vmroot';
mysql>flush privileges;
mysql>exit;

開啟防火墻mysql 3306端口的外部訪問

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd--reload

配置默認(rèn)編碼為utf8

vi /etc/my.cnf

修改/etc/my.cnf配置文件,在[mysqld]下添加編碼配置,如下所示:

[mysqld]
character_set_server=utf8
init_connect='set names utf8'

默認(rèn)配置文件路徑:
配置文件:/etc/my.cnf
日志文件:/var/log//var/log/mysqld.log
服務(wù)啟動(dòng)腳本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid

CentOS7中LNMP環(huán)境怎么搭建

如果想使用防火墻,建議使用以下方法配置:

關(guān)閉firewall:

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機(jī)啟動(dòng)

安裝iptables防火墻:

yum install iptables-services #安裝
sudo vi /etc/sysconfig/iptables #編輯防火墻配置文件

配置文件更改如下:

# firewall configuration written by system-config-firewall
# manual customization of this file is not recommended.
*filter
:input accept [0:0]
:forward accept [0:0]
:output accept [0:0]
-a input -m state --state established,related -j accept
-a input -p icmp -j accept
-a input -i lo -j accept
-a input -m state --state new -m tcp -p tcp --dport 22 -j accept
 
//下面是編輯添加的部分
-a input -m state --state new -m tcp -p tcp --dport 80 -j accept
-a input -m state --state new -m tcp -p tcp --dport 3306 -j accept
//以上是編輯添加的部分
 
-a input -j reject --reject-with icmp-host-prohibited
-a forward -j reject --reject-with icmp-host-prohibited
commit

然后輸入:wq保存退出,在命令窗口輸入以下命令使其生效:

systemctl restart iptables.service #最后重啟防火墻使配置生效
systemctl enable iptables.service #設(shè)置防火墻開機(jī)啟動(dòng)

2、關(guān)閉selinux

命令行輸入以下內(nèi)容,打開selinux配置文件:

sudo vi /etc/selinux/config

修改內(nèi)容如下

#selinux=enforcing #注釋掉 
#selinuxtype=targeted #注釋掉 
selinux=disabled #增加

輸入:wq!#保存退出,然后命令行輸入以下內(nèi)容,使其生效

setenforce 0 #使配置立即生效

2.安裝php

yum默認(rèn)安裝的php版本較低,這次,我們準(zhǔn)備安裝php5.6版本,所以需要先安裝epel庫,然后安裝php。

yum install epel-release

rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum install --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-fpm php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-phpunit php-pecl-xdebug php-pecl-xhprof

安裝完成后鍵入php -v會(huì)顯示出php的版本,代表我們php安裝完成了。

php -v

CentOS7中LNMP環(huán)境怎么搭建

3.安裝nginx

復(fù)制代碼 代碼如下:

wget http://nginx.org/packages/centos/7/noarch/rpms/nginx-release-centos-7-0.el7.ngx.noarch.rpm
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm

然后啟動(dòng)nginx

systemctl start nginx.service #啟動(dòng)nginx 
systemctl stop nginx.service #停止 
systemctl restart nginx.service #重啟 
systemctl enable nginx.service #設(shè)置開機(jī)啟動(dòng)

.更改nginx端口號(hào)(根據(jù)自己需求)

cd /etc/nginx/conf.d/ 
vim default.conf 
把listen 80改成listen 81
然后重啟nginx
systemctl restart nginx.service #重啟nginx

CentOS7中LNMP環(huán)境怎么搭建

這時(shí)我們打開瀏覽器,訪問localhost如果出現(xiàn)welcome to nginx!那么nginx就安裝成功了

CentOS7中LNMP環(huán)境怎么搭建

nginx安裝完成了,那么該配置php-fpm了。讓nginx與php聯(lián)動(dòng)起來。

打開php-fpm配置文件

sudo vi /etc/php-fpm.d/www.conf

修改以下內(nèi)容(這里查找配置項(xiàng)時(shí),可以使用斜杠加要查找的關(guān)鍵字回車查找,如下圖所示)

listen.owner = nginx
listen.group = nginx
listen.mode = 0666

CentOS7中LNMP環(huán)境怎么搭建

最后,把三個(gè)參數(shù)修改完成后:wq退出然后重啟php-fpm服務(wù)

sudo systemctl start php-fpm #啟動(dòng)php-fpm
sudo systemctl enable php-fpm #開機(jī)啟動(dòng)fpm

然后,我們來修改nginx的配置,先使用find命令查找配置文件位置,我的配置文件位置如下圖

find / -name nginx.conf

CentOS7中LNMP環(huán)境怎么搭建

然后,使用vi 命令進(jìn)入查看,在最后一行發(fā)現(xiàn)這個(gè)配置文件又引入了其他配置文件。

vi /etc/nginx/nginx.conf

CentOS7中LNMP環(huán)境怎么搭建

再次進(jìn)入這個(gè)目錄發(fā)現(xiàn)配置文件如下圖

CentOS7中LNMP環(huán)境怎么搭建

使用vi命令修改它

vi default.conf

在localhost下加上同級(jí),如下圖所示

location ~ \.php$ {
root /var/www/html; #指定php的根目錄
fastcgi_pass 127.0.0.1:9000;#php-fpm的默認(rèn)端口是9000
fastcgi_index index.php;
fastcgi_param script_filename $document_root$fastcgi_script_name;
include fastcgi_params;
}

CentOS7中LNMP環(huán)境怎么搭建

修改保存之后,使用nginx -t命令確認(rèn)格式無錯(cuò)誤,后重啟nginx。如下圖所示

nginx -tnginx -s reload

之后,在剛剛設(shè)置的php目錄下,新建一個(gè)php文件用于測試。

在/var/www/html建立index.php

<?php

phpinfo();

CentOS7中LNMP環(huán)境怎么搭建

然后,我們訪問localhsot/index.php如果看到以下畫面,則說明我們的nginx php 已經(jīng)關(guān)聯(lián)上了。

CentOS7中LNMP環(huán)境怎么搭建

讀到這里,這篇“CentOS7中LNMP環(huán)境怎么搭建”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI