• 推荐使用最新版火狐浏览器或Chrome浏览器访问本网站
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

网站搭建-20分钟完成阿里云服务器搭建多个网站

建站 紫鹰 3年前 (2021-07-14) 4029次浏览 0个评论 扫描二维码

0x00 网站搭建–阿里云服务器+LNMP配置多个个网站(域名)

作为一个程序猿,拥有自己的网站或者博客是一种什么感觉 …..

创建自己博客或者网站,首先就需要购买空间或者主机和购买域名。

网站空间的价格相对便宜,但是通常都有限制只能建一个博客或者网站,硬盘空间也有限制。主机的价格相对要贵一点,但自由度很大,可以自行配置,适合喜欢折腾的站长。

新站长推荐阿里云服务器腾讯云服务器,一般都会有比较大的优惠,可以一次购买较长时间比如3年。使用个人现有的账号都能直接登录选购;

域名的价格相对比较低,一般几十块钱一年,但国内网站需要对域名进行备案,阿里云腾讯云等都提供域名购买和在线免费自助备案,很方便;

服务器价格高,自由度高,域名便宜,那是否可以再一台服务器上建多个网站,配置多个域名?答案是肯定的;

也就可以跟别人共享主机,或者提供建站和托管服务等;

网站搭建

怎么搭建多个网站

0x01.一建安装LNMP

wget http://soft.vpser.net/lnmp/lnmp1.6.tar.gz -cO lnmp1.6.tar.gz && tar zxf lnmp1.6.tar.gz && cd lnmp1.6 && ./install.sh lnmp

过程中可以选择安装的程序版本,如果没有特殊需求可以使用默认的,设置mysql账号时,要记录设置的密码;

0x02.如何搭建多个网站-WordPress安装

默认情况下,nginx域名的绑定都是通过nginx.conf文件来配置的,但是有时候我们有多个域名或者子域名的情况下要怎么绑定呢?或者有子域名指定不同的目录的时候我们又该怎么设置呢?其实很简单,如果留意看过LNMP一键安装脚本的代码,就知道它的原理。

默认的 nginx.conf 路径: /usr/local/nginx/conf/nginx.conf

如果不知道 nginx 的安装路径,可以通过以下命令来查找:

find / -name nginx.conf

打开 nginx.conf 文件,内容如下:

user  www www;

worker_processes auto;
worker_cpu_affinity auto;

error_log  /home/wwwlogs/nginx_error.log  crit;

pid        /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
{
    use epoll;
    worker_connections 51200;
    multi_accept off;
    accept_mutex off;
}

http
{
    include       mime.types;
    default_type  application/octet-stream;

    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;

    sendfile on;
    sendfile_max_chunk 512k;
    tcp_nopush on;

    keepalive_timeout 60;

    tcp_nodelay on;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 256k;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
    gzip_vary on;
    gzip_proxied   expired no-cache no-store private auth;
    gzip_disable   "MSIE [1-6]\.";

    #limit_conn_zone $binary_remote_addr zone=perip:10m;
    ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

    server_tokens off;
    access_log off;

server
{
    listen 80 default_server reuseport;
    #listen [::]:80 default_server ipv6only=on;
    server_name _;
    index index.html index.htm index.php;
    root  /home/wwwroot/default;

    #error_page   404   /404.html;

    # Deny access to PHP files in specific directory
    #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

    include enable-php.conf;

    location /nginx_status
    {
        stub_status on;
        access_log   off;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
    }

    location ~ /.well-known {
        allow all;
    }

    location ~ /\.
    {
        deny all;
    }

    access_log  /home/wwwlogs/access.log;
}
include vhost/*.conf;
}

使用阿里云服务器的需要注意屏蔽 #listen [::]:80 defaultserver ipv6only=on; 这一行是配置IPV6的,阿里云服务器开启IPV6会有问题;

这个文件重点倒数第二行 include vhost/*conf; 要开启,LNMP安装时候默认就是开启的,在配置的时候只需要检查确认一下即可;

这一行配置的意思是执行 vhost 目录下的所有.conf文件,所以在vhost目录下新建域名的配置文件,可以多个域名一个文件,也可以一个域名一个文件。比如在vhost目录下新建一个www.abc.com.conf 的配置文件。

server
{
    listen 80;
    listen 443 ssl;
    #listen [::]:80;
    server_name www.abc.com abc.com;
    index index.html index.htm index.php default.html default.htm default.php;
    root  /opt/www/www.abc.com;
    ssl_certificate /usr/local/nginx/cert/www.abc.com.pem;
    ssl_certificate_key /usr/local/nginx/cert/www.abc.com.key;

    include rewrite/wordpress.conf;
    #error_page   404   /404.html;

    # Deny access to PHP files in specific directory
    #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

    include enable-php-pathinfo.conf;

    location ~* ^.+\.(gif|jpg|jpeg|png|swf|flv|mp3|mp4|ogg|flav|wav|rar|zip)$ {
        valid_referers none blocked  www.abc.com;
        if ($invalid_referer) {
            return 404;
            break;
        }
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
    }

    location ~ /.well-known {
        allow all;
    }

    location ~ /\.
    {
        deny all;
    }

    access_log  /opt/wwwlogs/www.abc.com.access.log;
    error_log  /opt/wwwlogs/www.abc.com.error.log;
}

listen 80; 监听80端口;

server_name 后面是域名或者子域名,可以是多个,但指向的都是一个目录;

index 默认文件,即网站根目录中的一个文件,是网站的入口,如有多个按顺序打开;

root 这一行配置网站指向的根目录;
对于磁盘空间小的,可以关闭 log 功能,即屏蔽以下两行,需要时再打开:

access_log  /opt/wwwlogs/www.abc.com.access.log;
error_log  /opt/wwwlogs/www.abc.com.error.log;

注意:include rewrite/wordpress.conf; 这一行,如果不是wordpress网站,需要去掉,否者会导致正常文件访问不到,而现实不了网页;

注意以下配置:

location ~* ^.+\.(gif|jpg|jpeg|png|swf|flv|mp3|mp4|ogg|flav|wav|rar|zip)$ {
        valid_referers none blocked  www.abc.com;
        if ($invalid_referer) {
            return 404;
            break;
        }
    }

这个地方要记得修改域名,否者网站上的所有媒体资源无法显示;

这个新建的配置文件里需要根据自己的域名自行修改;

0x03. 重启

配置完 conf 文件后,重启 nginxphp (Centos 为例)

service nginx restart
service php-fpm restart

Ubuntu:

/etc/init.d/nginx restart

版权所有丨如未注明 , 均为原创丨
转载请注明原文链接:网站搭建-20分钟完成阿里云服务器搭建多个网站
喜欢 (95)
[谢谢打赏!]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址