centos7 yum搭建lnmp環境及配置wordpress超詳細教程

yum安裝lnmp環境是最方便,最快捷的一種方法。源碼編譯安裝須要花費大量的人類時間,固然源碼編譯能夠個性化配置一些其它功能。目前來講,yum安裝基本知足咱們搭建web服務器的需求。php

本文是我根據近期學習的內容加上我本身的理解,整理出來的從yum搭建lnmp環境到網站配置的超詳細的教程。若有錯誤的地方請指出,不勝感激!html

 

lnmp環境分爲4部分,linux、nginx、mysql和php。mysql

 

軟件安裝

linux系統

採用vm虛擬機最小化安裝centos7.6,目前最新的centos系統。linux

nginx安裝

配置nginx源nginx

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

安裝nginx-1.16版本web

 yum install -y nginx redis

確認是否安裝:sql

yum install nginx,會默認建立nginx:nginx,組和帳戶。數據庫

文件默認位置:apache

  二進制文件在/usr/sbin/nginx
  配置文件夾在/etc/nginx/
  log文件夾在/var/log/nginx
yum安裝會自動建立/usr/lib/systemd/system/nginx.service,能夠用systemctl 啓動中止。

mysql安裝

配置mysql源

rpm -ivh  http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

安裝mysql5.7

 yum -y install mysql-community-server 

確認是否安裝:

 

php安裝

配置php源

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.0

yum install -y php70w-common php70w-fpm php70w-opcache php70w-gd php70w-mysqlnd php70w-mbstring php70w-pecl-redis php70w-pecl-memcached php70w-devel 

上面該命令便可安裝拓展:

php-api, php-bz2, php-calendar, php-ctype, php-curl, php-date, php-exif, php-fileinfo, php-filter, php-ftp, php-gettext, php-gmp, php-hash, php-iconv, php-json, php-libxml, php-openssl, php-pcre, php-pecl-Fileinfo, php-pecl-phar, php-pecl-zip, php-reflection, php-session, php-shmop, php-simplexml, php-sockets, php-spl, php-tokenizer, php-zend-abi, php-zip, php-zlib

若是要加裝其它php拓展(如xml),直接」yum install php70w-xml」便可,很是簡單

確認是否安裝:


查看安裝的php拓展

 1 [root@localhost ~]# php -m
 2 [PHP Modules]
 3 bz2
 4 calendar
 5 Core
 6 ctype
 7 curl
 8 date
 9 dom
10 exif
11 fileinfo
12 filter
13 ftp
14 gd
15 gettext
16 gmp
17 hash
18 iconv
19 igbinary
20 json
21 libxml
22 mbstring
23 memcached
24 mysqli
25 mysqlnd
26 openssl
27 pcntl
28 pcre
29 PDO
30 pdo_mysql
31 pdo_sqlite
32 Phar
33 posix
34 readline
35 redis
36 Reflection
37 session
38 shmop
39 SimpleXML
40 sockets
41 SPL
42 sqlite3
43 standard
44 sysvmsg
45 sysvsem
46 sysvshm
47 tokenizer
48 wddx
49 xml
50 xmlreader
51 xmlwriter
52 xsl
53 Zend OPcache
54 zip
55 zlib
56 
57 [Zend Modules]
58 Zend OPcache
View Code

至此,nginx、mysql、php安裝完成。下一步,配置。

 

軟件配置及wordpress安裝

假設咱們要搭建一個博客網站,wordpress。

nginx配置

首先看一下nginx配置文件夾

[root@localhost ~]# tree /etc/nginx
/etc/nginx
├── conf.d
│   └── default.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── modules -> ../../usr/lib64/nginx/modules
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf

再看一下nginx配置文件nginx.conf,其包含了conf.d文件夾內全部後綴是conf的配置文件。其實conf.d文件夾中以conf結尾的文件即爲網站的配置文件。

查看conf.d/default.conf默認配置文件。

每個網站目錄都須要一個」server「來引導。server_name爲要訪問的網址(即域名,後面創建wordpress配置會詳細講解),root即爲網站的默認目錄,index爲索引。

下面咱們來重建一個客戶端瀏覽器與web服務器交互的過程,來理解這些配置的做用。

瀏覽器裏面輸入「localhost」(即server_name,能夠用ip地址代替),瀏覽器會默認訪問其80端口,剛好web服務器nginx監聽的就是80端口(配置中的listen   80),這時候nginx會找到網站的默認目錄root,即/usr/share/nginx/html,而後尋找該目錄下的index索引文件,即index.html、index.htm,咱們來看一下/usr/share/nginx/html文件夾和index.html文件的內容:

index.html是一個html語言編寫的文件,能夠在網頁上顯示出來,啓動nginx, systemctl start nginx ,訪問localhost(或者ip),便可顯示出來index.html文件中的內容

ok,整個過程再捋一下:

客戶端瀏覽器訪問web服務器80端口,nginx監聽到80端口有訪問,根據訪問的地址(域名)找到網站的默認目錄(上文是/usr/share/nginx/html),而後訪問index定義的索引文件index.html,最後nginx將index.html文件的內容返回給瀏覽器,顯示出來。

以上就是一個簡單的web服務器工做的過程,若是不嫌麻煩,徹底能夠編寫一個個的html文件來當作我的博客,訪問按文件名訪問便可。

php配置

但單純地訪問靜態的html網頁實現的功能太少了,因此,噹噹噹當,php出場了!用php語言來編寫網頁。nginx收到用戶請求後,訪問網站所在目錄,看到其索引文件是php結尾的,index.php,而後將其轉交給php處理,php處理後返回給nginx,nginx再返回給用戶。

從wordpress官網上下載源碼,而後解壓,查看其文件:

[root@localhost ~]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz
[root@localhost ~]# tar xf latest-zh_CN.tar.gz 
[root@localhost ~]# ls wordpress/ index.php        wp-admin              wp-content         wp-load.php      wp-signup.php
license.txt      wp-blog-header.php    wp-cron.php        wp-login.php     wp-trackback.php
readme.html      wp-comments-post.php  wp-includes        wp-mail.php      xmlrpc.php
wp-activate.php  wp-config-sample.php  wp-links-opml.php  wp-settings.php

即該網站源碼的索引是index.php。

第一節安裝過程咱們單獨安裝了nginx與php,它們根本就不認識,怎麼交互?因此須要將nginx與php程序關聯起來,即改動default.conf文件中的有關fastcgi的項,把註釋去掉,而後把 /script 改爲 $document_root ,即

 

網站源碼,即上文的wordpress文件夾,放在任何位置均可以,但必須配置好權限,讓nginx所在的用戶組有讀寫權限。爲了方便,咱們將其放在/usr/share/html/目錄下,而且更改其用戶爲nginx

[root@localhost ~]# mv wordpress/ /usr/share/nginx/html/ 
[root@localhost ~]# cd /usr/share/nginx/html
[root@localhost html]# chown -R nginx:nginx wordpress/
[root@localhost html]# ll
total 12
-rw-r--r--. 1 root  root   494 Aug 13  2019 50x.html
-rw-r--r--. 1 root  root   612 Aug 13  2019 index.html
drwxr-xr-x. 5 nginx nginx 4096 Jul 29 10:00 wordpress

接下來配置網站的nginx配置文件。假設咱們註冊的網站域名爲domain.com,進入/etc/nginx/conf.d/文件夾,新建domain.com.conf文件(名稱隨意,但後綴必須爲conf,爲了方便區分使用「域名」+「.conf"),修改內容以下(能夠直接複製default.conf,而後修改):

注意幾個要修改的地方,server_name改成你註冊的域名(域名要解析到服務器的ip,一臺vps服務器上能夠有多個網站,不一樣網站就是用server_name來區分的),root即網站根目錄,定位到上文咱們複製到的地址,index裏添加index.php(wordpress目錄裏只有index.php,沒有index.html),還有下方的nginx與fastcgi關聯。

至此,網站的配置文件完成。下一步,配置mysql。

mysql配置

爲何須要數據庫?由於數據太多,查找、修改等都是麻煩事,因此咱們須要一個管理數據的軟件,即數據庫。mysql是一個關係型數據庫管理系統。

咱們今天的主角wordpress一樣也是須要數據庫的,接下來咱們用mysql建立一個數據庫。建立完,咱們確定須要訪問,訪問的話須要用戶權限,咱們還須要建立一個用戶(儘可能不要用mysql的root用戶去訪問,root的權限太大了)。

首先啓動mysql,

systemctl start mysqld

使用yum安裝完mysql以後,第一次會生成一個隨機密碼,在/var/log/mysqld.log中,能夠用該密碼登入

[root@localhost ~]# grep password /var/log/mysqld.log 
2019-08-02T19:27:52.679467Z 1 [Note] A temporary password is generated for root@localhost: Fu0fT!4b,4,b

而後登陸mysql,輸入上方的密碼(密碼不會顯示出來),回車

[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection
id is 2 Server version: 5.7.27 Copyright (c) 2000, 2019, 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>

修改root密碼:

mysql> set password for root@localhost = password('Mysql./root123');
Query OK, 0 rows affected, 1 warning (0.00 sec)

建立用戶wordpress_user:

mysql> CREATE USER 'wordpress_user'@'host' IDENTIFIED BY 'Mysql./user123';
Query OK, 0 rows affected (0.00 sec)

建立數據庫wordpress,而且將其全部權賦給用戶wordpress_user:

mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)

mysql> grant all privileges on wordpress.* to "wordpress_user"@"localhost" identified by "Mysql./user123";
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

從新啓動nginx、mysql、php-fpm

systemctl restart nginx    
systemctl restart mysqld
systemctl restart php-fpm

爲避免出現其它問題,先關閉防火牆和selinux,後續再進行配置:

systemctl stop firewalld
setenforce 0

在瀏覽器中輸入 domain.com (這是域名domain.com要換成你本身的域名,和上方conf配置中一致),即出現wordpress初始化界面:

點擊「如今就開始!」,填入上面mysql建立的用戶名和數據庫:

點擊「提交」,

報錯是由於,以前咱們設置網站文件夾wordpress的用戶組爲 nginx:nginx ,而yum安裝php的默認用戶組爲 apache:apache ,查看php-fpm配置文件 vim /etc/php-fpm.d/ww.conf ,能夠看到:

將apache改成nginx,保存退出,從新啓動php-fpm, systemctl restart php-fpm ,便可正常進入下一步,

輸入「站點標題」、「用戶名」、「密碼」、「電子郵件」,便可安裝。安裝完成,便可登陸後臺管理界面:

至此,mysql配置完成,wordpress博客網站配置完成!

總結:

使用yum安裝nginx、mysql、php,方便快捷,實測在1c/1g虛擬機上安裝只需5分鐘!

單獨安裝完nginx、php軟件後,須要配置nginx關聯php。

配置wordpress網站,網站源碼(即根目錄)能夠放置在任何位置,只要在nginx的conf配置文件裏定位到就能夠。nginx的主要配置文件是 nginx.conf ,它會包含(include)conf.d文件夾下面全部後綴是」.conf"的配置文件,本質上是包含描述網站具體配置的server { } 代碼。

目前全部web服務器lnmp,lamp等,其安裝方式,源碼編譯安裝、yum安裝、一鍵腳本安裝、面板安裝等,具體實現原理即上面講述的兩點:

  一、配置nginx(或lamp)關聯php;

  二、配置網站的conf配置文件。

相關文章
相關標籤/搜索