LNMP环境搭建及Nginx多站点配置

举报
灰烬 发表于 2022/02/17 09:25:50 2022/02/17
【摘要】 基于华为云平台搭建LNMP环境以及Nginx多站点配置

=================================

/*

* @平台 | 系统:华为云 | CentOS7

* @版本:nginx-1.20.2

* @描述:LNMP环境搭建,虚拟多站点

*/

=================================

一、LNMP环境搭建

    ① 安装依赖包

# yum install -y libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel ncurses-devel bison gnutls-devel cmake perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel GeoIP GeoIP-devel zlib-devel pcre-devel openssl-devel sqlite-devel freetype-devel libmcrypt-devel recode-devel libicu-devel libzip-devel oniguruma-devel mcrypt libcurl-devel glib2-devel libmemcached-devel xmlrpc-c-devel gmp-devel expat-devel readline-devel libjpeg-devel libpng-devel

    ② 安装nginx1.20.2

    安装步骤略...

    推荐文章:nginx的几种安装方式安装

    ③ 安装Mysql5.6

    安装步骤略...

    推荐文章:

    1. Linux安装MySQL5.6的两种简单方法

    2. 二进制安装数据库MariaDB

    3. 源码编译安装MariaDB

    ④ 编译安装PHP7

# cd /home/softs/
# wget https://www.php.net/distributions/php-7.4.27.tar.gz
# tar -zxvf php-7.4.27.tar.gz
# cd php-7.4.27
# ./configure \
--prefix=/usr/local/php \
--with-config-file-path=/etc \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-curl \
--with-freetype-dir \
--enable-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-jpeg-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--with-bz2 \
--with-mhash \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-xml \
--enable-zip \
--enable-fpm
# make && make install
# 在解压文件夹拷贝文件覆盖原有的配置文件
# cp php.ini-production /etc/php.ini
# cd /usr/local/php/etc/
# cp php-fpm.conf.default php-fpm.conf
# sed -i 's@:pid = run/php-fpm.pid@pid = /usr/local/php/var/run/php-fpm.pid@' php-fpm.conf
# 创建启动文件
# vim /usr/lib/systemd/system/php-fpm.service
# ===content===
[Unit]
Description=The PHP FastCGI Process Manager
After=network.target

[Service]
Type=simple
PIDFile=/usr/local/php/var/run/php-fpm.pid 
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
# ===end===
# systemctl start php-fpm
# netstat -antup |grep php-fpm
tcp   0   0 127.0.0.1:9000   0.0.0.0:*   LISTEN   1800/php-fpm: maste
# cd /etc/nginx/conf.d/
# vim default.conf
# ===content===
# 在location下添加index,php
location / {
        root   网站根目录;
        index  index.html index.htm index.php;
    }
    ...
    location ~ \.php$ {
        root           网站根目录;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  网站根目录$fastcgi_script_name;
        include        fastcgi_params;
    }
# ===end===
# systemctl restart nginx
# cd /home/www
# vim index.php
# ===content===
<?php
phpinfo();
?>
# ===end===
# 测试
# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.20.2
Date: Wed, 16 Feb 2022 03:55:19 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.4.27

二、nginx多站点

# cd /etc/nginx/
# 创建conf.d,配置文件存放目录
# cd conf.d/
# cp default.conf default.conf.old #准备了一个模板文件并做备份
# cp default.conf.old test1.conf
# cp default.conf.old test2.conf
# 修改配置文件,只贴出来了部分
# vim test1.conf
# vim test2.conf
# ===content===
server {
    listen       80;
    server_name  test1.com; #这里是自己的域名

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /home/www/test1; #这里是域名对应网站根目录
        index  index.html index.htm;
    }
# ============================
server {
    listen       80;
    server_name  test2.com; #域名或和公网IP

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /home/www/test2;
        index  index.html index.htm;
    }
# ===end===
# cp nginx.conf nginx.conf.old
# vim nginx.conf
# ===content===
# 找到http{在这里加入以下内容}
include /etc/nginx/conf.d/*.conf;
# ===end===
# 已经做了域名解析,这里做测试
# vim /etc/hosts
# ===content===
127.0.0.1       test1.com
127.0.0.1       test2.com
# ===end===
# mkdir -p /home/www/test1
# echo "test1.com" > /home/www/test1/index.html
# mkdir -p /home/www/test2
# echo "test2.com" > /home/www/test2/index.html
# chown -R nginx:nginx /home/www
# systemctl restart nginx

测试

# curl test1.com
test1.com
# curl test2.com
test2.com

-本篇完-

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。