1. 程式人生 > >LNMP架構介紹,MySQL與PHP安裝,Nginx介紹

LNMP架構介紹,MySQL與PHP安裝,Nginx介紹

LNMP架構介紹

  • 和LAMP不同的是,提供web服務的是Nginx
  • 並且php是作為一個獨立服務存在的,這個服務叫做php-fpm
  • Nginx直接處理靜態請求,動態請求會轉發給php-fpm

MySQL重新安裝

  • MySQL是通過免編譯二進位制安裝包進行安裝的,需要刪除對應的目錄後進行重灌,重灌之前要先停止服務
[[email protected] ~]# ps -ef | grep mysql
root      1449     1  0 14:35 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/usr/local/mysql/mysqld.pid
mysql     1930  1449  0 14:35 ?        00:00:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/db.err --pid-file=/usr/local/mysql/mysqld.pid --socket=/usr/local/mysql/mysql.sock --port=3306
root      2496  2430  0 15:09 pts/0    00:00:00 grep --color=auto mysql
[
[email protected]
~]# service mysqld stop Shutting down MySQL.. SUCCESS! [[email protected] ~]# rm -rf /usr/local/mysql # 刪除MySQL安裝目錄 [[email protected] ~]# rm -rf /etc/init.d/mysqld # 刪除MySQL服務指令碼 [[email protected] ~]# rm /data/mysql/* -rf # 清空MySQL資料
  • 解壓包,重新安裝
[[email protected]
~]# cd /usr/local/src/ [[email protected] src]# tar -zxvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz [[email protected] src]# mv mysql-5.7.23-linux-glibc2.12-x86_64 /usr/local/mysql [[email protected] src]# ls /usr/local/mysql bin COPYING docs include lib man README share support-files [
[email protected]
src]# cd /usr/local/mysql [[email protected] mysql]# id mysql # 需要mysql使用者,檢視是否存在 uid=1007(mysql) gid=1008(mysql) groups=1008(mysql) [[email protected] mysql]# ./bin/mysqld --initialize --user=mysql --datadir=/data/mysql #初始化,生成MySQL啟動需要的一些目錄和檔案 2018-11-25T07:27:03.963759Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2018-11-25T07:27:03.986922Z 0 [ERROR] Could not open file '/usr/local/mysql/db.err' for error logging: Permission denied 2018-11-25T07:27:03.986975Z 0 [ERROR] Aborting [[email protected] mysql]# touch /usr/local/mysql/db.err [[email protected] mysql]# chown mysql:mysql db.err [[email protected] mysql]# ./bin/mysqld --initialize --user=mysql --datadir=/data/mysql # 這次生成的臨時密碼在db.err檔案中 [[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld # 拷貝啟動指令碼 [[email protected] mysql]# vim /etc/init.d/mysqld # 編輯啟動指令碼,設定basedir和datadir basedir=/usr/local/mysql datadir=/data/mysql # 啟動 [[email protected] mysql]# /etc/init.d/mysqld start Starting MySQL...... ERROR! The server quit without updating PID file (/usr/local/mysql/mysqld.pid). [[email protected] mysql]# chown mysql:mysql -R . [[email protected] mysql]# [[email protected] mysql]# /etc/init.d/mysqld start Starting MySQL.. SUCCESS! #設定開機啟動 [[email protected] mysql]# chkconfig --add mysqld [[email protected] mysql]# chkconfig mysqld on

PHP安裝

[[email protected] mysql]# cd /usr/local/src/php-5.6.32/
[[email protected] php-5.6.32]# make clean  # 刪除之前編譯過的檔案
find . -name \*.gcno -o -name \*.gcda | xargs rm -f
find . -name \*.lo -o -name \*.o | xargs rm -f
find . -name \*.la -o -name \*.a | xargs rm -f
find . -name \*.so | xargs rm -f
find . -name .libs -a -type d|xargs rm -rf
rm -f libphp5.la sapi/cli/php sapi/cgi/php-cgi    libphp5.la modules/* libs/*

[[email protected] php-5.6.32]# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl  --with-openssl

...
checking whether to enable ctype functions... yes
checking for cURL support... yes
checking for cURL in default path... not found
configure: error: Please reinstall the libcurl distribution -
    easy.h should be in <curl-dir>/include/curl/

# 報錯,安裝curl
[[email protected] php-5.6.32]# yum install -y libcurl-devel
# 再次初始化
[[email protected] php-5.6.32]# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl  --with-openssl  

...
Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

# 編譯安裝
[[email protected] php-5.6.32]# make && make install
...
[PEAR] XML_Util       - installed: 1.4.2
[PEAR] PEAR           - installed: 1.10.5
Wrote PEAR system config file at: /usr/local/php-fpm/etc/pear.conf
You may want to add: /usr/local/php-fpm/lib/php to your php.ini include_path
/usr/local/src/php-5.6.32/build/shtool install -c ext/phar/phar.phar /usr/local/php-fpm/bin
ln -s -f phar.phar /usr/local/php-fpm/bin/phar
Installing PDO headers:           /usr/local/php-fpm/include/php/ext/pdo/
  • 配置
[[email protected] php-5.6.32]# ls /usr/local/php-fpm/
bin  etc  include  lib  php  sbin  var
[[email protected] php-5.6.32]# ls /usr/local/php
bin  etc  include  lib  php
# php-fpm的選項和php差不多,有-i,-m等,多了-t來檢查配置語法
[[email protected] php-5.6.32]# /usr/local/php-fpm/sbin/php-fpm -t
[25-Nov-2018 16:45:09] ERROR: failed to open configuration file '/usr/local/php-fpm/etc/php-fpm.conf': No such file or directory (2)
[25-Nov-2018 16:45:09] ERROR: failed to load configuration file '/usr/local/php-fpm/etc/php-fpm.conf'
[25-Nov-2018 16:45:09] ERROR: FPM initialization failed

[[email protected] php-5.6.32]# cd /usr/local/php-fpm/etc/
[[email protected] etc]# cp /usr/local/src/php-5.6.32/php.ini-production ./php.ini # 拷貝配置檔案php.ini
[[email protected] etc]# vim php-fpm.conf # 建立php-fpm.conf配置檔案,新增如下內容
[global] # 全域性配置
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www] #模組名
listen = /tmp/php-fcgi.sock #socket監聽
# listen = 127.0.0.1:9000 # tcp監聽
listen.mode = 666 # /tmp/php-fcgi.sock檔案許可權
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024

# 拷貝啟動指令碼
[[email protected] etc]# cp /usr/local/src/php-5.6.32/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm  
[[email protected] etc]# chmod 755 /etc/init.d/php-fpm
[[email protected] etc]# chkconfig --add php-fpm
[[email protected] etc]# chkconfig php-fpm on
[[email protected] etc]# useradd -s /sbin/nologin php-fpm # 建立不能登入的php-fpm使用者  

[email protected] etc]# service php-fpm start
Starting php-fpm  done
[[email protected] etc]# ps -aux|grep php-fpm
root     54315  1.0  0.4 124564  4880 ?        Ss   17:06   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm  54316  0.0  0.4 124564  4644 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54317  0.0  0.4 124564  4644 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54318  0.0  0.4 124564  4644 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54319  0.0  0.4 124564  4644 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54320  0.0  0.4 124564  4648 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54321  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54322  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54323  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54324  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54325  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54326  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54327  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54328  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54329  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54330  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54331  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54332  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54333  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54334  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54335  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www

Nginx 介紹

  • Nginx官網 nginx.org
  • Nginx應用場景:web服務、反向代理、負載均衡
  • Nginx著名分支,淘寶基於Nginx開發的Tengine,使用上和Nginx一致,服務名,配置檔名都一樣,和Nginx的最大區別在於Tenging增加了一些定製化模組,在安全限速方面表現突出,另外它支援對js,css合併
  • Nginx核心+lua相關的元件和模組組成了一個支援lua的高效能web容器openresty,參考 http://jinnianshilongnian.iteye.com/blog/2280928