LNMP(Nginx+MySQL+PHP)

1、搭建Nginx

一、解壓壓縮包php

[root@localhost abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt/

二、建立用戶,而且不容許登陸系統html

[root@localhost abc]# useradd -M -s /sbin/nologin nginx

三、安裝編譯工具mysql

[root@localhost abc]# yum install gcc gcc-c++ pcre-devel zlib-devel -y

四、進行編譯安裝nginx

[root@localhost opt]# cd nginx-1.12.2/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

安裝c++

[root@localhost nginx-1.12.2]# make && make install

五、爲了方便管理,能夠給nginx建立一個軟鏈接sql

[root@localhost nginx-1.12.2]#  ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

六、編寫一個腳本,能夠用 systemctl 工具控制其開啓、關閉、重載數據庫

root@localhost nginx-1.12.2]# cd /lib/systemd/
[root@localhost systemd]# cd system
[root@localhost system]# vim nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

添加權限vim

root@localhost system]#  chmod 754 nginx.service

七、啓動Nginx,並關閉防火牆和加強性安全功能安全

[root@localhost system]# systemctl start nginx.service 
[root@localhost system]# systemctl stop firewalld.service
[root@localhost system]# setenforce 0

咱們在客戶機上進行訪問
在這裏插入圖片描述bash

2、搭建MySQL

一、解壓MySQL

[root@localhost abc]# tar zxvf mysql-boost-5.7.20.tar.gz -C /opt/

二、安裝工具包

yum -y install \
ncurses \
ncurses-devel \
bison \
cmake

三、建立用戶

[root@localhost abc]# useradd -s /sbin/nologin mysql

四、編譯

[root@localhost abc]# cd /opt/
[root@localhost opt]# cd mysql-5.7.20/
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1

五、安裝

make && make install

六、設置權限

[root@localhost mysql-5.7.20]# cd /usr/local/
[root@localhost local]# chown -R mysql.mysql mysql/

七、修改配置文件

[root@localhost local]# vim /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

八、配置環境變量
source /etc/profile

echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
echo 'export PATH' >> /etc/profile

啓動

source /etc/profile

九、初始化數據庫

[root@localhost mysql]# cd /usr/local/mysql/
bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

十、在當前路下複製到 /lib/systemd/system/下,方便systemctl管理

cp usr/lib/systemd/system/mysqld.service /lib/systemd/system/

十一、檢查mysql服務是否啓動

[root@localhost mysql]# systemctl start mysqld.service

在這裏插入圖片描述
十二、爲mysql設置密碼

mysqladmin -u root -p  password

登陸數據庫

mysql -u root -p

3、安裝PHP

一、安裝環境包

yum -y install \
libjpeg \
libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 \
libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel

二、解壓PHP

[root@02 abc]# tar jxvf php-7.1.10.tar.bz2 -C /opt/

三、編譯

[root@02 /]# cd /opt/php-7.1.10/
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \                                   ###客戶端支持庫
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip

四、安裝

[root@02 php-7.1.10]# make && make install

五、修改配置文件

cp php.ini-development /usr/local/php/lib/php.ini   //複製到lib路徑下
root@02 php-7.1.10]# vim /usr/local/php/lib/php.ini
mysqli.default_socket = /usr/local/mysql/mysql.sock  //指定路徑
date.timezone = Asia/Shanghai                        //指定時區

4、配置及優化FPM模塊

一、配置FPM模塊

cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf   //複製一份到 php-fpm.conf
vim php-fpm.conf    //進入文件
pid = run/php-fpm.pid    //開啓pid(去掉行首的註釋符便可)

cd /usr/local/php/etc/php-fpm.d/   
cp www.conf.default www.conf        //複製擴展包

啓動PHP

/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini

二、檢測是否成功開啓

netstat -napt | grep 9000

三、建立軟鏈接以便系統識別

ln -s /usr/local/php/bin/* /usr/local/bin/

5、讓Nginx支持PHP

root@localhost php-fpm.d]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf

  location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

二、從新啓動nginx服務

[root@localhost conf]# systemctl restart nginx.service

三、建立PHP首頁

vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>

在這裏插入圖片描述

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息