Lnmp搭建(三台主机)
环境说明:
系统 | IP | 需要安装的服务 |
---|---|---|
centos8 | 192.168.174.168 | nginx-1.20.2 |
centos8 | 192.168.174.171 | mysql-5.7 |
centos8 | 192.168.174.173 | php-8.1.11 |
nginx安装与配置
nginx安装
//首先关闭防火墙和selinux
[root@nginx ~]# setenforce 0
[root@nginx ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@nginx ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
//创建用户
[root@nginx ~]# useradd -rMs /sbin/nologin nginx
//安装所需要的依赖包
[root@nginx ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim
//下载nginx源码包,并解压
[root@nginx ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
[root@nginx ~]# tar -xf nginx-1.20.2.tar.gz
[root@nginx ~]# cd nginx-1.20.2/
[root@nginx nginx-1.20.2]#
//准备安装
##在配置前我们其实可以隐藏一下nginx版本号,以防版本泄露
[root@nginx nginx-1.20.2]# vim src/core/nginx.h
#define NGINX_VERSION "111" //修改这个参数隐藏nginx版本号
//开始配置
[root@nginx nginx-1.20.2]# ./configure \
> --prefix=/usr/local/nginx \ //指定安装路径
> --user=nginx \ //指定用户和组
> --group=nginx \
> --with-debug \ //开启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
//开启编译安装
[root@nginx nginx-1.20.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
.........
//安装成功
[root@nginx nginx-1.20.2]# cd /usr/local/nginx/
[root@nginx nginx]# ls
conf html logs sbin
nginx配置
//配置环境变量
[root@nginx nginx]# echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh
[root@nginx nginx]# source /etc/profile.d/nginx.sh
//启动服务
[root@nginx nginx]# nginx
[root@nginx nginx]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
//关闭服务,编写service文件,并设置服务开机自启
[root@nginx nginx]# nginx -s stop
[root@nginx nginx]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@nginx nginx]# cat > /usr/lib/systemd/system/nginx.service << EOF
> [Unit]
> Description=nginx server daemon
> After=network.target
>
> [Service]
><