最近重新构建家里的 homelab,在 PVE 上建些虚拟机来跑玩具。

先从最基础的 WebServer 开始吧,编译安装一套基础的 LNMP 来用。

服务器初始化

登录信息

先写入自己的公钥

echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDO1/rzvqYn1Jm/b4LvKN7TviSU2lE1gJagkNiFdcSVuACiIRxpmYfdi22ePm+USbDzv4zUAfkHiya6xoELaoM7zx1Du/WxFGN+fcFND3Xwba5icGmmRPxH641YSyIzyL9R/aNFfnXGIRRVwplNVgB7VupIzOu4PX4HiADvI+DHn/qejSeurbXkwe55QHiMpncdJy7gi4AvgOLt1kiwQWlYNpWwRsi1yYrwNTUr2AoIzmkTHDurM6t6WPCo/w1sSNaH84j9SZhcv+p8Bos3UtLRWZPHl0GuhTrqcjjETdfaalngBQS9tvATJ04GqlvrTUOLuQiMlq3EbMcGBGoemCqx liqing@rMBP24.local" >> ~/.ssh/authorized_keys

养成良好习惯,关闭密码登录

vim /etc/ssh/sshd_config
PasswordAuthentication no
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys

安装软件

安装一下 epel 仓库和常用的软件

yum -y install epel-release
yum clean all && yum makecache
yum install vim curl wget git screen htop iftop zsh -y

安装 nginx

下载源码

先去 nginx 的官网,查看一下最新的稳定版源码下载地址,当前 1.16.1 是最新版。

mkdir nginx && cd nginx
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar zxf nginx-1.16.1.tar.gz

扩展方面好像没有别的特殊需要的,只装个 brotli 的扩展就好。

git clone https://github.com/google/ngx_brotli.git
cd ngx_brotli
git submodule update --init
cd ..

编译安装

装些编译的时候要用的软件

yum install pcre pcre-devel openssl openssl-devel gcc gcc-c++ automake zlib zlib-devel -y

生成一下 Makefile

cd nginx-1.16.1
./configure --add-module=../ngx_brotli --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module

提示了一堆默认路径和配置,记下来省得以后还要猜或者找

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

没什么要改的了,直接编译安装

make && make install

为二进制文件创建个软链接

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

配置和启动

创建个 www 用户给nginx 和将来的 php 用

useradd -s /sbin/nologin -M www

稍微修改一下 nginx 的配置文件

vim /usr/local/nginx/conf/nginx.conf

大致修改如下

# 使用新建的 www 用户运行
user  www;
# pid 文件与接下来要修改的 systemd 配置文件一致
pid   /run/nginx.pid;

# server 段内
# 打开 gzip 压缩
    gzip  on;

# 打开 brotli 压缩,以及相关的压缩配置
    brotli on;
    brotli_comp_level 6;
    brotli_min_length   512;
    brotli_types text/plain text/javascript text/css text/xml text/x-component application/javascript application/x-javascript application/xml application/json application/xhtml+xml application/rss+xml application/atom+xml application/x-font-ttf application/vnd.ms-fontobject image/svg+xml image/x-icon font/opentype;
    brotli_static       always;

配置下 systemd 脚本

vim /lib/systemd/system/nginx.service

内容参考 nginx 官方给的样本

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

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/nginx -t
ExecStart=/usr/bin/nginx
ExecReload=/usr/bin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

然后就可以使用 systemctl 管理 nginx 服务了

systemctl status nginx
systemctl start nginx
systemctl restart nginx
systemctl reload nginx
systemctl stop nginx
systemctl enable nginx

配置 firewalld

启动 nginx 后,由于大部分 CentOS 都默认启用了 firewalld,外部机器默认还不能访问。
接下来就参考 这篇文章 对 firewalld 进行一下配置。

首先查看当前的网卡接口所处的 zone ,以及 zone 的配置

firewall-cmd --get-active-zones
firewall-cmd --zone=public --list-all

一般来说都在 public 上,在这个 zone 增加 http 服务即可

firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --zone=public --add-service=https --permanent

也可以选择自己手写端口的方式

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent

添加完成之后,记得需要 reload 才会生效

firewall-cmd --reload

相应的,也可以删除某个端口或者服务

firewall-cmd --zone=public --remove-service=http --permanent
firewall-cmd --zone=public --remove-port=80/tcp --permanent

至此,nginx 已经可以正常的跑起来了~

安装 php

考虑到好像没什么需要特别处理的,php 就不编译安装了,直接走包管理。
这里使用 remi 源进行安装。

rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

yum --enablerepo=remi install php73-php php73-php-pear php73-php-bcmath php73-php-pecl-jsond-devel php73-php-mysqlnd php73-php-gd php73-php-common php73-php-fpm php73-php-intl php73-php-cli php73-php php73-php-xml php73-php-opcache php73-php-pecl-apcu php73-php-pdo php73-php-gmp php73-php-process php73-php-pecl-imagick php73-php-devel php73-php-mbstring php73-php-zip php73-php-ldap php73-php-imap php73-php-pecl-mcrypt -y

老样子先加个符号链接

ln -s /opt/remi/php73/root/usr/bin/php /usr/bin/php
ln -s /opt/remi/php73/root/usr/sbin/php-fpm /usr/sbin/php-fpm

改一下配置文件

vim /etc/opt/remi/php73/php.ini
vim /etc/opt/remi/php73/php-fpm.d/www.conf

虽然已经有一个 php73-php-fpm 的配置,但是太长了而且写死的,我选择重新写个:

vim /usr/lib/systemd/system/php-fpm.service

内容也就直接参照 php73-php-fpm ,不过既然已经建了软链接,就写通用点省得以后换。

# It's not recommended to modify this file in-place, because it
# will be overwritten during upgrades.  If you want to customize,
# the best way is to use the "systemctl edit" command.

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=notify
ExecStart=/usr/sbin/php-fpm --nodaemonize
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

那么接下来,也就可以用 systemd 进行管理了

systemctl status php-fpm
systemctl start php-fpm
systemctl restart php-fpm
systemctl stop php-fpm
systemctl enable php-fpm

安装 MySQL

MySQL 就先装个 5.7 用着吧,先安装官方源

wget https://repo.mysql.com/mysql57-community-release-el7.rpm
yum localinstall mysql57-community-release-el7.rpm

直接 yum 安装

yum install mysql-community-server

装好之后日常改下配置

vim /etc/my.cnf

systemd 配置直接可以用

systemctl status mysqld
systemctl start mysqld
systemctl restart mysqld
systemctl stop mysqld
systemctl enable mysqld

获取一下安装时生成的临时密码

grep 'temporary password' /var/log/mysqld.log

修改个新的 root 密码就搞掂了

mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY '12345wtfPass?!';

安装 redis

突然想起来 redis 也是不可缺少的,顺便也装一下。
鉴于 remi 源里直接就有,那就继续 yum 安装了。

yum --enablerepo=remi install redis

检查一下配置,确认一下监听的 IP 不是 0.0.0.0

vim /etc/redis.conf 

配置什么问题就直接 systemd 搞起

systemctl status redis
systemctl start redis
systemctl restart redis
systemctl stop redis
systemctl enable redis

nginx php 配置

非常简单,其实 nginx 已经自带了相关配置,只需要简单启用即可

        location ~ .*\.php$
        {
            try_files $uri =404;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }

顺手装上个 phpMyAdmin ,测下数据库连接是否正常,就全都搞掂~

文章目录