lnmp部署

 nginx部署

//创建nginx用户
[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

//解压nginx源码包
[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 nginx-1.24.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh

//启动nginx,并查看端口情况
[root@nginx nginx-1.24.0]# nginx
[root@nginx nginx-1.24.0]# ss -anlt

  • 配置文件详解

主配置文件:/usr/local/nginx/nginx.conf

默认启动nginx时,使用的配置文件是:安装路径/conf/nginx.conf

可以在启动nginx时通过-c选项来指定要读取的配置文件

nginx常见的配置文件及其作用:

nginx.conf             // nginx的基本配置文件

mime.types           //MIME类型关联的扩展文件

fastcgi.conf           //与fastcgi相关的配置

proxy.conf            //与proxy相关的配置

sites.conf             //配置nginx提供的网站,包括虚拟主机

Nginx.conf配置文件详解

main配置段:全局配置段,其中main配置段中可能包含event配置段

events{}:定义event模型工作特性

http{}:定义http协议相关的配置

支持使用变量:

  1. 内置变量:模块会提供内建变量定义
  2. 自定义变量:set var_name value

用于调试、定位问题的配置参数

daemon  {on|off};   //是否以守护进程方式运行nginx,调试应设置为off

master_process  {on|off}; //是否以master/worker模型来运行nginx,调试时可以设置为off

error_log 位置  级别;    //配置错误日志

error_log里的位置和级别的一些选项

位置

级别

file

stderr

syslog:server=address[,parameter=vaule]

memory:size

debug:若要使用debug级别,需要在编译nginx时使用--with-debug选项

info

notice

warn

error

crit

alert

emerg

正常运行必备的配置参数

user  USERNAME  [GROUPNAME]; //指定运行worker进程的用户和组

pid   /path/to/pid_file; //指定nginx守护进程的pid文件

worker_rlimit_nofile  number; //设置所有worker进程最大可以打开的文件数,默认为1024

worker_rlimit_core  size; //指明所有worker进程所能够使用的总体的最大核心文件大小,保持默认即可

优化性能的配置参数

worker_processes  n; //启动n个worker进程,这里的n为了避免上下文切换,通常设置为cpu总核心数-1或等于总核心数

worker_cpu_affinity cpumask ...; //将进程绑定到某cpu中,避免频繁刷新缓存

#cpumask:使用8位二进制表示cpu核心,如:

    0000 0001   #第一颗cpu核心

    0000 0010   #第二颗cpu核心

    0000 0100   #第三颗cpu核心

    0000 1000   #第四颗cpu核心

    0001 0000   #第五颗cpu核心

    0010 0000   #第六颗cpu核心

    0100 0000   #第七颗cpu核心

    1000 0000   #第八颗cpu核心

timer_resolution  interval;  //计时器解析度。降低此值,可减少gettimeofday()系统调用的次数

worker_priority  number; //指明worker进程的nice值

事件相关的配置:event{}段中的配置参数

accept_mutex {off|on}; //master调度用户请求至各worker进程时使用的负载均衡锁;on表示能让多个worker轮流的、序列化的去响应新请求

lock_file file; //accept_mutex用到的互斥锁所文件路径

use  [epoll | rtsig | select | poll]; //指明使用的事件模型,建议让nginx自行选择

worker_connections  1024;  //每个进程能够接受的最大连接数

网络连接相关的配置参数

keepalive_timeout number;    //长连接的超时时长,默认为65s

keepalive_requests number;    //在一个长连接上所能够允许请求的最大资源数

keepalive_disable [msie6|safari|none];    //为指定类型的UserAgent禁用长连接

tcp_nodelay on|off;    //是否对长连接使用TCP_NODELAY选项,为了提升用户体验,通常设为on

client_header_timeout number;    //读取http请求报文首部的超时时长

client_body_timeout number;    //读取http请求报文body部分的超时时长

send_timeout number;    //发送响应报文的超时时长

Fastcgi的相关配置参数

LNMP:php要启用fpm模型

location  ~  \.php$ {

  root html;

  fastcgi_pass 127.0.0.1:9000;      //定义反向代理

  fastcgi_index index.php;

  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

  include fastcgi_params;

}

常需要进行调整的参数

worker_processes

worker_connections

worker_cpu_affinity

worker_priority

http{....}:配置http相关,由ngx_http_core_module模块引入。Nginx的HTTP配置主要包括4个区块

http {            //协议级别

  include mime.types;

  default_type application/octet-stream;

  keepalive_timeout 65;

  gzip on;

  upstream {          //负载均衡配置

    ...

  }

  server {          //服务器级别,每个server类似于httpd中的一个<VirtualHost>

    listen 80;

    server_name localhost;

    location / {       //请求级别,类似于httpd中的<Location>,用于定义URL与本地文件系统的映射关系

      root html;

      index index.html index.htm;

    }

  }

}

http{}端配置指定

server{}:定义一个虚拟主机

server  {

listen  80;

server_name  www.example.com;

root  “/vhost/web”;

}

listen: 指定监听的地址和端口

listen  address[:port]

listen  port;

 mysql部署

//部署好mysql
[root@mysql2 ~]# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@mysql2 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye

//配置文件 
[root@mysql2 ~]# vim /etc/profile
MANDATORY_MANPATH                       /usr/local/mysql/man
[root@mysql2 ~]# vim /etc/man_db.conf 
/usr/local/mysql/lib

 php部署

//安装依赖包
[root@node3 ~]#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

//安装php
[root@node3 ~]#yum -y install php-*

//配置php
[root@php3 ~]# vim /etc/php-fpm.d/www.conf
listen = 0.0.0.0:9000
listen.allowed_clients = 192.168.35.142

//生成测试页面,修改所有者所属组,并重启服务
[root@php3 ~]# vim /var/www/html/index.php
[root@php3 ~]# chown -R apache.apache /var/www/html/
[root@php3 ~]# systemctl restart php-fpm.service
[root@php3 ~]# systemctl enable php-fpm.service
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.

//回到nginx那台主机
//整合nginx和php
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
            root       html;  //更改目录
            index  index.php index.html index;  //添加一个index.php
        }
        location ~ \.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;
        }

//测试nginx配置文件,重启服务
[root@nginx ~]# 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
[root@nginx ~]# nginx  -s stop
[root@nginx ~]# nginx

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ansible角色是Ansible中的一种组织方式,它是一组任务、变量、文件和模板的集合,用于实现某个特定功能。在这个问题中,我们可以使用Ansible角色来部署LNMP(Linux、Nginx、MySQL和PHP)堆栈。 以下是一个简单的LNMP Ansible角色部署示例: 1. 创建一个名为“lnmp”的Ansible角色目录: ``` mkdir roles/lnmp ``` 2. 在lnmp目录中创建一个tasks目录: ``` mkdir roles/lnmp/tasks ``` 3. 在tasks目录中创建一个main.yml文件,其中包含以下任务: ``` - name: Install Nginx yum: name: nginx state: present - name: Start Nginx service service: name: nginx state: started - name: Install MySQL yum: name: mysql-server state: present - name: Start MySQL service service: name: mysqld state: started - name: Install PHP yum: name: php state: present - name: Install PHP-FPM yum: name: php-fpm state: present - name: Start PHP-FPM service service: name: php-fpm state: started ``` 这些任务将安装和启动Nginx、MySQL和PHP-FPM服务。 4. 在lnmp目录中创建一个vars目录: ``` mkdir roles/lnmp/vars ``` 5. 在vars目录中创建一个main.yml文件,其中包含以下变量: ``` --- nginx_conf_file: /etc/nginx/nginx.conf mysql_root_password: mysecretpassword php_conf_dir: /etc/php.d/ ``` 这些变量将用于配置Nginx、MySQL和PHP的设置。 6. 在lnmp目录中创建一个templates目录: ``` mkdir roles/lnmp/templates ``` 7. 在templates目录中创建一个nginx.conf.j2模板文件: ``` worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; server { listen 80; server_name localhost; location / { root /var/www/html; index index.php index.html index.htm; } location ~ \.php$ { root /var/www/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } ``` 这个模板将用于生成Nginx的配置文件。 8. 在tasks目录中创建一个configure.yml文件,其中包括以下任务: ``` - name: Copy Nginx configuration file template: src: nginx.conf.j2 dest: "{{ nginx_conf_file }}" mode: '0644' - name: Set MySQL root password mysql_user: name: root password: "{{ mysql_root_password }}" login_unix_socket: /var/lib/mysql/mysql.sock - name: Copy PHP configuration file copy: src: php.ini dest: "{{ php_conf_dir }}" mode: '0644' ``` 这些任务将生成Nginx配置文件、设置MySQL root密码和复制PHP配置文件。 9. 在lnmp目录中创建一个files目录: ``` mkdir roles/lnmp/files ``` 10. 在files目录中创建php.ini文件: ``` memory_limit = 128M upload_max_filesize = 64M post_max_size = 64M ``` 这个文件将被复制到PHP配置目录中。 11. 在lnmp目录中创建一个meta目录: ``` mkdir roles/lnmp/meta ``` 12. 在meta目录中创建一个main.yml文件,其中包含以下元数据: ``` --- dependencies: - { role: geerlingguy.repo-epel } - { role: geerlingguy.mysql } ``` 这些元数据将指定依赖项,以便安装EPEL存储库和MySQL角色。 13. 在playbook中使用lnmp角色: ``` - hosts: webserver become: true roles: - lnmp ``` 这个playbook将在webserver主机上使用lnmp角色。 这就是一个简单的LNMP Ansible角色部署示例。当然,还有很多其他的配置选项和任务可以添加到这个角色中,以满足不同的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值