簡介
LNMP架構平臺介紹
L:Linux(平臺是linux)
N:Nginx(請求轉發,反向代理,處理靜態資源)
M:MySQL(存儲數據)
P:PHP(處理動態語言)
靜態頁面請求:client–>nginx(location)–>html–>client
動態頁面請求:client–>nginx(location)–>fastcgi(快速通用網關接口)–>php-fpm–>wrappe(真正執行者)–>php–>mysql–>php–>wrapper–>php-fpm–>fastcgi–>nginx–>client
MySQL
下載MySQL
在這裏下載包:link
我下載了5.7.30的安裝包
解壓後我們可以看到:
[root@localhost mysql-5.7.30]# ls
boost Docs libservices README testclients
BUILD Doxyfile-perfschema LICENSE regex unittest
client extra man scripts VERSION
cmake include mysql-test source_downloads vio
CMakeLists.txt INSTALL mysys sql win
cmd-line-utils libbinlogevents mysys_ssl sql-common zlib
config.h.cmake libbinlogstandalone packaging storage
configure.cmake libmysql plugin strings
dbug libmysqld rapid support-files
預編譯,編譯安裝
接下來我們按理說應該使用./configure來進行預編譯
但是mysql是通過cmake來進行預編譯
在企業中,我們爲了安全,會創建一個新的用戶進行對mysql的操作
useradd -s /sbin/nologin -M mysql
在cmake之前,我們先安裝幾個依賴包,解決依賴性問題,如果你已經有了這些包,就可以跳過這一步。如果還是缺少包,出問題後可以下載解決
yum install -y cmake gcc gcc-c++ ncurses-devel openssl-devel.x86_64
安裝cmake,進行預編譯:
yum install -y gcc gcc-c++ ncurses-devel bison cmake openssl-devel.x86_64
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql -DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_USER=mysql -DMYSQL_TCP_PROT=3306 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=boost/boost_1_59_0/
make && make install
配置
編譯安裝完成之後,會出現指定目錄,在/usr/local/lnmp/mysql
創建data目錄,並對其中的權限進行設置:
mkdir data
chown -R root . 讓mysql目錄下的所有目錄只有root用戶有權限更改
chown -R mysql data 讓剛纔建立mysql用戶可以向data目錄中寫入數據
ln -s /usr/local/lnmp/mysql/bin/* /usr/local/bin 做軟連接,可以直接mysql啓動
更改配置文件位置,以便修改配置:
它的配置文件在:
/usr/local/lnmp/mysql/mysql-test/include/default_my.cnf
備份mariadb數據庫配置文件:
mv /etc/my.cnf /etc/my.cnf.bak
把mysql的配置文件放到/etc/下:
cp default_my.cnf /etc/my.cnf
它的啓動腳本放在:
/usr/local/lnmp/mysql/support-files/mysql.server
方便起見,我們將啓動腳本挪出來,放在/etc/init.d下:
cp -a mysql.server /etc/init.d/mysqld ##就可以用systemctl的方式啓動了
給它執行權限:
chmod +x /etc/init.d/mysqld
配置開機啓動:
chkconfig mysqld on
啓動
先對mysql進行初始化,生成一些相應的表:
mysqld --user=mysql --initialize
在這一步會有一些warning並生成一個臨時密碼,記住這個臨時密碼,之後要用。
如果忘記了,戳:
啓動mysql:
方法一:
systemctl start mysqld.service
方法二:
/etc/init.d/mysqld start
登錄mysql,使用上面生成的密碼:
mysql -uroot -p
現在還不能去show databases,會報錯,提示重置密碼
我們進行一個安全初始化;
mysql_secure_installation
更改密碼,並進行一些簡單設置,就可以看到內容了:
[root@localhost init.d]# 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.30 Source distribution
Copyright (c) 2000, 2020, 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;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)
mysql>
PHP
下載
下載安裝包,我用7.4.1
解壓編譯
解壓:
[root@server1 /]# tar zxf php-7.4.1.tar.gz
預編譯,編譯安裝
預編譯:
cd php-7.4.1
./configure --prefix=/usr/local/lnmp/php \
--with-config-file-path=/usr/local/lnmp/php/etc \
--with-mysqli=/usr/local/lnmp/mysql/bin/mysql_config \
--enable-soap \
--enable-mbstring=all \
--enable-sockets \
--with-pdo-mysql=/usr/local/lnmp/mysql \
--enable-gd \
--without-pear \
--enable-fpm
發現在這個過程中,需要安裝一些包:
yum install -y oniguruma-5.9.5-3.el7.x86_64.rpm oniguruma-devel-5.9.5-3.el7.x86_64.rpm
yum install libxml2-devel.x86_64 sqlite-devel.x86_64 libpng-devel.x86_64 -y
預編譯完成後,顯示:
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
Thank you for using PHP.
編譯安裝:
make && make install
完成之後就可以在預定義的目錄中看到php目錄
[root@server1 php]# ls
bin etc include lib php sbin var
[root@server1 php]# pwd
/usr/local/lnmp/php
配置
拷貝一份默認的配置文件爲自己所用:
[root@server1 etc]# cp php-fpm.conf.default php-fpm.conf
[root@server1 etc]# pwd
/usr/local/lnmp/php/etc
打開裏面的pid進程:
[root@server1 etc]# vim php-fpm.conf
[global]
; Pid file
; Note: the default prefix is /usr/local/lnmp/php/var
; Default Value: none
pid = run/php-fpm.pid
進入php安裝目錄裏面的etc文件,同樣拷貝一份裏面的配置文件:
[root@server1 etc]# cd php-fpm.d/
[root@server1 php-fpm.d]# cp www.conf.default www.conf
[root@server1 php-fpm.d]# ls
www.conf www.conf.default
更改配置文件裏面的用戶和羣組,因爲php是處理nginx傳過來的請求的:
[root@server1 php-fpm.d]# vim www.conf ##配置配置文件
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = nginx
group = nginx
我們創建一個nginx用戶:
[root@server1 php-fpm.d]# useradd -s /sbin/nologin -M nginx
拷貝源碼包中的配置文件到php的安裝目錄:
[root@server1 php-7.4.1]# cp /php-7.4.1/php.ini-production /usr/local/lnmp/php/etc/php.ini
在這個配置目錄裏面我們只需要更改一下時區:
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Shanghai
啓動
配置啓動腳本:
把源碼包中的啓動腳本複製到/etc/init.d中去,並添加執行權限:
[root@server1 fpm]# cp /php-7.4.1/sapi/fpm/init.d.php-fpm /etc/init.d/
[root@server1 init.d]# chmod +x /etc/init.d/php-fpm
此時就可以使用執行腳本的方式啓動php:
[root@server1 init.d]# /etc/init.d/php-fpm start
Starting php-fpm done
檢查端口是否可以打開:
[root@server1 init.d]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 130654/php-fpm: mas
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN 8074/X
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 8137/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 7618/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 7611/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 8082/master
tcp6 0 0 :::3306 :::* LISTEN 11176/mysqld
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::6000 :::* LISTEN 8074/X
tcp6 0 0 :::22 :::* LISTEN 7618/sshd
tcp6 0 0 ::1:631 :::* LISTEN 7611/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 8082/master
3306和9000端口都已經開啓!
Nginx
下載
[root@server1 /]# tar zxf nginx-1.18.0.tar.gz
預編譯,編譯安裝
對Nginx進行簡單的編譯安裝:
./configure --prefix=/usr/local/lnmp/nginx \ ##安裝目錄
--with-http_ssl_module \
--with-http_stub_status_module \
--user=nginx --group=nginx ## 將它的用戶和用戶組設置爲nginx
make && make install
成功後會看到相應目錄:
[root@server1 lnmp]# cd nginx/
[root@server1 nginx]# pwd
/usr/local/lnmp/nginx
配置
修改配置文件:
[root@server1 conf]# pwd
/usr/local/lnmp/nginx/conf
[root@server1 conf]# vim nginx.conf
......
location / {
root html;
index index.php index.html index.htm;
}
......
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
連接到環境變量方便我們的使用:
[root@server1 conf]# ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/bin/
訪問過程是:
先訪問index.php,將動態訪問請求轉到本地9000(php-fpm)端口
啓動
檢查配置文件是否正確:
[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
打開nginx:
[root@server1 conf]# nginx
查看端口是否啓用:
[root@server1 conf]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 130654/php-fpm: mas
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3264/nginx: master
tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN 8074/X
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 8137/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 7618/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 7611/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 8082/master
tcp6 0 0 :::3306 :::* LISTEN 11176/mysqld
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::6000 :::* LISTEN 8074/X
tcp6 0 0 :::22 :::* LISTEN 7618/sshd
tcp6 0 0 ::1:631 :::* LISTEN 7611/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 8082/master
啓用成功!
測試訪問
關掉主機的防火牆
[root@server1 conf]# systemctl stop firewalld.service
網頁訪問主機ip:192.168.1.11
此時因爲我們沒有index.php頁面,所以它默認訪問的是index.html
我們可以寫一個index.php
[root@server1 html]# pwd
/usr/local/lnmp/nginx/html
重新加載nginx
[root@server1 html]# ps aux| grep nginx ##先關閉nginx進程
root 3264 0.0 0.0 45872 1148 ? Ss 13:52 0:00 nginx: master process nginx
nginx 3266 0.0 0.1 48404 2248 ? S 13:52 0:00 nginx: worker process
root 3559 0.0 0.0 112708 976 pts/2 S+ 14:04 0:00 grep --color=auto nginx
nginx 130655 0.0 0.2 196844 4000 ? S 13:19 0:00 php-fpm: pool www
nginx 130656 0.0 0.2 196844 4000 ? S 13:19 0:00 php-fpm: pool www
[root@server1 html]# kill 3264
[root@server1 html]# ps aux| grep nginx
root 3575 0.0 0.0 112708 976 pts/2 S+ 14:04 0:00 grep --color=auto nginx
nginx 130655 0.0 0.2 196844 4000 ? S 13:19 0:00 php-fpm: pool www
nginx 130656 0.0 0.2 196844 4000 ? S 13:19 0:00 php-fpm: pool www
[root@server1 html]# nginx ##重新開啓
在瀏覽器上面進行訪問:
到此爲止,php頁面就訪問成功了。
後記
1.安裝的nginx比較大
2.nginx版本號對外可見,並不安全
3.mysql並沒有使用到