LNMP(Linux + Nginx + Mysql/Mariadb + PHP)是目前互聯網公司最經常使用的經典Web服務環境組合。css
- Nginx:是一高性能的HTTP和反向代理服務器。
- Mysql/Mariadb:是 一個小型關係型數據庫管理系統,用於存儲數據庫(如用戶名、密碼、文章等)
- PHP:是一種在服務器端執行的嵌入HTML文檔的腳本語言。
這3個軟件均爲免費開源軟件,組合在一塊兒跑在Linux系統之上,成爲一個免費、高效、擴展性強的網站服務系統。html
LNMP組合工做流程:mysql
- 用戶經過瀏覽器輸入域名請求Nginx web服務,若是請求的是靜態資源,則由Nginx解析並返回給用戶;
- 若是是動態請求(.php結尾),那麼Nginx就會將該請求經過FastCGI接口發送給PHP引擎服務(FastCGI進程php-fpm)進行解析;
- 若是該動態請求須要讀取數據庫的數據,那麼PHP就會繼續向後端請求Mysql/Mariadb數據庫,以讀取須要的數據,然後經過Nginx服務將獲取的數據返回給用戶。
須要注意的是,這裏所謂的靜態和動態元素linux
- 所謂靜態,是指Nginx能夠直接處理的圖片、js、css、視頻、音頻、flash等等。
- 所謂動態,是指這些請求須要和數據庫進行連接。好比用戶的登陸,博客文章的查看和編寫。
以下圖:nginx
(1)下載二進制免編譯版本mysql 5.6.35 [root@localhost tools]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz [root@localhost tools]# wget http://cn.php.net/distributions/php-7.2.5.tar.gz (2)增長mysql運行用戶 [root@localhost tools]# useradd -s /sbin/nologin -M mysql (3)解壓並移動Mysql到指定的安裝路徑 [root@localhost tools]# tar -zxf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz [root@localhost tools]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql-5.6.35 (4)建立軟鏈接並更改目錄所屬 [root@localhost tools]# ln -sv /usr/local/mysql-5.6.35 /usr/local/mysql ‘/usr/local/mysql’ -> ‘/usr/local/mysql-5.6.35’ [root@localhost mysql]# chown -R mysql.mysql /usr/local/mysql (5)初始化Mysql [root@localhost mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql (6)拷貝Mysql啓動腳本,並修改腳本權限啓動 [root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@localhost mysql]# chmod 755 /etc/init.d/mysqld [root@localhost mysql]# vim /etc/init.d/mysqld basedir=/usr/local/mysql datadir=/usr/local/mysql/data [root@localhost mysql]# cp support-files/my-default.cnf /etc/my.cnf [root@localhost mysql]# /etc/init.d/mysqld start Starting MySQL.Logging to '/usr/local/mysql/data/localhost.err'. ... SUCCESS! [root@localhost mysql]# netstat -tulnp |grep 3306 tcp6 0 0 :::3306 :::* LISTEN 62679/mysqld (7)加入開機啓動,測試登陸 [root@localhost mysql]# chkconfig --add mysqld [root@localhost mysql]# chkconfig mysqld on [root@localhost mysql]# export PATH=/usr/local/mysql/bin/:$PATH [root@localhost mysql]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.35 MySQL Community Server (GPL) 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> quit; (8)mysql安全設置 [root@localhost mysql]# mysqladmin -uroot password '123456' //配置mysql的root用戶密碼 Warning: Using a password on the command line interface can be insecure. [root@localhost mysql]# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) [root@localhost mysql]# mysql -uroot -p123456 -e "show databases;" Warning: Using a password on the command line interface can be insecure. +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ [root@localhost mysql]# mysql -uroot -p //清理無用的Mysql用戶和庫 Enter password: mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | ::1 | | | localhost | | root | localhost | +------+-----------+ rows in set (0.01 sec) mysql> drop user "root"@"::1" -> ; Query OK, 0 rows affected (0.00 sec) mysql> drop user ""@"localhost"; Query OK, 0 rows affected (0.00 sec) mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | localhost | +------+-----------+ rows in set (0.00 sec) 有時使用drop命令刪除不了用戶,多是大寫或者是特殊的Linux主機名致使的,以下: mysql> drop user ''@'MySQL'; ERROR 1396 (HY000): Operation DROP USER failed for ''@'mysql' 解決辦法以下: mysql> delete from mysql.user where user='' and host='MySQL'; mysql> flush privileges;
一、CGI和FastCGIc++
CGI(Common Gateway Interface)通用網關接口,爲HTTP服務器與其餘機器上的程序服務通訊交流的一種工具,CGI程序必需要在網絡服務器上運行。傳統的CGI接口方式存在很大的缺點就是性能較差。每次HTTP服務器遇到動態程序時,都須要從新啓動解析器來執行解析,以後結果纔會被返回給HTTP服務器,而這種方式在高併發的場景下是沒法使用的,爲了解決這一問題,隨之產生的就是FastCGI。web
FastCGI是一個可伸縮、高速和HTTP服務器和動態腳本語言之間通訊的接口(在Linux環境下,FastCGI接口即爲socket,這個socket能夠是文件socket,也能夠是ip socket),主要優勢是把動態語言和HTTP服務器分離開來。FastCGI採用的是C/S架構,能夠將HTTP服務器和腳本解析服務器分開,同時還能在腳本解析服務器上啓動一個或多個腳本解析守護進程,然後將獲得的結果返回給瀏覽器。這種方式可使HTTP服務器專注處理靜態請求,或者是將動態請求的結果返回客戶端,在這一點上大大提高了應用系統的性能。算法
二、Nginx FastCGI的運行原理sql
Nginx不支持對外部動態程序的直接調用或者解析,全部的外部程序(如PHP)必須經過FastCGI接口來調用。FastCGI在Linux下是socket,爲了調用CGI程序,還須要一個FastCGI的wrapper(能夠理解爲用於啓動線程的工具),這個wrapper綁定在某個固定的socket上,如端口或文件socket,好比127.0.0.1:9000。當Nginx將CGI請求發送到這個socket的時候,經過FastCGI接口,wrapper接口到請求,而後派生一個新的線程,這個線程再去調用解釋器或者外部程序處理腳原本讀取返回的數據;接着wrapper再將返回的數據經過FastCGI接口,沿着原來的socket傳遞個Nginx;最後Nginx將返回的數據發送給客戶端。
三、PHP的編譯安裝步驟
(1)準備PHP依賴的庫環境 [root@localhost ~]# yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses curl openssl-devel gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c xmlrpc-c-devel [root@localhost ~]# cd /tools (2)下載php 7.2.5並解壓 [root@localhost ~]# wget http://cn.php.net/distributions/php-7.2.5.tar.gz [root@localhost tools]# tar -zxf php-7.2.5.tar.gz [root@localhost tools]# cd php-7.2.5 (3)編譯安裝 [root@localhost php-7.2.5]# ./configure \ --prefix=/usr/local/php7.2.5 \ //指定 php 安裝目錄 --with-config-file-path=/usr/local/php7.2.5/etc \ //指定php.ini位置 --with-config-file-scan-dir=/usr/local/php7.2.5/etc/conf.d \ //指定擴展php.ini位置 --enable-inline-optimization \ //優化線程 --disable-debug \ //關閉調試模式 --disable-rpath \ //關閉額外的運行庫文件 --enable-shared \ --enable-opcache \ //啓用操做碼緩存 --enable-fpm \ //表示激活PHP-FPM方式服務,即FactCGI方式運行PHP服務。 --with-mysql=mysqlnd \ //增長mysql支持 --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-gettext \ //打開gnu 的gettext 支持,編碼庫用到 --enable-mbstring \ //多字節,字符串的支持 --with-iconv \ //打開iconv函數,多種字符集間的轉換 --with-mcrypt \ //啓用mcrypt加密算法 --with-mhash \ //啓用mhash加密算法 --with-openssl \ //openssl的支持,加密傳輸時用到的 --enable-bcmath \ //打開圖片大小調整,用到zabbix監控的時候用到了這個模塊 --enable-soap \ --with-libxml-dir \ //打開libxml2庫的支持 --enable-pcntl \ //freeTDS須要用到,多是連接mssql --enable-shmop \ --enable-sysvmsg \ --enable-sysvsem \ //使用sysv信號機制 --enable-sysvshm \ --enable-sockets \ //打開sockets支持 --enable-exif \ --enable-zend-signals \ --enable-gd-native-ttf \ //支持TrueType字符串函數庫 --enable-ftp \ //打開ftp的支持 --with-curl \ //打開curl瀏覽工具的支持 --with-zlib \ //打開zlib庫的支持 --enable-zip \ //打開對zip的支持 --with-bz2 \ //打開對bz2文件的支持 --with-readline \ --with-jpeg-dir \ //打開對jpeg圖片的支持 --with-png-dir \ //打開對png圖片的支持 --with-gd \ //打開gd庫的支持 --with-freetype-dir \ //打開對freetype字體庫的支持 --with-pear \ //打開pear命令的支持,PHP擴展用的 [root@localhost php-7.2.5]# make && make install (4)修改php服務的相關配置文件 [root@localhost php-7.2.5]# cp php.ini-production /usr/local/php7.2.5/etc/php.ini [root@localhost php-7.2.5]# cd /usr/local/php7.2.5/etc/ [root@localhost etc]# cp php-fpm.conf.default php-fpm.conf [root@localhost etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf (5)拷貝啓動腳本,並添加到開機啓動 [root@localhost etc]# cp /tools/php-7.2.5/sapi/fpm/php-fpm.service /usr/lib/systemd/system/ [root@localhost etc]# /usr/local/php7.2.5/sbin/php-fpm -t [17-Jul-2018 16:56:41] NOTICE: configuration file /usr/local/php7.2.5/etc/php-fpm.conf test is successful [root@localhost etc]# export PATH=/usr/local/php7.2.5/bin/:/usr/local/php7.2.5/sbin/:$PATH [root@localhost etc]# systemctl enable php-fpm Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service. (6)啓動php-fpm,並檢查端口 [root@localhost etc]# systemctl start php-fpm [root@localhost etc]# netstat -tulnp |grep 9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 14853/php-fpm: mast [root@localhost php7.2.5]# ln -sv /usr/local/php7.2.5 /usr/local/php ‘/usr/local/php’ -> ‘/usr/local/php7.2.5’
(1)安裝pcre、openssl依賴 pcre爲正則依賴的庫包文件,openssl在使用https時會用到 [root@localhost tools]# yum install -y pcre pcre-devel openssl openssl-devel [root@localhost tools]# rpm -qa pcre pcre-devel pcre-devel-8.32-17.el7.x86_64 pcre-8.32-17.el7.x86_64 [root@localhost tools]# rpm -qa openssl openssl-devel openssl-devel-1.0.2k-12.el7.x86_64 openssl-1.0.2k-12.el7.x86_64 (2)下載nginx,並解壓 [root@localhost tools]# wget http://nginx.org/download/nginx-1.15.1.tar.gz [root@localhost tools]# tar -zxf nginx-1.15.1.tar.gz (3)編譯安裝 [root@localhost tools]# useradd -s /sbin/nologin -M nginx [root@localhost tools]# cd nginx-1.15.1 [root@localhost nginx-1.15.1]# ./configure --help [root@localhost nginx-1.15.1]# ./configure \ --user=nginx \ #配置進程用戶權限 --gourp=nginx \ #配置進程用戶組權限 --prefix=/usr/local/nginx1.15.1 \ #指定安裝路徑 --with-http_stub_status_module \ #使用激活狀態信息模塊 --with-http_ssl_module #使用ssl功能模塊 [root@localhost nginx-1.15.1]# make && make install [root@localhost nginx-1.15.1]# ln -sv /usr/local/nginx1.15.1 /usr/local/nginx ‘/usr/local/nginx’ -> ‘/usr/local/nginx1.15.1’ (5)啓動 [root@localhost ~]# /usr/local/nginx/sbin/nginx -t #檢查配置文件語法 nginx: the configuration file /usr/local/nginx1.15.1/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx1.15.1/conf/nginx.conf test is successful [root@localhost ~]# /usr/local/nginx/sbin/nginx #啓動nginx [root@localhost ~]# netstat -tunlp |grep 80 #檢查監聽端口 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 58713/nginx: master
官網文檔連接:http://nginx.org/en/linux_packages.html
(1)增長Nginx倉庫源 [root@localhost ~]# vim /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/ gpgcheck=0 enabled=1 (2)安裝Nginx [root@localhost ~]# yum install -y nginx (3)啓動nginx [root@localhost ~]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost ~]# systemctl start nginx
瀏覽器訪問:http://192.168.56.11,會出現nginx的歡迎頁