上一篇博客我們在虛擬機上安裝了centos7,接下來,就開始安裝lnmp環(huán)境吧。
還是跟之前一樣,進入命令行后,先使用su命令切換到root權(quán)限。
首先配置防火墻
centos 7.0默認使用的是firewall作為防火墻
1.關(guān)閉firewall:
1
2
|
systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall開機啟動 |
2.關(guān)閉selinux:
1
|
vi /etc/selinux/config |
#selinux=enforcing #注釋掉
selinux=disabled #增加
:wq! #保存退出
setenforce 0 #使配置立即生效
3.安裝priorities與wget
1
2
|
yum install yum-priorities -y yum -y install wget |
1.安裝mysql
下載mysql源安裝包
安裝mysql源
檢查mysql源是否安裝成功,注意命令里的點號。
安裝mysql
啟動mysql服務(wù),啟動服務(wù)時可能會慢一些,因電腦配置各異。
查看mysql的啟動狀態(tài)
開機啟動
1
2
|
systemctl enable mysqld systemctl daemon-reload |
查看root本地登錄密碼(這條命令會查出mysql設(shè)置的默認隨機密碼,如下圖,我的隨機密碼為t3e4woyyi=:y)
1
|
grep 'temporary password' /var/log/mysqld .log |
通過隨機密碼登陸mysql(隨機密碼比較難辨認,多幾次,我在登陸的時候就因為看錯密碼試了兩次才成功)
1
|
mysql -u root -p |
修改mysql登陸密碼(注意不要漏掉分號,這是mysql的語句,修改完成后使用exit退出后再次登陸)
1
2
|
set password for 'root' @ 'localhost' = "chen123456." ; exit ; |
注意:mysql5.7默認安裝了密碼安全檢查插件(validate_password),默認密碼檢查策略要求密碼必須包含:大小寫字母、數(shù)字和特殊符號,并且長度不能少于8位。否則會提示error 1819 (hy000): your password does not satisfy the current policy requirements錯誤,如下所示:
alter user ‘root'@'localhost' identified by ‘mynewpass4!';
set password for ‘root'@'localhost'=password(‘mynewpass4!');
通過msyql環(huán)境變量可以查看密碼策略的相關(guān)信息:
1
|
mysql> show variables like ‘%password%'; |
如果上面的方式不能修改可以使用下面安全模式修改root:
關(guān)閉服務(wù)
systemctl stop mysqld.service
vi /etc/my.cnf
mysqld下面添加skip-grant-tables 保存退出啟動服務(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
如果進行操作出現(xiàn)下面的提示:
you must reset your password using alter user statement before executing thisstatement.
就再設(shè)置一遍密碼
set password = password(‘root-123');
開放3306端口(允許使用用戶名root密碼root-123456從任何主機連接到mysql服務(wù)器)
1
2
3
|
mysql>grant all on root.* to root@ '%' identified by 'vmroot!@#456vmroot' ; mysql>flush privileges; mysql> exit ; |
開啟防火墻mysql 3306端口的外部訪問
1
2
|
firewall-cmd --zone=public --add-port=3306 /tcp --permanent firewall-cmd--reload |
配置默認編碼為utf8
1
|
vi /etc/my .cnf |
修改/etc/my.cnf配置文件,在[mysqld]下添加編碼配置,如下所示:
1
2
3
|
[mysqld] character_set_server=utf8 init_connect= 'set names utf8' |
默認配置文件路徑:
配置文件:/etc/my.cnf
日志文件:/var/log//var/log/mysqld.log
服務(wù)啟動腳本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid
如果想使用防火墻,建議使用以下方法配置:
關(guān)閉firewall:
1
2
|
systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall開機啟動 |
安裝iptables防火墻:
1
2
|
yum install iptables-services #安裝 sudo vi /etc/sysconfig/iptables #編輯防火墻配置文件 |
配置文件更改如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# 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保存退出,在命令窗口輸入以下命令使其生效:
1
2
|
systemctl restart iptables.service #最后重啟防火墻使配置生效 systemctl enable iptables.service #設(shè)置防火墻開機啟動 |
2、關(guān)閉selinux
命令行輸入以下內(nèi)容,打開selinux配置文件:
1
|
sudo vi /etc/selinux/config |
修改內(nèi)容如下
1
2
3
|
#selinux=enforcing #注釋掉 #selinuxtype=targeted #注釋掉 selinux=disabled #增加 |
輸入:wq!#保存退出,然后命令行輸入以下內(nèi)容,使其生效
1
|
setenforce 0 #使配置立即生效 |
2.安裝php
yum默認安裝的php版本較低,這次,我們準備安裝php5.6版本,所以需要先安裝epel庫,然后安裝php。
1
2
3
4
|
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會顯示出php的版本,代表我們php安裝完成了。
1
|
php - v |
3.安裝nginx
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
然后啟動nginx
1
2
3
4
|
systemctl start nginx.service #啟動nginx systemctl stop nginx.service #停止 systemctl restart nginx.service #重啟 systemctl enable nginx.service #設(shè)置開機啟動 |
.更改nginx端口號(根據(jù)自己需求)
cd /etc/nginx/conf.d/
vim default.conf
把listen 80改成listen 81
然后重啟nginx
systemctl restart nginx.service #重啟nginx
這時我們打開瀏覽器,訪問localhost如果出現(xiàn)welcome to nginx!那么nginx就安裝成功了
nginx安裝完成了,那么該配置php-fpm了。讓nginx與php聯(lián)動起來。
打開php-fpm配置文件
1
|
sudo vi /etc/php-fpm .d /www .conf |
修改以下內(nèi)容(這里查找配置項時,可以使用斜杠加要查找的關(guān)鍵字回車查找,如下圖所示)
1
2
3
|
listen.owner = nginx listen.group = nginx listen.mode = 0666 |
最后,把三個參數(shù)修改完成后:wq退出然后重啟php-fpm服務(wù)
1
2
|
sudo systemctl start php-fpm #啟動php-fpm sudo systemctl enable php-fpm #開機啟動fpm |
然后,我們來修改nginx的配置,先使用find命令查找配置文件位置,我的配置文件位置如下圖
1
|
find / -name nginx.conf |
然后,使用vi 命令進入查看,在最后一行發(fā)現(xiàn)這個配置文件又引入了其他配置文件。
1
|
vi /etc/nginx/nginx .conf |
再次進入這個目錄發(fā)現(xiàn)配置文件如下圖
使用vi命令修改它
1
|
vi default.conf |
在localhost下加上同級,如下圖所示
1
2
3
4
5
6
7
|
location ~ \.php$ { root /var/www/html ; #指定php的根目錄 fastcgi_pass 127.0.0.1:9000; #php-fpm的默認端口是9000 fastcgi_index index.php; fastcgi_param script_filename $document_root$fastcgi_script_name; include fastcgi_params; } |
修改保存之后,使用nginx -t命令確認格式無錯誤,后重啟nginx。如下圖所示
1
|
nginx -tnginx -s reload |
之后,在剛剛設(shè)置的php目錄下,新建一個php文件用于測試。
在/var/www/html建立index.php
<?php
phpinfo();
然后,我們訪問localhsot/index.php如果看到以下畫面,則說明我們的nginx php 已經(jīng)關(guān)聯(lián)上了。
至此,lnmp已經(jīng)按裝完成,這篇博客的篇幅已經(jīng)夠長了,下篇博客,我們再來安裝phpmyadmin。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。