LNMP-compile and install architecture

One, LNMP architecture

1. Install Nginx

Prepare the software package

cd /opt
放入软件包
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
yum -y install pcre-devel zlib-devel gcc gcc-c++ make

Insert picture description here

3. Create and run users and groups and compile and install Nginx

(The Nginx service program runs as nobody by default. It is recommended to create a special user account for it to control its access permissions more accurately)

useradd -M -s /sbin/nologin nginx

cd /opt
tar zxvf nginx-1.12.0.tar.gz 

cd nginx-1.12.0/
./configure \
--prefix=/usr/local/nginx \				      #指定nginx的安装路径
--user=nginx \										#指定用户名
--group=nginx \										#指定组名
--with-http_stub_status_module						#启用 http_stub_status_module 模块以支持状态统计
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/		#让系统识别nginx的操作命令

Insert picture description here
Insert picture description here

3. Check, start, restart, and stop the nginx service

nginx -t								#检查配置文件是否配置正确
nginx							#启动		

查看nginx的进程号
四种方式
cat /usr/local/nginx/logs/nginx.pid 
ss -ntpl | grep 80        //推荐使用,读取速度快
netstat -natp | grep 80
netstat -natp | grep nginx
lsof -i :80

kill -3 <PID号>
kill -s QUIT <PID号>				#停止
killall -3 nginx
killall -s QUIT nginx

kill -1 <PID号>					#重载
kill -s HUP <PID号>
killall -1 nginx
killall -s HUP nginx
#日志分隔,重新打开日志文件
kill -USR1 <PID号>
#平滑升级
kill -USR2 <PID号>

Insert picture description here

4. Add Nginx system service

method one:

vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: - 99 20
#description:Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
  $COM
;;

stop)
  kill -s QUIT $(cat $PID)
;;

restart)
  $0 stop
  $0 start
;;

reload)
  kill -s HUP $(cat $PID)
;;

*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1

esac
exit 0


chmod +x /etc/init.d/nginx
chkconfig --add nginx							#添加为系统服务
systemctl stop nginx
systemctl start nginx

Method Two:

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

chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service

Insert picture description here
Insert picture description here

Two, install MySQL service

1. Install the Mysql environment dependency package and create a user

yum -y install \
ncurses \
ncurses-devel \
bison \
cmake
或者:
yum -y install  ncurses ncurses-devel bison cmake
useradd -M -s /sbin/nologin mysql

Insert picture description here

2. Compile and install Mysql

cd /opt
tar zxvf mysql-boost-5.7.20.tar.gz

cd /opt/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    //编译安装

Insert picture description here

3. Adjust the permissions of the mysql data directory

chown -R mysql:mysql /usr/local/mysql

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

Insert picture description here

4. Set environment variables, and output, refresh the file

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

Insert picture description here

5. Initialize the data file

cd /usr/local/mysql/bin
mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

Insert picture description here

6. Use systemctl for service control and restart the service

cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld.service  #开启服务
systemctl enable mysqld         #开机自启动
netstat -anpt | grep 3306       #查看端口

Insert picture description here

7. Set the password of the mysql database and log in

mysqladmin -u root -p password "123"

mysql -u root -p 
grant all privileges on *.* to 'root'@'%' identified by '123';
#授予root用户可以在所有终端远程登录,使用的密码是abc123,并对所有数据库和所有表有操作权限
show databases;   #查看当前已有的数据库

Insert picture description here

Three, install PHP

1. Decompress and install the dependency package

tar jxvf php-7.1.10.tar.bz2

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

Insert picture description here

3. Configure the module

cd 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

make && make install

Insert picture description here

4. Modify the installation file

1.路径优化
ln -s /usr/local/php/bin/* /usr/local/bin/
ln -s /usr/local/php/sbin/* /usr/local/sbin/
2.调整PHP配置文件
php有三个配置文件:
php.ini        主配置文件
php-fpm.conf   进程服务配置文件
www.conf       扩展展配置文件

//调整主配置文件
cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini 
//在测试环境时使用php.ini-development文件,而在生产环境时使用php.ini-production文件
vim /usr/local/php/lib/php.ini
--1170行--修改
mysqli.default_socket = /usr/local/mysql/mysql.sock
--939行--取消注释,修改
date.timezone = Asia/Shanghai

php -m   //验证安装的模块  

//调整进程服务配置文件:
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
——17行——去掉“;”注释
pid = run/php-fpm.pid

//调整扩展配置匹配文件
cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf

Insert picture description here
Insert picture description here

Insert picture description here

5. Start php-fpm, configure Nginx to support PHP parsing

PHP-FPM (FastCGI Process Manager: FastCGI Process Manager) is a PHPFastCGI manager. Since the Nginx server cannot handle dynamic pages, Nginx needs to pass dynamic requests to the php-fpm process for analysis.

/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
netstat -natp | grep 9000

vim /usr/local/nginx/conf/nginx.conf
——65行——取消注释,修改

ocation ~ \.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;  //将/scripts修改为
            nginx的工作目录
            include        fastcgi_params;
        }

Insert picture description here
Insert picture description here

6. Create a home page and restart nginx

vim /usr/local/nginx/html/index.php
//可以改成显示php配置的web网页
<?php
phpinfo();
?>

systemctl restart nginx
http://192.168.221.15/index.php

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/s15212790607/article/details/115308194