在单台主机上配置LNMP

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在单台主机上配置LNMP相关的知识,希望对你有一定的参考价值。

在单台主机上配置LNMP

编译安装nginx

1.登录官网获取下载链接直接wget

[[email protected] ~]# wget http://nginx.org/download/nginx-1.17.0.tar.gz

2.解压文件

[[email protected] ~]# tar xf nginx-1.17.0.tar.gz

3.检查当前环境是否符合编译要求,并生成makefile文件

[[email protected] ~]# cd nginx-1.17.0
[[email protected] nginx-1.17.0]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module  --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

4.根据makefile文件内容生成指定的模块

[[email protected] nginx-1.14.2]# make

5.将生成的模块复制到相应的目录,如果目录不存在会创建目录

[[email protected] nginx-1.14.2]# make install 

6.将执行文件关联到sbin下

[[email protected] nginx-1.17.0]# ln -s /apps/nginx/sbin/nginx /sbin/nginx

7.配置服务脚本
使用yum安装的服务脚本进行修改

[[email protected] ~]# vim /usr/lib/systemd/system/nginx.service 

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid  #修改为nginx编译安装的目录
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/apps/nginx/sbin/nginx -t  #修改nginx的路径
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf     #修改nginx路径,并添加配置文件
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

8.创建nginx用户

[[email protected]t ~]# useradd -u 2000 nginx 

9.修改配置文件

[[email protected] ~]# vim /apps/nginx/conf/nginx.conf
user  nginx;            #修改用户为nginx
pid        /apps/nginx/logs/nginx.pid;      #修改pid文件存放路径与服务脚本内路径相同

10.启动服务

[[email protected] ~]# systemctl start nginx
[[email protected] ~]# ss -tnl | grep 80
LISTEN     0      128          *:80                       *:*  

编译安装php-fpm

1.解压文件

[[email protected] ~]# tar xf php-7.3.5.tar.bz2 

2.检查当前环境是否符合编译要求,并生成makefile文件

[[email protected] ~]# cd php-7.3.5
[[email protected] php-7.3.5]# ./configure --prefix=/app/php --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo

3.生成模块并将模块复制到指定位置

[[email protected] ~]# make && make install

4.复制启动配置文件并修改

[[email protected] php-7.3.5]# cp php.ini-production /etc/php.ini
[[email protected] php-7.3.5]# sed -i ‘/;date.tim/[email protected]*@data.timezone = "Asia/Shanghai"@‘ /etc/php.ini

5.复制服务器脚本,并配置为开机启动

[[email protected] php-7.3.5]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[[email protected] php-7.3.5]# chmod +x /etc/init.d/php-fpm
[[email protected] php-7.3.5]# chkconfig --add /etc/init.d/php-fpm

6.复制php配置文件

[[email protected] php-7.3.5]# cp /app/php/etc/php-fpm.conf.default /app/php/etc/php-fpm.conf
[[email protected] php-7.3.5]# cp /app/php/etc/php-fpm.d/www.conf.default /app/php/etc/php-fpm.d/www.conf

配置nginx和php

1.修改php配置文件

[[email protected] ~]# vim /app/php/etc/php-fpm.d/www.conf
user = ngnix
group = nginx
listen.allowed_clients = 127.0.0.1

2.修改nginx配置文件

[[email protected] ~]# mkdir /apps/nginx/conf/server
[[email protected] ~]# vim /apps/nginx/conf/server/mylinuxops.conf
[[email protected] ~]# vim /apps/nginx/conf/server/mylinuxops.conf
server 
    server_name www.mylinuxops.com;
    listen 80;
    location / 
        root /data/www;
        index index.html;
    
    location ~ \.php$ 
        root /data/www/php;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    

3.在nginx主配置文件中将server的配置导入

[[email protected] ~]# vim /apps/nginx/conf/nginx.conf
http 
    ......
include /apps/nginx/conf/server/*.conf;

4.启动nginx和php-fpm

[[email protected] ~]# service php-fpm start
[[email protected] ~]# systemctl start nginx 

5.测试
创建测试页面

[[email protected] ~]# echo www.mylinuxops.com > /data/www/index.html
[[email protected] ~]# vim /data/www/php/index.php
<?php
phpinfo();
?>

6.访问测试页面
技术图片技术图片

二进制安装MySQL

1.创建MySQL用户和组

[[email protected] ~]# groupadd -r  mysql
[[email protected] ~]# useradd -g mysql -r -s /sbin/nologin mysql

2.解压文件到/usr/local目录下

[[email protected] ~]# tar xf mariadb-10.2.23-linux-x86_64.tar.gz -C /usr/local/

3.对目录创建软连接并修改属主属组

[[email protected] ~]# cd /usr/local/
[[email protected] local]# ln -s mariadb-10.2.23-linux-x86_64 mysql
[[email protected] local]# chown -R root.root mysql

4.复制配置文件模板,并修改

[[email protected] local]# mkdir /etc/mysql
[[email protected] local]# cp mysql/support-files/my-huge.cnf /etc/mysql/my.cnf
[[email protected] mysql]# vim /etc/mysql/my.cnf 
datadir=/data/mysql         #指定数据库目录

5.复制服务启动脚本,并配置为开机自动启动

[[email protected] local]# cp mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] local]# chkconfig --add mysqld

6.创建数据库目录并配置为安全权限

[[email protected] local]# mkdir /data/mysql
[[email protected] local]# chown -R  mysql.mysql /data/mysql
[[email protected] local]# chmod 700 /data/mysql

7.初始化数据库

[[email protected] mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql 

8.启动数据库服务

[[email protected] mysql]# service mysqld start
Starting mysqld (via systemctl):                           [  OK  ]

测试lnmp

1.创建测试页面

[[email protected] mysql]# vim /data/www/php/index.php 
<?php
$dsn=‘mysql:host=127.0.0.1;dbname=test‘;
$username=‘mysql‘; $passwd=‘‘;
$dbh=new PDO($dsn,$username,$passwd);
var_dump($dbh);
phpinfo();
?>

2.使用浏览器访问
技术图片

以上是关于在单台主机上配置LNMP的主要内容,如果未能解决你的问题,请参考以下文章

将 Ansible 剧本安全地限制在单台机器上?

在单台服务器上搭建elasticsearch集群

如何使 c# windows 窗体应用程序仅在单台 PC 上运行?

是否可以在单台计算机上为 Atlassian Bamboo 安装多个远程代理?

尝试在单台 Mac 机器上使用并行 Jenkins 管道构建 iOS 应用程序时出现缓存问题

如何在单台机器上管理/组合多台服务器?