LNMP環境搭建

一、LNMP架構介紹

本文介紹的LNMP環境各個軟件的版本爲:Centos7 + Nginx1.12.2 + Mysql-5.7.17 + PHP7.2.5。本文是在假設Centos7已經安裝完成的前提下進行介紹的,若尚未安裝,可以前往Centos官網下載對應的ios文件安裝,下載地址:http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1804.iso
LNMP環境搭建
LNMP架構和LAMP架構挺像的,只不過一個用的是Apach,一個用的是Nginx。LNMP就是Linux+Nginx+MySQL+PHP,Nginx和Apache一樣都是web服務器。

有一點不同的是在LNMP結構裏php會啓動一個服務:php-fpm,而LANP中php只是作爲Apache的一個模塊存在。Nginx會把用戶的動態請求交給php服務去處理,這個php服務就會去和數據庫進行交互。用戶的靜態請求Nginx會直接處理,Nginx處理靜態請求的速度要比Apache快很多性能上要好,所以Apache和Nginx在動態請求處理上區別不大,但如果是靜態請求處理的話就會明顯發現Nginx要快於Apache,而且Nginx能承受的併發量要比Apache大,可以承受好幾萬的併發量,所以大一些的網站都會使用Nginx作爲web服務器。

二、系統環境

系統平臺: CentOS 7.3
Server: 192.168.8.55

# 寫hosts記錄
[root@LNMP ~]# echo "192.168.8.55 LNMP" >> /etc/hosts

# 關閉firewalld和selinux
[root@LNMP ~]# systemctl stop firewalld && systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

# 關閉selinux
[root@LNMP ~]# setenforce 0
[root@LNMP ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

三、使用yum源安裝(忽略)

本文不使用yum安裝,如需要使用yum安裝LNMP環境,則需搭建epel第三方yum倉庫。本文主要使用源碼包安裝,如下標題第四、五、六。

1、安裝依賴包

[root@LNMP ~]# yum –y install gcc gcc-c++ ncurses-devel perl

2、修改yum源

安裝epel擴展yum的軟件包,用於在本機生成epel擴展yum配置文件。

[root@LNMP ~]# rpm -Uvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@LNMP ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@LNMP ~]# rpm -Uvh  http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

3、安裝Nginx、MySQL、PHP

配置好epel的yum源安裝相應的基礎模塊,可以使用yum直接安裝。

# 安裝Apache
[root@LAMP ~]# yum -y install httpd
# 安裝數據庫
[root@LAMP ~]# yum -y install mariadb
# 安裝php7.0
[root@LAMP ~]# yum -y install php70w php70w-devel
# 安裝php擴展
[root@LAMP ~]# yum install -y  php70w-mysql.x86_64   php70w-gd.x86_64   php70w-ldap.x86_64   php70w-mbstring.x86_64  php70w-mcrypt.x86_64
# 安裝PHP-FPM
[root@LAMP ~]# yum install -y php70w-fpm

四、安裝MySQL

1、卸載已有MySQL

[root@LNMP ~]# yum -y remove mariadb
[root@LNMP ~]# yum -y remove mysql

[root@LNMP ~]# rm -rf /usr/local/mysql
[root@LNMP ~]# rm -rf /usr/src/mysql-5.7.18/
[root@LNMP ~]# rm -rf /var/lib/mysql/
[root@LNMP ~]# rm /etc/my.cnf

2、安裝MySQL依賴包

[root@LNMP ~]# yum –y install libevent* libtool* autoconf* libstd* ncurse* bison* openssl*

# MySQL 5.5之後的版本需要cmake(c語言編譯器)來進行編譯安裝
[root@LNMP ~]# yum -y install cmake

3、安裝boost包

# MySQL 5.7 之後的版本,cmake編譯需要boost類庫
# 下載
[root@LNMP ~]# wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
# 解壓
[root@LNMP ~]# tar zxvf boost_1_59_0.tar.gz -C /usr/local/
# 重命名
[root@LNMP ~]# mv /usr/local/boost_1_59_0/ /usr/local/boost/

4、下載解壓MySQL包

[root@LNMP ~]# wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17.tar.gz
# 解壓到/usr/src/目錄
[root@LNMP ~]# tar zxvf mysql-5.7.17.tar.gz -C /usr/src/

5、編譯安裝

# 進入解壓之後的/usr/src/mysql-5.7.17/目錄
[root@LNMP ~]# cd /usr/src/mysql-5.7.17/
[root@LNMP mysql-5.7.17]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql57 -DMYSQL_DATADIR=/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql57/mysql57.socket -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=/usr/local/boost

[root@LNMP mysql-5.7.17]# make
[root@LNMP mysql-5.7.17]# make install
**cmake編譯配置參數說明:**
-DCMAKE_INSTALL_PREFIX=dir_name   # 安裝的主目錄
-DMYSQL_DATADIR=dir_name MySQL   # 文件目錄的路徑,這個參數也可以在啓動MySQL的時候帶上--datadir參數進行設置
-DSYSCONFDIR=dir_name my.cnf   # 參數文件的路徑
-DWITH_INNOBASE_STORAGE_ENGINE=1   # 將INNODB存儲引擎編譯進去
-DWITH_MYISAM_STORAGE_ENGINE=1   # 將INNODB存儲引擎編譯進去
-DWITH_MEMORY_STORAGE_ENGINE=1   # 安裝memory存儲引擎
-DWITH_READLINE=bool   # 是否使用readline庫
-DMYSQL_UNIX_ADDR=file_name Unix socket   # 文件的路徑,socket文件用於服務器監聽連接,這個參數必須是絕對路徑
-DMYSQL_TCP_PORT=port_num   # 服務器監聽TCP/IP連接的端口,默認是3306
-DENABLED_LOCAL_INFILE=1   # 是否打開LOAD DATA INFILE的LOCAL參數
-DWITH_PARTITION_STORAGE_ENGINE=1   # 將分區存儲引擎編譯進去
-DEXTRA_CHARSETS=all   # 安裝所有擴展字符集
-DDEFAULT_CHARSET   # 字符集,默認字符集是latin1
-DDEFAULT_COLLATION=collation_name   # 服務校對,默認的是latin1_swedish_ci,可以通過SHOW COLLATION語句查看哪個校對匹配的字符集
-DWITH_BOOST   # boost類庫所在目錄
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1   # 將FEDERATED存儲引擎編譯進去
-DWITH_BLACKHOLE_STORAGE_ENGINE=1   # 將BLACKHOLE存儲引擎編譯進去
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1   # 不編譯EXAMPLE存儲引擎
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1   # 將Performance Schema(性能視圖)存儲引擎編譯進去
-DCOMPILATION_COMMENT=string   # 編譯環境描述
-DENABLED_PROFILING=bool   # 是否開啓profiling代碼的查詢(用於SHOW PROFILE and SHOW PROFILES語句)
-DWITH_EXTRA_CHARSETS=name   # 指定額外的字符集,默認是all,包含所有的字符集。
-DINSTALL_BINDIR=dir_name   # 安裝用戶程序的路徑,默認路徑是DCMAKE_INSTALL_PREFIX/bin
-DINSTALL_DOCDIR=dir_name   #安裝文檔的路徑,默認路徑是DCMAKE_INSTALL_PREFIX/doc
-DINSTALL_INCLUDEDIR=dir_name   #安裝頭文件的路徑,默認路徑是DCMAKE_INSTALL_PREFIX/include
-DINSTALL_LIBDIR=dir_name   # 安裝庫文件的路徑,默認路徑是DCMAKE_INSTALL_PREFIX/lib
-DINSTALL_MANDIR=dir_name   #安裝幫助手冊的路徑,默認路徑是DCMAKE_INSTALL_PREFIX/man
-DINSTALL_PLUGINDIR=dir_name   # 安裝插件的路徑,默認路徑是DCMAKE_INSTALL_PREFIX/lib/plugin
-DINSTALL_SBINDIR=dir_name   # 安裝mysqld服務端啓動腳本的路徑,默認路徑是DCMAKE_INSTALL_PREFIX/bin
-DINSTALL_SCRIPTDIR=dir_name   # 初始化MySQL數據庫的數據文件路徑的mysql_install_db腳本路徑,默認路徑是DCMAKE_INSTALL_PREFIX/scripts
-DINSTALL_SQLBENCHDIR=dir_name   # 安裝sql-bench的路徑,默認路徑是DCMAKE_INSTALL_PREFIX
-DINSTALL_SUPPORTFILESDIR=dir_name   # 安裝支持文件的路徑,默認路徑是DCMAKE_INSTALL_PREFIX/support-files
# 如若要編譯進其它功能,如SSL等,則可使用類似如下選項來實現編譯時使用某庫或不使用某庫:
-DWITH_READLINE=1
-DWITH_SSL=system
-DWITH_ZLIB=system
-DWITH_LIBWRAP=0

6、配置MySQL

(1)創建id爲27的mysql用戶

[root@LNMP ~]# groupadd mysql
[root@LNMP ~]# useradd -M -g mysql -s /sbin/nologin mysql

(2)配置文件my.cnf
[root@LNMP ~]# vim /etc/my.cnf

# 修改爲以下內容,其它全部刪掉
[mysqld]
port=3306
datadir=/data
pid-file=/var/run/mysql57/mysql57.pid
socket=/var/lib/mysql57/mysql57.socket
log-error=/var/log/mysql57/mysql57.log
user=mysql

[client]
socket=/var/lib/mysql57/mysql57.socket

(3)創建相關目錄,並修改權限

# 創建cmake命令中的-DMYSQL_DATADIR設置的路徑)
[root@LNMP ~]# mkdir /data/
# 創建cmake命令中的-DMYSQL_UNIX_ADDR設置的路徑)
[root@LNMP ~]# mkdir /var/lib/mysql57/
[root@LNMP ~]# mkdir /var/run/mysql57
[root@LNMP ~]# mkdir /var/log/mysql57/
# 修改權限
[root@LNMP ~]# chown -R mysql:mysql /usr/local/mysql57/  /data/  /var/lib/mysql57/  /var/log/mysql57/  /var/run/mysql57

7、初始化配置

# 初始化MySQL服務
[root@LNMP ~]# cd /usr/local/mysql57/
[root@LNMP mysql57]# ./bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql57/  --datadir=/data/
# 啓動MySQL服務
[root@LNMP ~]# /usr/local/mysql57/support-files/mysql.server start
初始化之後,啓動mysql57數據庫,但是路徑太長,操作起來很麻煩,我們可以把MySQL設置爲系統服務,如下步驟。

8、設置MySQL爲系統服務

[root@LNMP mysql57]# vim /lib/systemd/system/mysql57.service
# 添加文件內容如下:
[Unit]
Description=mysql
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/mysql57/support-files/mysql.server start
ExecStop=/usr/local/mysql57/support-files/mysql.server stop
ExecRestart=/usr/local/mysql57/support-files/mysql.server restart
ExecReload=/usr/local/mysql57/support-files/mysql.server reload
PrivateTmp=true

[Install]
WantedBy=multi-user.target

9、設置MySQL服務開機自啓動

[root@LNMP ~]# systemctl enable mysql57.service

10、啓動MySQL服務

[root@LNMP ~]# systemctl restart mysql57.service
[root@LNMP ~]# lsof -i:3306
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  24965 mysql   16u  IPv6  44563      0t0  TCP *:mysql (LISTEN)

LNMP環境搭建

11、修改MySQL密碼

# 登陸MySQL數據庫
[root@LNMP ~]# /usr/local/mysql57/bin/mysql
mysql> update mysql.user set authentication_string=password('123') where user='root' and host = 'localhost';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 1

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

五、安裝PHP

1、安裝PHP依賴包

[root@LNMP ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel

LNMP環境搭建
若提示yum中沒有可用的軟件包libmcrypt和libmcrypt-devel,則需要手動編譯安裝。

[root@LNMP ~]# wget https://sourceforge.mirrorservice.org/m/mc/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
[root@LNMP ~]# tar zxvf libmcrypt-2.5.8.tar.gz -C /usr/src/
[root@LNMP ~]# cd /usr/src/libmcrypt-2.5.8/
[root@LNMP libmcrypt-2.5.8]# ./configure --prefix=/usr/local/libmcrypt
[root@LNMP libmcrypt-2.5.8]# make
[root@LNMP libmcrypt-2.5.8]# make install
[root@LNMP libmcrypt-2.5.8]# /sbin/ldconfig   --重新加載系統庫包

2、下載解壓PHP包

[root@LNMP ~]# wget http://cn2.php.net/distributions/php-7.2.5.tar.gz
[root@LNMP ~]# tar zxvf php-7.2.5.tar.gz -C /usr/src/

3、編譯安裝

# 進入解壓之後的/usr/src/php-7.2.5/目錄
[root@LNMP ~]# cd /usr/src/php-7.2.5/
[root@LNMP php-7.2.5]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/etc/php.d --with-mysqli=/usr/local/mysql57/bin/mysql_config --with-iconv-dir=/usr/local --enable-mysqlnd --with-pdo-mysql=/usr/local/mysql57 --with-pcre-dir=/usr/local/ --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --with-gd --with-iconv --with-zlib --with-libxml-dir=/usr --enable-xml --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-ftp --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache --enable-bcmath

[root@LNMP php-7.2.5]# make
[root@LNMP php-7.2.5]# make install
**configure編譯配置參數說明:**
--prefix   # 用於指定PHP安裝目錄
--with-config-file-path   # 指定php.ini位置
--with-config-file-scan-dir   # 指定php.d位置
--with-mysqli   # mysqli文件目錄,優化支持
--with-iconv-dir   # 用於 PHP 編譯時指定 iconv 在系統裏的路徑,否則會掃描默認路徑
--with-iconv   # 使編譯iconv時不被禁用
--enable-mysqlnd   # 啓用mysqlnd的庫來支持mysql的連接s
--with-pdo-mysql   # MySQ安裝目錄,對MySQL的支持
--with-pcre-dir   # perl的正則庫案安裝位置
--enable-fpm   # 打上PHP-fpm 補丁後纔有這個參數,CGI方式安裝的啓動程序
--with-fpm-user   # 設置 FPM 運行的用戶身份(默認 - nobody)
--with-fpm-group   # 設置 FPM 運行時的用戶組(默認 - nobody)
--with-fpm-systemd   # 啓用 systemd 集成 (默認 - no)
--with-fpm-acl   # 使用POSIX 訪問控制列表 (默認 - no) 5.6.5版本起有效
--with-gd   # 啓用gd庫的支持
--with-zlib   # 啓用zlib庫的支持
--with-libxml-dir   # 啓用libxml2庫的支持
--enable-xml   # 啓用xml庫支持
--enable-shmop   # 啓用shmop函數支持
--enable-inline-optimization   # 優化線程
--enable-mbstring   # 多字節,字符串的支持
--enable-ftp   # 啓用ftp的支持
--with-openssl   # openssl的支持,加密傳輸時用到的
--enable-pcntl   # freeTDS需要用到的,可能是鏈接mssql 纔用到
--enable-sockets   # 啓用 sockets 支持
--with-xmlrpc   # 啓用xml-rpc的c語言
--enable-zip   # 啓用對zip的支持
--enable-soap  # 啓用soap模塊的支持
--without-pear   # 不安裝PEAR
--with-gettext   # 啓用gnu 的gettext 支持,編碼庫用到
--enable-session   # 啓用session模塊的支持
--with-curl   # 啓用curl瀏覽工具的支持
--with-jpeg-dir   # 啓用對jpeg圖片的支持
--with-freetype-dir   # 啓用對freetype字體庫的支持
--enable-opcache   # 啓用opcache共享擴展的支持
--with-mcrypt   # 指定的是libmcrypt的安裝目錄
--enable-safe-mode   # 啓用安全模式
--with-bz2   # 啓用對bz2文件的支持
--with-png-dir   # 啓用對png圖片的支持
--without-iconv   # 關閉iconv函數,一種字符集間的轉換 
--enable-gd-native-ttf   # 支持TrueType字符串函數庫 
--with-curlwrappers   # 運用curl工具啓用url流 
--with-ttf   # 啓用freetype1.*的支持,可以不加了 
--with-xsl   # 啓用XSLT 文件支持,擴展了libXML2庫 ,需要libxslt軟件 
--with-pear   # 啓用pear命令的支持,PHP擴展用的 
--enable-calendar   # 啓用日曆擴展功能 
--enable-bcmath   # 啓用圖片大小調整,用到zabbix監控的時候用到了這個模塊 
--enable-exif   # 圖片的元數據支持 
--enable-magic-quotes   # 魔術引用的支持 
--disable-rpath   # 關閉額外的運行庫文件 
--disable-debug   # 關閉調試模式 
--with-mime-magic=/usr/share/file/magic.mime   # 魔術頭文件位置
--enable-fastCGI   # 支持fastcgi方式啓動PHP
--with-ncurses   # 支持ncurses 屏幕繪製以及基於文本終端的圖形互動功能的動態庫
--enable-pcntl   # freeTDS需要用到的,可能是鏈接mssql 纔用到
--with-mcrypt   # 啓用mcryp算法
--with-mhash   # 企業mhas算法
--with-gdbm   # dba的gdbm支持
--enable-zend-multibyte   # 支持zend的多字節

4、拷貝PHP模版配置文件

將PHP包解壓目錄中的配置文件放置到/usr/local/php/etc/目錄下(也就是configure命令中的--with-config-file-path設置的路徑)

# sysconfigdir參數決定的(因爲,指定安裝路徑爲/usr/local/php,所以就要拷到/usr/local/php/etc/目錄下)
[root@LNMP ~]# cp /usr/src/php-7.2.5/php.ini-development /usr/local/php/etc/php.ini
[root@LNMP ~]# vim /usr/local/php/etc/php.ini
# 修改以下內容
1013 pdo_mysql.default_socket=/var/lib/mysql57/mysql57.socket  # 對應的socket文件地址
1154 mysqli.default_port = 3306  # 改成對應的MySQL的端口
1159 mysqli.default_socket = /var/lib/mysql57/mysql57.socket  # 對應的socket文件地址

配置php-fpm配置文件(配置fastcgi)

# 先改名,把.default去掉
[root@LNMP ~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@LNMP ~]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

可以在這些配置文件修改的選項有:

  • 監聽的IP,端口或者socket
  • 初始化的進程數
  • 執行身份
  • 錯誤是否顯示
  • 打開的資源限制等
[root@LNMP ~]# vim /usr/local/php/etc/php-fpm.d/www.conf
# 修改以下內容,並把註釋“;”去掉
23 user = www-data
24 group = www-data
36 listen = /var/run/fastcgi/fastcgi.socket
47 listen.owner = www-data
48 listen.group = www-data
49 listen.mode = 0660
113 pm.max_children = 64
118 pm.start_servers = 20
123 pm.min_spare_servers = 5
128 pm.max_spare_servers = 35
139 pm.max_requests = 3000
344 rlimit_files = 65535
420 php_flag[display_errors] = on

這三行是控制啓動fastcgi之後的socket文件的權限(特別是新版本的PHP,修復了以前socket訪問權限的bug,所以這裏指定好權限,那麼Nginx纔能有權限讀取socket來訪問fastcgi)

[root@LNMP ~]# groupadd www-data
[root@LNMP ~]# useradd -M -g www-data -s /sbin/nologin www-data
[root@LNMP ~]# mkdir /var/run/fastcgi
# 使用Nginx用戶的權限
[root@LNMP ~]# chown -R nginx.nginx /var/run/fastcgi/

5、啓動php-fpm服務

# 啓動fastcgi,直接這樣啓動,5.3.3版本之前的php需要加start參數來啓動
[root@LNMP ~]# /usr/local/php/sbin/php-fpm
[root@LNMP ~]# ll /var/run/fastcgi/
總用量 0
# 啓動過後,就可以在/var/run/fastcgi/目錄下找到socket文件
srw-rw---- 1 www-data www-data 0 5月  15 16:44 fastcgi.socket
# 也可以用此命令來查看php的factcgi的進程,有20個進程,因爲我在前面配置pm.start_servers = 20
[root@LNMP ~]# ps -ef | grep fpm
root      62646      1  0 16:44 ?        00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
www-data  62647  62646  0 16:44 ?        00:00:00 php-fpm: pool www
www-data  62648  62646  0 16:44 ?        00:00:00 php-fpm: pool www
……

LNMP環境搭建

同樣,php-fpm服務啓動和停止路徑太長,不方便操作,我們可以把php-fpm設置爲系統服務,使用系統命令進行啓動,操作步驟如下。

6、設置php-fpm爲系統服務

[root@LNMP ~]# vim /etc/systemd/system/php-fpm.service
# 添加文件內容如下:
[Unit]
Description=php-fpm
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm  # 只有啓動
PrivateTmp=True

[Install]
WantedBy=multi-user.target

7、設置php-fpm開機自啓動

[root@LNMP ~]# systemctl enable php-fpm.service

8、開啓php-fpm服務

[root@LNMP ~]# systemctl start php-fpm.service

LNMP環境搭建

六、安裝Nginx

1、安裝Nginx依賴包

# Nginx的Rewrite模塊和HTTP核心模塊會使用到PCRE正則表達式語法
[root@LNMP ~]# yum -y install pcre pcre-devel

# Nginx的各種模塊中需要使用gzip壓縮
[root@LNMP ~]# yum -y install zlib zlib-devel

# Nginx的安全套接字層密碼庫
[root@LNMP ~]# yum -y install openssl openssl-devel

2、下載解壓Nginx包

[root@LNMP ~]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
# 解壓到/usr/src/目錄
[root@LNMP ~]# tar zxvf nginx-1.12.2.tar.gz -C /usr/src/

3、編譯安裝

# 進入解壓之後的/usr/src/nginx-1.12.2/目錄
[root@LNMP ~]# cd /usr/src/nginx-1.12.2/
# --with-http_stub_status_module模塊記得要加,後面做查看nginx狀態需要這個模塊
[root@LNMP nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_gzip_static_module  --with-http_stub_status_module  --with-http_ssl_module
[root@LNMP nginx-1.12.2]# make
[root@LNMP nginx-1.12.2]# make install
**configure編譯配置參數說明:**
--prefix   # 用於指定Nginx編譯後的安裝目錄
--user   # 設置Nginx運行的用戶身份(默認 - nobody)
--group   # 設置Nginx運行的用戶組(默認 - nobody)
--add-module   # 爲添加的第三方模塊,此次添加了fdfs的Nginx模塊
--with..._module   # 表示啓用的Nginx模塊,如此處啓用了http_ssl_module模塊

4、創建設置Nginx賬號

[root@LNMP ~]# groupadd nginx
[root@LNMP ~]# useradd -M -g nginx -s /sbin/nologin nginx

5、配置nginx.conf主配置文件

[root@LNMP ~]# vim /usr/local/nginx/conf/nginx.conf
# 設置參數如下:
  2 user  nginx nginx;   # 運行用戶和組
  3 worker_processes  1;   # 啓動ngnix的服務的工作進程數
  5 error_log  logs/error.log;   # 打開註釋,錯誤日誌以及日誌等級
  9 pid        logs/nginx.pid;   # 打開註釋,pid文件
 12 events {
 13     worker_connections  65535;   # 每個進程允許打開的併發連接數
 14 }
 17 http {
 18     include       mime.types;
 19     default_type  application/octet-stream;
27     sendfile        on;
 28     tcp_nopush     on;   # 打開註釋
 31     keepalive_timeout  65;   # 打開註釋
33     gzip  on;
35     server {
 36         listen       80;   # 監聽的端口
 37         server_name  192.168.8.55;   # 主機名或者IP
 39         charset utf-8;   # 字符集
43         location / {
 44             root   /web;   # 根目錄,可以使用相對路徑(/usr/local/nginx),也可以使用絕對路徑,去掉location{ }
45             index index.php index.html index.htm;    # 主頁文件
46         }
65         location ~ \.php$ {
 66         fastcgi_connect_timeout 300;
67         fastcgi_read_timeout 300;
68         fastcgi_send_timeout 300;
69         fastcgi_pass    unix:/var/run/fastcgi/fastcgi.socket;   # 對應配置php-fpm.conf裏的設置
70             fastcgi_index  index.php;
71             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_    script_name;   # 把/scripts改成$document_root,表示家目錄下的.php文件也當會以php來執行,否則可能出現白麪
72             include        fastcgi.conf;
 73         }

LNMP環境搭建

# 把定義的網站家目錄也創建出來
[root@LNMP ~]# mkdir /web

# 檢測nginx.conf是否配置正確
[root@LNMP ~]# /usr/local/nginx/sbin/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@LNMP ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [warn] 65535 worker_connections exceed open file resource limit: 1024
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
由於修改系統打開的文件限制數,導致啓動時會有警告,因爲我的配置worker_connections 65535;
[root@LNMP ~]# ulimit -SHn 65535

錯誤二:
[root@LNMP ~]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] "fastcgi_pass" directive is duplicate in /usr/local/nginx/conf/nginx.conf:69
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
fastcgi_pass文件路徑有錯,因爲這個路徑是由php-fpm服務啓動之後產生的socket的文件路徑,如果沒有,啓動php-fpm服務即可。

一旦上面的修改重啓電腦後不生效,如果要開機就生效可以使用下面的兩種方法之一
方法一:把上面的命令加到/etc/rc.local裏
方法二:寫進配置文件/etc/security/limits.conf

[root@LNMP ~]# vim /etc/security/limits.con
# 添加以下內容
nginx           soft    nofile  65535
nginx           hard    nofile  65535
root            soft    nofile  65535
root            hard    nofile  65535

# 啓動Nginx服務
[root@LNMP ~]# /usr/local/nginx/sbin/nginx
# 停止Nginx服務
[root@LNMP ~]# /usr/local/nginx/sbin/nginx -s stop

同樣,Nginx服務啓動和停止路徑太長,不方便操作,我們可以把Nginx設置爲系統服務,使用系統命令進行啓動,操作步驟如下。

6、設置Nginx爲系統服務

[root@LNMP ~]# vim /lib/systemd/system/nginx.service
# 添加文件內容如下:
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

7、設置Nginx開機自啓動

[root@LNMP ~]# systemctl enable nginx.service

8、開啓Nginx服務

[root@LNMP ~]# systemctl start nginx.service

# 查看nginx是否啓動成功:
[root@LNMP ~]# lsof -i:80

LNMP環境搭建

在瀏覽器中訪問測試:

http://192.168.8.55

出現以下界面則表示可以成功訪問:
LNMP環境搭建

9、編寫PHP測試頁

建立一個主頁,在家目錄下建立一個PHP測試頁來測試

[root@LNMP ~]# vim /web/index.php
<?php
        phpinfo();
?>

在瀏覽器中訪問測試:

http://192.168.8.55

LNMP環境搭建

至此,LNMP環境搭建完成!

七、安裝Discuz論壇

安裝一個開源論壇,驗證LNMP環境配置是否正常。

1、下載解壓重命名

# 下載
[root@LNMP ~]# wget http://zj.mycodes.net/201712/Discuz_X3.4_GIT_SC_GBK.zip
# 解壓
[root@LNMP ~]# unzip Discuz_X3.4_GIT_SC_GBK.zip -d /web/
[root@LNMP ~]# cd /web/
# 重命名
[root@LNMP web]# mv /web/dir_SC_GBK/ /web/discuz
[root@LNMP discuz]# cd /web/discuz/
[root@LNMP discuz]# ls
readme  upload  utility
# 移動
[root@LNMP discuz]# mv upload/* ./
[root@LNMP discuz]# mv readme/* ./
[root@LNMP discuz]# mv utility/* ./

在瀏覽器中訪問測試

http://192.168.8.55/discuz/install

進入安裝嚮導
LNMP環境搭建

點擊“我同意”。
LNMP環境搭建

2、修改權限

目錄、文件權限檢查狀態爲不可寫,需要修改權限,用戶改成daemon。

[root@LNMP ~]# chmod -R a+w /web/
[root@LNMP ~]# chown -R daemon.daemon /web/

修改完成之後,刷新瀏覽器,點擊“下一步”。
LNMP環境搭建

選擇“全新安裝Discuz!X(含Ucenter Server)”,點擊“下一步”。

3、安裝數據庫並授權

安裝數據庫,需要MySQL數據庫授權:

# 登陸MySQL數據庫
[root@LNMP ~]# /usr/local/mysql57/bin/mysql -p123
# 創建一個庫,用於存放將要安裝的Discuz論壇的表
mysql> create database discuz;
Query OK, 1 row affected (0.00 sec)
# 授權一個用戶,用於Discuz論壇程序連接MySQL
mysql> grant all on discuz.* to 'discuz'@'localhost' identified by '123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
# 刷新
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

LNMP環境搭建

數據庫安裝如下圖所示:
LNMP環境搭建

填寫完成之後,點擊“下一步”,開始安裝數據庫。可以看到剛纔創建的discuz庫裏面有297張表(show tables discuz;)。
LNMP環境搭建
LNMP環境搭建

4、安裝完成

安裝完成之後可以看到Discuz論壇的首頁。
LNMP環境搭建

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章