1. 程式人生 > >Linux中詳細搭建Lnmp架構

Linux中詳細搭建Lnmp架構

.org vim ams eas ng- repo head 添加 skip

環境說明

ip 需要安裝的程序
192.168.209.12 nginx mysql php
 *lnmp架構的安裝的順序*
 ①安裝nginx
 ②安裝mysql
 ③安裝php
 ④最後配置nginx和創建php測試程序
 ⑤驗證

*******************①安裝nginx***********************
1.//創建系統用戶nginx
 [root@lanzhiyong ~]# useradd -r -M -s /sbin/nologin nginx

//安裝依賴環境
[root@lanzhiyong ~]# yum -y install pcre-devel openssl openssl-devel gd-devel
[root@lanzhiyong ~]# yum install -y gd-devel-2.0.35-26.el7.x86_64.rpm 
[root@lanzhiyong ~]# yum -y groups mark install ‘Development Tools‘

//創建日誌存放目錄
[root@lanzhiyong ~]# mkdir -p /var/log/nginx
[root@lanzhiyong ~]# chown -R nginx.nginx /var/log/nginx

//下載nginx
[root@lanzhiyong ~]# cd /usr/src/ 
[root@lanzhiyong src]# wgethttp://nginx.org/download/nginx-1.14.0.tar.gz

//編譯安裝
[root@lanzhiyong src]# tar xf nginx-1.14.0.tar.gz
[root@lanzhiyong src]# cd  nginx-1.14.0

[root@lanzhiyong nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log

 [root@lanzhiyong nginx-1.14.0]# make -j $(grep ‘processor‘ /proc/cpuinfo | wc -l) && make install

 2.nginx安裝後配置
 //配置環境變量   
  [root@lanzhiyong ~]# echo ‘export PATH=/usr/local/nginx/sbin:$PATH‘ > /etc/profile.d/nginx.sh
  [root@lanzhiyong ~]# source /etc/profile.d/nginx.sh 
  [root@lanzhiyong ~]# nginx
  [root@lanzhiyong ~]# ss -antl
  State      Recv-Q Send-Q      Local Address:Port                     Peer Address:Port              
  LISTEN     0      128                     *:80                                  *:*                  
  LISTEN     0      128                     *:22                                  *:*                  
  LISTEN     0      100             127.0.0.1:25                                  *:*    
  LISTEN     0      128                    :::22                                 :::*                  
  LISTEN     0      100                   ::1:25                                 :::*                   

 ***********************②安裝mysql**************************
 //安裝依賴 包
[root@lanzhiyong ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

//創建用戶和組
[root@lanzhiyong ~]# cd /usr/src/
[root@lanzhiyong src]# groupadd -r -g 306 mysql
[root@lanzhiyong src]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

//下載二進制格式的mysql軟件包

[root@lanzhiyong src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

//解壓軟件至/usr/local
[root@lanzhiyong src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz  -C /usr/local/
[root@lanzhiyong src]# cd /usr/local/

//創建軟連接
[root@lanzhiyong local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql

//修改目錄/usr/local/mysql的屬主屬組
[root@lanzhiyong local]# chown -R mysql.mysql /usr/local/mysql
[root@lanzhiyong local]# ll /usr/local/mysql

//添加環境變量
[root@lanzhiyong ~]# ls /usr/local/mysql
[root@lanzhiyong ~]# echo ‘export PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[root@lanzhiyong ~]# source /etc/profile.d/mysql.sh
[root@lanzhiyong ~]# echo $PATH 

//建立數據存放目錄
[root@lanzhiyong ~]# mkdir /opt/data
[root@lanzhiyong ~]# chown -R mysql.mysql /opt/data/
[root@lanzhiyong ~]# ll /opt/
總用量 0
drwxr-xr-x. 2 mysql mysql 6 8月  19 13:20 data

//初始化數據庫 註意這個命令後會生成臨時密碼 要記住 jd?ajfrKY4pQ
[root@lanzhiyong ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/

//配置mysql
//軟連接
[root@lanzhiyong ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
[root@lanzhiyong ~]# echo ‘/usr/local/mysql/lib‘ > /etc/ld.so.conf.d/mysql.conf
[root@lanzhiyong ~]# ldconfig -v

//生成配置文件
[root@lanzhiyong ~]# cat > /etc/my.cnf << EOF
> [mysqld]
> basedir = /usr/local/mysql
> datadir = /opt/data
> socket = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/data/mysql.pid
> user = mysql
> skip-name-resolve
> EOF

//配置服務啟動腳本
[root@lanzhiyong ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lanzhiyong ~]# sed -ri ‘s#^(basedir=).*#\1/usr/local/mysql#g‘ /etc/init.d/mysqld
[root@lanzhiyong ~]# sed -ri ‘s#^(datadir=).*#\1/opt/data#g‘ /etc/init.d/mysqld

//啟動mysql
[root@lanzhiyong ~]# service mysqld start
[root@lanzhiyong ~]# ps -ef |grep mysql
[root@lanzhiyong ~]#  ss -antl
    State      Recv-Q Send-Q      Local Address:Port                      Peer Address:Port              
LISTEN     0      128                     *:80                                  *:*                  
LISTEN     0      128                     *:22                                  *:*                  
LISTEN     0      100             127.0.0.1:25                                  *:*                                
LISTEN     0      128                    :::22                                 :::*                  
LISTEN     0      100                   ::1:25                                 :::*                  
LISTEN     0      80                     :::3306                               :::*         

//修改密碼 使用臨時密碼登入
[root@lanzhiyong ~]# mysql -uroot -p
jd?ajfrKY4pQ 這是以上步驟的臨時密碼
mysql> set password = password(‘lanzhiyong‘);

****************************③安裝php*************************
3.安裝php
//配置yum源
[root@lanzhiyong ~]# cd /etc/yum.repos.d/
[root@lanzhiyong yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo 
[root@lanzhiyong ~]# sed -i ‘s/\$releasever/7/g‘ /etc/yum.repos.d/CentOS7-Base-163.repo
[root@lanzhiyong ~]# sed -i ‘s/^enabled=.*/enabled=1/g‘ /etc/yum.repos.d/CentOS7-Base-163.repo
[root@lanzhiyong ~]# yum install -y epel-release

//安裝依賴包
[root@lanzhiyong ~]# 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 mhash mhash-devel

//下載php
[root@lanzhiyong ~]# cd /usr/src/
[root@lanzhiyong src]# wget http://cn.php.net/distributions/php-7.2.8.tar.xz

//編譯安裝php   
[root@lanzhiyong src]# tar xf php-7.2.8.tar.xz
[root@lanzhiyong src]# cd php-7.2.8
[root@lanzhiyong php-7.2.8]#
./configure --prefix=/usr/local/php7 --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir=/usr --with-mysqli=/usr/local/mysql/bin/mysql_config --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-jpeg-dir --with-png-dir --with-xmlrpc --with-xsl --with-zlib --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip
[root@lanzhiyong php-7.2.8]# make -j $(cat /proc/cpuinfo |grep processor |wc -l)
[root@lanzhiyong php-7.2.8]# make install

//安裝後配置
[root@lanzhiyong ~]# echo ‘export PATH=/usr/local/php7/bin:$PATH‘ >  /etc/profile.d/php7.sh
[root@lanzhiyong ~]# source /etc/profile.d/php7.sh
[root@lanzhiyong ~]# cd /usr/src/php-7.2.8
[root@lanzhiyong php-7.2.8]# which php
/usr/local/php7/bin/php
[root@lanzhiyong php-7.2.8]# php -v

//配置php-fpm
[root@lanzhiyong php-7.2.8]# cp php.ini-production /etc/php.ini
[root@lanzhiyong php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@lanzhiyong php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
[root@lanzhiyong php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf

[root@lanzhiyong php-7.2.8]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

//配置fpm的相關選項為你所需的值:
[root@lanzhiyong ~]# vim /usr/local/php7/etc/php-fpm.conf
*****在配置文件最後面添加一下內容
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
[root@lanzhiyong ~]# tail /usr/local/php7/etc/php-fpm.conf

//啟動php-fpm
[root@lanzhiyong ~]# service php-fpm start
Starting php-fpm  done
[root@lanzhiyong ~]# ss -antl 
State      Recv-Q Send-Q      Local Address:Port       Peer Address:Port              
LISTEN     0      128                     *:80                                  *:*                  
LISTEN     0      128                     *:22                                  *:*                  
LISTEN     0      100             127.0.0.1:25                            *:*                  
LISTEN     0      128             127.0.0.1:9000                        *:*                  
LISTEN     0      128                    :::22                                 :::*                  
LISTEN     0      100                   ::1:25                                :::*                  
LISTEN     0      80                     :::3306                              :::*   
 [root@lanzhiyong ~]# ps -ef |grep php

*******************④最後配置nginx和創建php測試程序****************************

//nginx配置文件/usr/local/nginx/conf/nginx.conf,只要修改配置文件中的server{ } 配置塊中的內容,修改location塊,追加index.php讓nginx服務器默認支持index.php為首頁
技術分享圖片

技術分享圖片

[root@lanzhiyong ~]# vim /usr/local/nginx/conf/nginx.conf
…………………………………………
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm//後面添加 index.php;
}
…… ……………………………………
#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;
#主要修改的就是fastcgi_param中的/scripts為$document_root
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include        fastcgi_params;
}
#deny access to .htaccess files, if Apache‘s document root

//修改配置文件後重新加載nginx
[root@lanzhiyong ~]# /usr/local/nginx/sbin/nginx -s reload

//創建測試php程序,在/usr/local/nginx/html/test.php,打印一下php配置
[root@lanzhiyong ~]# cat > /usr/local/nginx/html/test.php << EOF
> ?php
> phpinfo();
> ?>
> EOF

  ***********************⑤驗證******************************
最後打開瀏覽器輸入 192.168.209.12/test.php 看到輸出的頁面,說明nginx和php都配置成功了

技術分享圖片

Linux中詳細搭建Lnmp架構