LNMP架構搭建

環境準備

'關閉防火牆和selinux'
[root@arongya ~]# systemctl stop firewalld
[root@arongya ~]# systemctl disable firewalld
[root@arongya ~]# setenforce 0
[root@arongya ~]# sed -ri 's/(SELINUX=).*/\1disabled/g' /etc/selinux/config

配置nginx

'創建系統組和用戶'
[root@arongya ~]# groupadd -r nginx
[root@arongya ~]# useradd -M -s /sbin/nologin -g nginx nginx

'安裝依賴包'
[root@arongya ~]# yum -y install pcre-devel openssl openssl-devel gd-devel

[root@arongya ~]# yum -y groups mark install 'Development Tools'

'檢查安裝Development Tools 是否安裝成功'
[root@arongya ~]# yum grouplist

'創建日誌存放目錄以及更改存放日誌目錄屬組和屬主'
[root@arongya ~]# mkdir -p /var/log/nginx
[root@arongya ~]# chown -R nginx.nginx /var/log/nginx

'下載nginx'
[root@arongya ~]# cd /usr/src/
[root@arongya src]# yum install wget
[root@arongya src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
[root@arongya src]# ls
debug  kernels  nginx-1.14.0.tar.gz

'解壓'
[root@arongya src]# tar xf nginx-1.14.0.tar.gz 

'編譯安裝'
[root@arongya src]# cd nginx-1.14.0/
[root@arongya 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@arongya nginx-1.14.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

nginx安裝後配置

'配置環境變量'
[root@arongya ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@arongya ~]# . /etc/profile.d/nginx.sh

'啓動nginx'
[root@arongya ~]# nginx 
[root@arongya ~]# 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                 :::*                  

在瀏覽器上輸入本主機的IP地址看是否能訪問,例如本主機IP是:192.168.228.30,如圖所示:
LNMP架構搭建


安裝MYSQL

'安裝依賴包'
[root@arongya ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

'創建mysql的系統用戶和組'
[root@arongya ~]# groupadd -r -g 306 mysql
[root@arongya ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

'下載二進制格式的mysql的軟件包'
[root@arongya ~]# cd /usr/src/
[root@arongya src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

'解壓軟件至/usr/local/'
[root@arongya src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@arongya src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@arongya src]# ls /usr/local/
bin    include  libexec                              sbin
etc    lib      mysql-5.7.22-linux-glibc2.12-x86_64  share
games  lib64    nginx                                src
[root@arongya src]# cd /usr/local/
[root@arongya local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
[root@arongya local]# ll
total 0
drwxr-xr-x.  2 root root   6 Nov  5  2016 bin
drwxr-xr-x.  2 root root   6 Nov  5  2016 etc
drwxr-xr-x.  2 root root   6 Nov  5  2016 games
drwxr-xr-x.  2 root root   6 Nov  5  2016 include
drwxr-xr-x.  2 root root   6 Nov  5  2016 lib
drwxr-xr-x.  2 root root   6 Nov  5  2016 lib64
drwxr-xr-x.  2 root root   6 Nov  5  2016 libexec
lrwxrwxrwx.  1 root root  36 Aug 24 16:29 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 root root 129 Aug 24 16:28 mysql-5.7.22-linux-glibc2.12-x86_64

'修改目錄/usr/local/mysql的屬主屬組'
[root@arongya ~]# chown -R mysql.mysql /usr/local/mysql
[root@arongya ~]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 Aug 24 16:29 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

'添加環境變量'
[root@arongya ~]# ls /usr/local/mysql
bin      docs     lib  README  support-files
COPYING  include  man  share
[root@arongya ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@arongya ~]# . /etc/profile.d/mysql.sh 
[root@arongya ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

'建立數據存放目錄'
[root@arongya ~]# mkdir /opt/data
[root@arongya ~]# chown -R mysql.mysql /opt/data/
[root@arongya ~]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Aug 24 16:33 data

'初始化數據庫'
[root@arongya ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-08-24T08:34:51.043748Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-24T08:34:52.915665Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-08-24T08:34:53.347600Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-08-24T08:34:53.418106Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 92b6fee8-a778-11e8-8c4e-000c29b52e8f.
2018-08-24T08:34:53.540420Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-08-24T08:34:53.544447Z 1 [Note] A temporary password is generated for root@localhost: gz(Ist:C;6_6
'這個命令最後會生成一個臨時密碼,此處密碼是gz(Ist:C;6_6'

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

'生成配置文件'
[root@arongya ~]# 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@arongya ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@arongya ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@arongya ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld 

'啓動mysql'
[root@arongya ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/arongya.err'.
. SUCCESS! 
[root@arongya ~]# ps -ef |grep mysql
root       4829      1  0 16:44 pts/1    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql      5007   4829  3 16:44 pts/1    00:00:00 /usr/localmysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arongya.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root       5039   1760  0 16:44 pts/1    00:00:00 grep --color=auto mysql
[root@arongya ~]# 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@arongya ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22
'設置新密碼'
mysql> set password = password('yaoxiaorong!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye

安裝php

'配置yum源'
[root@arongya ~]# cd /etc/yum.repos.d/
[root@arongya yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
--2018-08-24 16:48:23--  http://mirrors.163.com/.help/CentOS7-Base-163.repo
Resolving mirrors.163.com (mirrors.163.com)... 59.111.0.251
Connecting to mirrors.163.com (mirrors.163.com)|59.111.0.251|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1572 (1.5K) [application/octet-stream]
Saving to: ‘CentOS7-Base-163.repo’

100%[===================>] 1,572       --.-K/s   in 0s      

2018-08-24 16:48:23 (46.9 MB/s) - ‘CentOS7-Base-163.repo’ saved [1572/1572]

[root@arongya yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo 
[root@arongya yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo 
[root@arongya yum.repos.d]# yum -y install epel-release
'安裝依賴包'
[root@arongya ~]# 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@arongya yum.repos.d]# cd /usr/src/
[root@arongya src]# wget http://cn.php.net/distributions/php-7.2.8.tar.xz

'編譯安裝php'
[root@arongya php-7.2.8]# tar xf php-7.2.8.tar.xz 
[root@arongya php-7.2.8]# cd php-7.2.8/
[root@arongya 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@arongya php-7.2.8]# make -j 2 && make install
'安裝後配置'
[root@arongya ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@arongya ~]# source /etc/profile.d/php7.sh 
[root@arongya ~]# php -v
HP 7.2.8 (cli) (built: Aug 24 2018 17:37:56) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Te
'配置php-fpm'
[root@arongya php-7.2.8]# cp php.ini-production /etc/php.ini
[root@arongya php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@arongya php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm 
[root@arongya php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@arongya php-7.2.8]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

'配置php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf),配置fpm的相關選項爲你所需要的值'
'配置文件的最後一行'
root@arongya php-7.2.8]# vim /usr/local/php7/etc/php-fpm.conf

pm.max_children = 50        '最多同時提供50個進程提供50個併發服務'
pm.start_servers = 5      '啓動時啓動5個進程'
pm.min_spare_servers =  2     '最小空閒進程數'
pm.max_spare_servers = 8      '最大空閒進程數'

'啓動php-fpm'
[root@arongya php-7.2.8]# service php-fpm start
Starting php-fpm  done
'默認情況下,fpm監聽在127.0.0.1的9000端口,也可以使用如下命令驗證其是否已經監聽在相應的套接字'
[root@arongya php-7.2.8]# 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@arongya php-7.2.8]# ps -ef |grep php
root      58499      1  0 18:51 ?        00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nobody    58500  58499  0 18:51 ?        00:00:00 php-fpm: pool www
nobody    58501  58499  0 18:51 ?        00:00:00 php-fpm: pool www
nobody    58502  58499  0 18:51 ?        00:00:00 php-fpm: pool www
nobody    58503  58499  0 18:51 ?        00:00:00 php-fpm: pool www
nobody    58504  58499  0 18:51 ?        00:00:00 php-fpm: pool www
root      58508  26795  0 18:52 pts/0    00:00:00 grep --color=auto php

配置nginx

'編輯nginx配置文件/usr/local/nginx/conf/nginx.conf,將index.php添加在index.htm後面'
    server {
        listen       80;      '搜索listen,就會查找到以下內容'
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm  index.php;  '添加在此'

'然後配置.php請求被傳送到後端的php-fpm模塊,默認情況下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 $document_root$fastcgi_script_name;   '將fastcgi_param中的/scripts修改成$document'
            include        fastcgi_params;
        }
最後保存並退出
'重新加載nginx'
root@arongya ~]# nginx -s reload
[root@arongya ~]# ps -ef | grep nginx
root       4621      1  0 17:12 ?        00:00:00 nginx: master process nginx
nginx     58569   4621  0 19:09 ?        00:00:00 nginx: worker process
root      58571  26795  0 19:10 pts/0    00:00:00 grep --color=auto nginx

測試PHP程序

'在/usr/local/nginx/html目錄下創建yxr.php文件並打印配置php'
[root@arongya ~]# cd /usr/local/nginx/html/
[root@arongya html]# touch yxr.php
[root@arongya html]# cat > yxr.php << EOF
> <?php
>   phpinfo();
> ?>
> EOF

在瀏覽器輸入192.168.228.30/yxr.php進行訪問,如圖就是訪問成功
LNMP架構搭建

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