本文主要是介绍Nginx的lnmp服务搭建,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言:
nginx是一款功能强大的web服务器。和apache一样,有http和https两个服务。nginx功能强大,且库使用第三方模块,正反向代理等。Nginx默认采用多进程工作方式,Nginx启动后,会运行一个master进程和多个worker进程。其中master充当整个进程组与用户的交互接口,同时对进程进行监护,管理worker进程来实现重启服务、平滑升级、更换日志文件、配置文件实时生效等功能。worker用来处理基本的网络事件,worker之间是平等的,他们共同竞争来处理来自客户端的请求。
关掉防火墙和selinux,时间同步
nginx:192.168.121.30
mysql:192.168.121.20
php:192.168.121.50
一、nginx的搭建
1.1编译安装nginx,下载安装包,创建文件、用户、目录
[root@node3 ~]# hostnamectl hostname nginx
[root@node3 ~]# bash
[root@nginx ~]# useradd -r -M -s /sbin/nologin nginx
[root@nginx ~]# yum -y groupinstall "Development Tools"
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make zlib-devel wget lrzsz
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx/
1.2解压,安装
[root@nginx ~]# tar -zxvf nginx-1.24.0.tar.gz -C /usr/local/
[root@nginx ~]# cd /usr/local/nginx-1.24.0/
到该目录下面进行配置功能,指定工作目录,用户和组
[root@nginx nginx-1.24.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-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 \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@nginx nginx-1.24.0]# make
[root@nginx nginx-1.24.0]# make install
[root@nginx ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' >> /etc/profile
[root@nginx ~]# source /etc/profile
配置环境变量。可以在任何目录实验nginx命令
服务控制方式,使用nginx命令
1.3nginx基本命令
-t 检查配置文件语法
-v 输出nginx的版本
-c 指定配置文件路径
-s 发送服务控制信号,可选值有stop、quit、reopen、reload
[root@nginx ~]# nginx
启动nginx
[root@nginx ~]# ss -anlt
在浏览器打开访问页面
二、部署mysql
2.1编译安装mysql
[root@node2 ~]# hostnamectl hostname mysql
[root@node2 ~]# bash
[root@mysql ~]# yum -y install libncurses* libaio lrzsz tar
[root@mysql ~]# tar -xzvf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@mysql ~]# cd /usr/local/
[root@mysql local]# ln -sv mysql-5.7.37-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.37-linux-glibc2.12-x86_64/'
[root@mysql local]# useradd -r -M -s /sbin/nologin mysql
[root@mysql local]# chown -R mysql.mysql /usr/local/mysql
[root@mysql local]# mkdir /opt/data
[root@mysql local]# chown -R mysql.mysql /opt/data
前面文章解释过,我就不写,怎么了
[root@mysql local]# echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
[root@mysql local]# source /etc/profile
[root@mysql local]# mysqld --initialize-insecure --user=mysql --datadir=/opt/data/
[root@mysql local]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
[root@mysql local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@mysql local]# ldconfig
[root@mysql ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
[root@mysql ~]# cat /etc/my.cnf
[root@mysql ~]# sed -ri "s#^(basedir=).*#\1/usr/local/mysql#g" /usr/local/mysql/support-files/mysql.server
[root@mysql ~]# sed -ri "s#^(datadir=).*#\1/opt/data#g" /usr/local/mysql/support-files/mysql.server
[root@mysql local]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysql server daemon
After=network.targe
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target
[root@mysql ~]# systemctl daemon-reload
[root@mysql ~]# systemctl restart mysqld
[root@mysql ~]# systemctl enable mysqld
[root@mysql ~]# vim /etc/man_db.conf
改一下命令文件位置
MANDATORY_MANPATH /usr/local/mysql/man
[root@mysql ~]# mysql -uroot -p
初始化后是空密码,直接回车
mysql> set password=password('123');
设置密码
三、安装php
3.1安装php依赖包还有连接数据库和nginx的工具等
#yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel php-mysqlnd
[root@node5 ~]# hostnamectl hostname php
[root@node5 ~]# bash
[root@node5 ~]#yum -y install php-*
安装php
3.2修改php配置文件,编写apache网页
[root@php ~]# vim /etc/php-fpm.d/www.conf
改两个地方
listen = 0.0.0.0:9000 //自身的9000端口打开,
listen.allowed_clients = 192.168.121.30 //填写nginx的客户端
[root@php ~]# vim /var/www/html/index.php //为什么是apche用户,前面安装的依赖包的时候创建的
<?php
phpinfo();
?>
[root@php ~]# chown -R apache.apache /var/www/html/
[root@php ~]# systemctl restart php-fpm.service
[root@php ~]# systemctl enable php-fpm.service
3.3配置nginx的反向代理
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html; #更改目录
index index.php index.html index; #添加一个index.php,添加文件名称
}
location ~ \.php$ { //这里~代表正则表达式,正则表达式就是检查文本匹配,反斜杠是转义后面那个点的,能识别为以点php结尾的任意名字。
root /var/www/html;#更改目录
fastcgi_pass 192.168.100.30:9000; #这里为PHP服务器的地址
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name; #前面设置目录
include fastcgi_params;
}
index index.php index.html index.htm;
}
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 192.168.121.50:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
[root@nginx ~]# nginx -t 检查为ok
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successfu
[root@nginx ~]# nginx -s stop 停止服务
[root@nginx ~]# nginx
四、测试结果与模糊后缀
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
index index.php index.html index.htm;
}
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 192.168.121.50:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
index index.php index.html index.htm;
}
location ~ 123$ {
root /var/www/html;
fastcgi_pass 192.168.121.50:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
必须是123结尾的才能访问,且在index.php文件目录下
扩展与思考:
安装php的页面在192.168.121.50不能访问,但是配置反向代理访问就能访问。而且192.168.121.30ip访问的是192.168.121.50的网站。那么192.168.121.50的网页能不能访问呢,答案是不能,首先没有80端口,其次其他的端口不是网页端口,使用192.168.121.30上的nginx的80将网页显示出来。
这篇关于Nginx的lnmp服务搭建的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!