LNMP(Nginx MySQL PHP)

时间:2021-10-05 02:52:46

一、搭建Nginx

1、解压压缩包

[[email protected] abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt/

2、创建用户,并且不允许登录系统

[[email protected] abc]# useradd -M -s /sbin/nologin nginx

3、安装编译工具

[[email protected]ost abc]# yum install gcc gcc-c   pcre-devel zlib-devel -y

4、进行编译安装

[[email protected] opt]# cd nginx-1.12.2/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module

安装

[[email protected] nginx-1.12.2]# make && make install

5、为了方便管理,可以给nginx创建一个软连接

[[email protected] nginx-1.12.2]#  ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

6、编写一个脚本,可以用 systemctl 工具控制其开启、关闭、重载

[email protected] nginx-1.12.2]# cd /lib/systemd/
[[email protected] systemd]# cd system
[[email protected] 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

添加权限

[email protected] system]#  chmod 754 nginx.service 

7、启动Nginx,并关闭防火墙和增强性安全功能

[[email protected] system]# systemctl start nginx.service 
[[email protected] system]# systemctl stop firewalld.service
[[email protected] system]# setenforce 0

我们在客户机上进行访问
LNMP(Nginx MySQL PHP)

二、搭建MySQL

1、解压MySQL

[[email protected] abc]# tar zxvf mysql-boost-5.7.20.tar.gz -C /opt/

2、安装工具包

yum -y install ncurses ncurses-devel bison cmake

3、创建用户

[[email protected] abc]# useradd -s /sbin/nologin mysql

4、编译

[[email protected] abc]# cd /opt/
[[email protected] 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

5、安装

make && make install 

6、设置权限

[[email protected] mysql-5.7.20]# cd /usr/local/
[[email protected] local]# chown -R mysql.mysql mysql/

7、修改配置文件

[[email protected] 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

8、配置环境变量
source /etc/profile

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

启动

source /etc/profile

9、初始化数据库

[[email protected] mysql]# cd /usr/local/mysql/
bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

10、在当前路下复制到 /lib/systemd/system/下,方便systemctl管理

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

11、检查mysql服务是否启动

[[email protected] mysql]# systemctl start mysqld.service 

LNMP(Nginx MySQL PHP)
12、为mysql设置密码

mysqladmin -u root -p  password

登录数据库

mysql -u root -p

三、安装PHP

1、安装环境包

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

2、解压PHP

[[email protected] abc]# tar jxvf php-7.1.10.tar.bz2 -C /opt/

3、编译

[[email protected] /]# 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

4、安装

[[email protected] php-7.1.10]# make && make install

5、修改配置文件

cp php.ini-development /usr/local/php/lib/php.ini   //复制到lib路径下
[email protected] php-7.1.10]# vim /usr/local/php/lib/php.ini
mysqli.default_socket = /usr/local/mysql/mysql.sock  //指定路径
date.timezone = Asia/Shanghai                        //指定时区

四、配置及优化FPM模块

1、配置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

2、检测是否成功开启

netstat -napt | grep 9000

3、创建软连接以便系统识别

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

五、让Nginx支持PHP

[email protected] php-fpm.d]# cd /usr/local/nginx/conf/
[[email protected] 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;
        }

2、重新启动nginx服务

[[email protected] conf]# systemctl restart nginx.service 

3、创建PHP首页

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

LNMP(Nginx MySQL PHP)