lnmp搭建

### 关闭防火墙
[root@VM-4-12-centos init.d]# systemctl stop firewalld

### 已经关闭
[root@VM-4-12-centos init.d]# getenforce
Disabled

### 若没有关闭
setenforce 0

### 安装nginx依赖
[root@VM-4-12-centos init.d]# yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl \
zlib*

###创建运行用户
### Nginx服务程序默认以nobody身份运行,建议为其创建专门的用户账户,以更精确的控制访问权限,增加灵活性、降低安全风险。
[root@VM-4-12-centos init.d]# useradd -M -s /sbin/nologin nginx

###编译安装Nginx
[root@VM-4-12-centos opt]# tar -zxf nginx-1.21.6.tar.gz 
[root@VM-4-12-centos opt]# cd nginx-1.21.6/
[root@VM-4-12-centos nginx-1.21.6]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

### 配置
[root@VM-4-12-centos nginx-1.21.6]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module

### ls 后多了makefile 和 objs
[root@VM-4-12-centos nginx-1.21.6]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
[root@VM-4-12-centos nginx-1.21.6]# cd objs/
[root@VM-4-12-centos objs]# ls
autoconf.err  Makefile  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  src

### make  寻找Makefile make 是用来编译的,它从Makefile中读取指令,然后编译
[root@VM-4-12-centos nginx-1.21.6]# make
[root@VM-4-12-centos nginx-1.21.6]# cd objs/
[root@VM-4-12-centos objs]# ls
autoconf.err  nginx    ngx_auto_config.h   ngx_modules.c  src
Makefile      nginx.8  ngx_auto_headers.h  ngx_modules.o

### make check 或 make test 来进行一些测试
[root@VM-4-12-centos nginx-1.21.6]# make check

### make install make install是用来安装的,它也从Makefile中读取指令,安装到指定的位置
[root@VM-4-12-centos nginx-1.21.6]# make install

### 路径的优化
ln -s /usr/local/nginx/conf/nginx.conf /etc/
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

### 测试nginx配置是否有误
[root@VM-4-12-centos nginx-1.21.6]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

###启动、停止Nginx
###直接运行Nginx即可启动Nginx服务器,这种方式将使用默认的配置文件,若要改用其他配置文件,需添加“-c 配置文件路径”选项来指定路径。需要注意的是,若服务器中已装有httpd等其他Web服务软件,应采取措施(修改端口、停用或卸载)避免冲突
[root@VM-4-12-centos nginx-1.21.6]# nginx
[root@VM-4-12-centos nginx-1.21.6]# netstat -anpt | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6381/nginx: master  
tcp        0      0 10.0.4.12:47792         169.254.0.55:8080       TIME_WAIT   -    
### 添加Nginx系统服务

### 法一:使用chkconfig工具来进行管理

###   vim /etc/init.d/nginx
"""
#!/bin/bash
# chkconfig: - 35 20 80
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
  start)
        $PROG
        ;;
  stop)
        kill -s QUIT $(cat $PIDF)
        ;;
  restart)
        $0 stop
        $0 start
        ;;
  reload)
        kill -s HUP $(cat $PIDF)
        ;;
  *)
        echo "Usage:$0 {start|stop|restart|reload}"
        exit 1
esac
exit 0

"""

### 增加权限
chmod +x /etc/init.d/nginx

### 加入服务
chkconfig --add nginx

### 使用命令看配置好没
[root@VM-4-12-centos nginx-1.21.6]# service nginx stop
### 因为无80端口,所以配置成功
[root@VM-4-12-centos nginx-1.21.6]# netstat -anpt | grep 80

### 所有命令为
service nginx start  开启
service nginx stop   停止
service nginx restart 重启
service nginx reload  重载

### 法二:用systemctl来管理
#添加Nginx系统服务
[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
[root@server1 ~]# systemctl start nginx
[root@server1 ~]# netstat -anpt | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      31826/nginx: master  
[root@server1 ~]# systemctl stop nginx
[root@server1 ~]# netstat -anpt | grep 80

### mysql-boost 5.7
### Mysql5.7版本必须要BOOST库 不过mysql的官网源码有带boost库的源码和不带boost库的源码两种,因此有两种安装方式,其实都是一样的,仅仅是不带boost库源码的需要单独安装boost

###安装mysql环境依赖包
yum -y install ncurses \
ncurses-devel \
bison \
cmake \
make \
gcc \
gcc-c++

### 创建运行用户
useradd -s /sbin/nologin mysql

### 解压
tar xf mysql-boost-5.7.20.tar.gz

### Cmake是用来makefile的一个工具:读入所有源文件之后,自动生成makefile。

[root@server1 opt]# cd mysql-5.7.20/
[root@server1 mysql-5.7.20]#cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \		#指定将 mysql 数据库程序安装到某目录下,如目录/usr/local/ mysql
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \			#指定套接字文件的存储路径,数据库连接的文件
-DSYSCONFDIR=/etc \ 	#指定初始化参数文件目录	
-DSYSTEMD_PID_DIR=/usr/local/mysql \		#指定PID目录 		#
-DDEFAULT_CHARSET=utf8  \		#指定默认使用的字符集编码,如 utf8
-DDEFAULT_COLLATION=utf8_general_ci \		#指定默认使用的字符集校对规则,utf8_general_ci 是适用于 UTF-8 字符集的通用规则
-DWITH_INNOBASE_STORAGE_ENGINE=1 \		#安装INNOBASE存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \		#安装ARCHIVE存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \		#安装BLACKHOLE存储引擎
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \		#安装FEDERATED存储引擎
-DMYSQL_DATADIR=/usr/local/mysql/data \		#数据安装路径
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
[root@server1 mysql-5.7.20]# make && install

### 数据库目录进行权限调整
chown -R mysql.mysql /usr/local/mysql/

### 调整配置文件
[root@server1 mysql-5.7.20]# vi /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

### 配置文件进行权限调整
chown mysql:mysql /etc/my.cnf

### 设置环境变量
echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
echo 'export PATH' >> /etc/profile
source /etc/profile

### 初始化数据库
[root@VM-4-12-centos system]# which mysqld
/usr/local/mysql/bin/mysqld
[root@VM-4-12-centos system]# cd /usr/local/mysql/bin
[root@VM-4-12-centos bin]# mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

### 
[root@VM-4-12-centos bin]# find -name mysqld.service / 
/opt/packages/mysql-5.7.20/scripts/mysqld.service
/usr/local/mysql/usr/lib/systemd/system/mysqld.service
### 分析两个文件,第二个是服务配置
[root@VM-4-12-centos bin]# vim /opt/packages/mysql-5.7.20/scripts/mysqld.service
[root@VM-4-12-centos bin]# vim /usr/local/mysql/usr/lib/systemd/system/mysqld.service

### 服务的总配置
/usr/lib/systemd/system/

### copy一下
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/

###开启mysqld
[root@VM-4-12-centos bin]# systemctl enable mysqld
[root@VM-4-12-centos bin]# systemctl start mysqld
[root@VM-4-12-centos bin]# systemctl status mysqld
 Active: active (running) since 四 2023-04-20 07:59:32 CST; 1s ago
 ### 关闭mysqld
 [root@VM-4-12-centos bin]# systemctl stop mysqld
 ### 重启mysqld
 [root@VM-4-12-centos bin]# systemctl restart mysqld
 
 ### 查看端口
 [root@VM-4-12-centos bin]# netstat -anpt | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      20347/mysqld 
### mysql默认无密码
### 可直接登录,需要按一下回车键 [root@VM-4-12-centos bin]# mysql -uroot -p

### 设置密码
[root@VM-4-12-centos bin]# mysqladmin -u root -p password
Enter password:   按回车键就行
New password:  admin123
Confirm new password:  admin123
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

### 登录带密码
[root@VM-4-12-centos bin]# mysql -u root -p
Enter password: admin123

### 安装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@VM-4-12-centos opt]# tar -xf php-7.1.24.tar.gz
[root@VM-4-12-centos opt]# cd php-7.1.24/
### 配置
[root@VM-4-12-centos opt]# ./configure \
> --prefix=/usr/local/php \		#指定将 PHP 程序安装到哪个目录下
> --with-mysql-sock=/usr/local/mysql/mysql.sock \		#设置Apache服务提供的apxs模块支持程序的文件位置
> --with-mysqli \		#mysqli扩展技术不仅可以调用MySQL的存储过程、处理MySQL事务,而且还可以使访问数据库工作变得更加稳定
> --with-zlib \		#支持zlib功能--压缩流
> --with-curl \		#开启curl扩展功能
> --with-gd \		#激活gd库的支持
> --with-jpeg-dir \		#激活jpeg的支持
> --with-png-dir \		#激活png的支持
> --with-freetype-dir \
> --with-openssl \
> --enable-mbstring \		#启用多字节字符串功能,以便支持中文等代码
> --enable-xml \
> --enable-session \
> --enable-ftp \
> --enable-pdo \
> --enable-tokenizer \
> --enable-zip \
> --enable-fpm		# ##开启php-fpm模块,使nginx能支持动态页面
[root@VM-4-12-centos opt]# make && make install

### make
### make test
### make install

###copy配置文件
[root@VM-4-12-centos php-7.1.24]# cp php.ini-development /usr/local/php/lib/php.ini
[root@VM-4-12-centos php-7.1.24]# vim /usr/local/php/lib/php.ini
date.timezone = Asia/Shanghai      
mysqli.default_socket = /usr/local/mysql/mysql.sock   

### 验证安装的模块
/usr/local/php/bin/php -m

###  配置及优化PHP-FPM模块
[root@VM-4-12-centos php-7.1.24]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@VM-4-12-centos php-7.1.24]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@VM-4-12-centos php-7.1.24]# vim /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid	#去掉前面的注释";"
[root@VM-4-12-centos php-7.1.24]# ln -s /usr/local/php/bin/* /usr/local/bin/
[root@VM-4-12-centos php-7.1.24]# ln -s /usr/local/php/sbin/* /usr/local/sbin/
[root@VM-4-12-centos php-7.1.24]# php-fpm -c /usr/local/php/lib/php.ini 
[root@VM-4-12-centos php-7.1.24]# netstat -anpt | grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      67943/php-fpm: mast 

### 添加PHP-FPM启动设置
[root@VM-4-12-centos php-7.1.24]# vi /etc/init.d/nginx

#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
PROG_FPM="/usr/local/php/sbin/php-fpm"
PIDF_FPM="/usr/local/php/var/run/php-fpm.pid"
case "$1" in
  start)
        $PROG
        $PROG_FPM
        ;;
  stop)
        kill -s QUIT $(cat $PIDF)
        kill -s QUIT $(cat $PIDF_FPM)
        ;;
  restart)
        $0 stop
        $0 start
        ;;
  reload)
        kill -s HUP $(cat $PIDF)
        kill -s HUP $(cat $PIDF_FPM)
        ;;
  *)
        echo "Usage:$0 {start|stop|restart|reload}"
        exit 1
esac
exit 0
[root@VM-4-12-centos php-7.1.24]# systemctl start nginx
[root@VM-4-12-centos php-7.1.24]# netstat -anpt | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1217/nginx: master  
[root@VM-4-12-centos php-7.1.24]# netstat -anpt | grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      1220/php-fpm: maste

###  配置Nginx支持PHP功能
[root@VM-4-12-centos ~]# vim /etc/nginx.conf
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;    
            include        fastcgi.conf;
        }
[root@VM-4-12-centos ~]# vi /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
[root@VM-4-12-centos ~]# systemctl restart nginx

###  测试数据库工作是否正常
将原来的测试网页内容更改如下:
[root@VM-4-12-centos ~]# vim /usr/local/nginx/html/index.php
<?php
$link=mysqli_connect('localhost','root','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>

posted @ 2023-04-20 09:37  Bre-eZe  阅读(9)  评论(0)    收藏  举报