LNMP環境的搭建(yum)方法(精)


第一 先安裝nginx

nginx在官方CentOS社區yum里面沒有,需要在nginx的官方網站去下載yum的配置文件

官方:https://www.nginx.com/resources/wiki/start/topics/tutorials/install/

配置文件/etc/yum.repos.d/nginx.repo(還有一種,官方做了rpm的nginx yum repo,安裝rpm,就會在/etc/yum.repo.d/目錄下生產文件)

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/         $releasever 是linux 的版本號centos 7
gpgcheck=0
enabled=1

配置后查看 yum list nginx  ,就會發現有了nginx,然后我們就可以安裝了。

yum install nginx

看到nginx安裝成功,就是啟動它了, which nginx (rpm -ql nginx)可以看它的目錄

/etc/init.d/nginx start             //CentOS6     
 
/bin/systemctl start nginx         //CentOS7

//還可以在目錄文件中啟動
//加入開機啟動以后研究

驗證:輸入ip地址查看默認網頁

參考查看 : https://www.cnblogs.com/fps2tao/p/7577378.html

 

 

第二 安裝 php

(我是感覺安裝php會一塊安裝依賴把(apache)httpd也安裝上去,但是單純安裝(apache)httpd,再安裝php需要配置apache的配置文件支持php擴展)

如下:

編輯 /usr/local/apache2/conf/httpd.conf 文件時要注意:         

找到:    
AddType  application/x-compress .Z    
AddType application/x-gzip .gz .tgz    
在后面添加:    
AddType application/x-httpd-php .php(使Apcche支持PHP)    
AddType application/x-httpd-php-source .php5       

找到:    <IfModule dir_module>    DirectoryIndex index.html    </IfModule>    
添加:    <IfModule dir_module>    DirectoryIndex index.html index.php    </IfModule>        

找到:    #ServerName www.example.com:80    
修改為:  ServerName 127.0.0.1:80或者ServerName localhost:80    記得要去掉前面的“#”  


修改默認的Web站點目錄

找到:DocumentRoot "/usr/local/apache2/htdocs"
修改為:DocumentRoot "/home/www/WebSite" --該目錄為自己創建的目錄
 
找到:<Directory "/usr/local/apache2/htdocs"> 
修改為:<Directory "/home/www/WebSite">

直接安裝php這個配置可以不看

------

查看  yum list php  php-fmp

這里為啥要安裝php-fpm,因為php-fpm,是nginx和php的橋梁,php-fpm(快速進程管理),php-fpm默認進程為127.0.0.1:9000,一會php和php-fpm安裝完成后,要配置nginx的配置文件,讓其遇到客戶端php請求是,轉發給php-fpm(127.0.0.1:9000),php-fpm再讓php解析完成,最后又給nginx.

安裝:

yum install -y php php-fpm
yum install php php-pear php-devel httpd //可選,參數更新中 php-pear為php的擴展工具,安裝后可以用pecl install 命令安裝php擴展 

 

安裝成功后,然后在配置nginx的配置文件,讓它遇到php的時候轉發給php-fpm

    location ~ \.php$ {
        root           /usr/share/nginx/html;    //這個地方要改下下
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  // /script要改成 $document_root 及/usr/share/nginx/html路徑,后面是文件名,就是這個絕對路徑的文件名給php-fpm
        include        fastcgi_params;
    }

然后重啟nginx ,啟動 php-fpm 后用phpinfo()測試.

//CentOS7

/bin/systemctl restart nginx
/bin/systemctl start php-fpm

//CentOS6
/etc/init.d/nginx restart
/etc/init.d/php-fpm start

此時 nginx 和php 已經安裝成功,但是php還不能連接mysql,先別急往后看.

 

注:php7的安裝方法:

http://www.cnblogs.com/fps2tao/p/7577544.html

 

第三  安裝mysql

因為CentOS7現在已經 不支持 mysql了,取而代之的是mariadb.所以mysql 的yum repo,要在官網獲取.

網址 :https://dev.mysql.com/downloads/repo/yum/

安裝一個適合自己系統的mysql,下載的是rpm包,安裝后會在/etc/yum.repos.d/下面建立msyql的repo.( mysql-community.repo)

wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
rpm -ivh mysql57-community-release-el7-11.noarch.rpm

更新一下本地的yum庫

yum clean
yum makecache

安裝

yum install mysql mysql-server php-mysql

php-mysql是php連接數據庫的插件,不然php沒有連接mysql的接口(msyql可以正常啟動也沒辦法)。

安裝完成后啟動

#systemctl start mysqld

安裝完成之后會自動在log中生成連接的密碼

查看密碼:

[root@mysqlA ~]# cat /var/log/mysqld.log |grep password
2016-08-23T02:33:48.872073Z 1 [Note] A temporary password isgenerated for root@localhost: %IrczkB+J7Ez

你必須重新設置密碼才能執行語句

[root@mysqlA ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.14
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

 

修改密碼~

這個安裝后不用配置,直接重啟php-fpm.

測試

 

擴展內容:mysql 密碼修改

 https://www.cnblogs.com/fps2tao/p/9950989.html

ALTER user 'root'@'localhost' IDENTIFIED BY 'L123#@'

 

其實想要重置 5.7 的密碼很簡單,就一層窗戶紙:

1、修改/etc/my.cnf,在 [mysqld] 小節下添加一行:skip-grant-tables=1

這一行配置讓 mysqld 啟動時不對密碼進行驗證

2、重啟mysqld 服務:systemctl restart mysqld

3、使用 root 用戶登錄到 mysql:mysql -uroot 

4、切換到mysql數據庫,更新 user 表:

update user set authentication_string = password('123456'),password_expired = 'N', password_last_changed = now() where user = 'root';

在之前的版本中,密碼字段的字段名是 password,5.7版本改為了 authentication_string

 在修改了密碼后  刷新一下緩存

flush privileges;

5、退出 mysql,編輯 /etc/my.cnf 文件,刪除 skip-grant-tables=1的內容

6、重啟mysqld 服務,再用新密碼登錄即可

不然 下面這些遠程和 更改密碼的sql 都不能運行

mysql> -- 下面我們另外添加一個新的 root 用戶, 密碼為空, 只允許 192.168.1.100 連接

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.100' IDENTIFIED BY '' WITH GRANT OPTION;
mysql> flush privileges;
mysql> update user set password=PASSWORD('123456') where user='root';
mysql> flush privileges;

 

 mysql 8 用phpmyadmin登錄失敗的問題 : https://www.cnblogs.com/fps2tao/p/9953430.html 

 

一個服務器安裝的例子,nginx,php,mysql,redis

https://blog.csdn.net/qq_26245325/article/details/78916178

 

 

 

第四,如果yum形式安裝mariadb,數據庫呢 ( https://blog.csdn.net/qq_39719589/article/details/81835330 )

1) 刪除原有的mysql包

yum remove mysql mysql-server mysql-libs compat-mysql51

2)安裝 mariadb 

yum install mariadb-server mariadb 

3) mariadb數據庫的相關命令是:

systemctl start mariadb  #啟動MariaDB
systemctl stop mariadb  #停止MariaDB
systemctl restart mariadb  #重啟MariaDB
systemctl enable mariadb  #設置開機啟動

4)首次登錄到數據庫

mysql -uroot -p
登錄到MariaDB,此時root賬戶的密碼為空,直接回車即可,退出Mariadb,exit;即可。

5)進行MariaDB的相關簡單配置,使用

mysql_secure_installation

命令進行配置(先退出數據庫)。

 

首先是設置密碼,會提示先輸入密碼
 
Enter current password for root (enter for none):<–初次運行直接回車
 
設置密碼
 
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數據庫,回車
 
Reload privilege tables now? [Y/n] <– 是否重新加載權限表,回車

初始化MariaDB完成,直接登錄,成功。

6)配置MariaDB的字符集

使用vim /etc/my.cnf.d/server.cnf命令編輯server.cnf文件,在[mysqld]標簽下添加:

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

配置初始化完成,重啟Mariadb。

之后進入Mariadb,查看字符集。

show variables like "%character%";show variables like "%collation%";

7) 添加用戶,設置權限

創建用戶命令(用戶名,密碼請自行修改)

create user username@localhost identified by 'password';
授予外網登陸權限 
grant all privileges on *.* to username@'%' identified by 'password';

select host,user,password from user;

簡單的用戶和權限配置就完成了。

授予部分權限只需把all privileges改為select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file其中一部分即可。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM