CentOS8.0编译安装NGINX1.17.4

下载NGINX1.17.4:

wget http://nginx.org/download/nginx-1.17.4.tar.gz

解压缩然后进入源码目录:

tar zxf nginx-1.17.4.tar.gz
cd nginx-1.17.4

编译安装:

./configure --prefix=/usr/local/nginx --user=nobody --group=nobody --with-http_ssl_module --with-http_v2_module && make && make install

出现报错信息:

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

这是因为缺少pcre-devel,安装pcre-devel:

yum install pcre-devel

编辑nginx.conf配置文件:

vi /usr/local/nginx/conf/nginx.conf

支持IPv6:

#HTTP
listen  [::]:80;
#HTTPS
listen  [::]:443 ssl;

启用HTTPS和HTTP2以及HSTS:

server {
        listen       443 ssl http2;
        listen  [::]:443 ssl http2;
        server_name  你的域名;
        add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";

        ssl_certificate      你的证书路径;
        ssl_certificate_key  你的私钥路径;
        ssl_session_cache    shared:SSL:5m;
        ssl_session_timeout  5m;
        ssl_early_data       on;
        ssl_protocols        TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers  on;
}

使HTTP请求301跳转至HTTPS:

server {
        listen      80;
        listen [::]:80;
        server_name 你的域名;
        return 301 https://你的域名;
    }

NGINX的WordPress伪静态规则:

location / {
            root   html;
            index  index.php;
            if (-f $request_filename/index.html){
                rewrite (.*) $1/index.html break;
            }
            if (-f $request_filename/index.php){
                rewrite (.*) $1/index.php;
            }
            if (!-f $request_filename){
                rewrite (.*) /index.php;
            }
        }

支持PHP:

location ~ \.php$ {
            root           html;
            fastcgi_pass   unix:/dev/shm/php-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  HTTPS            $https;
            include        fastcgi_params;
        }

浏览器缓存文件类型和过期时间以及防盗链:

location ~ \.(gif|jpg|png|css|js)$ {
            root   html;
            expires 30d;
            valid_referers none blocked 你的域名;
            if ($invalid_referer) {
            return 403;
            }
        }

启动NGINX:

/usr/local/nginx/sbin/nginx

CentOS8.0编译安装PHP7.3.10

下载PHP7.3.10:

wget https://www.php.net/distributions/php-7.3.10.tar.gz

解压缩然后进入源码目录:

tar zxf php-7.3.10.tar.gz
cd php-7.3.10

编译安装:

./configure --prefix=/usr/local/php --with-mysql-sock=/dev/shm/mysql.sock --enable-fpm --with-fpm-user=nobody --with-fpm-group=nobody --with-openssl --with-zlib --with-curl --enable-bcmath --enable-mbstring --enable-exif --with-mysqli --with-jpeg-dir --with-png-dir --with-freetype-dir --with-gd --with-pcre-dir --with-iconv-dir && make && make install

整个过程出现以下报错信息:

configure: error: in `/root/php-7.3.10':
configure: error: no acceptable C compiler found in $PATH
configure: error: libxml2 not found. Please check your libxml2 installation.
configure: error: Cannot find OpenSSL's <evp.h>
configure: error: cURL version 7.15.5 or later is required to compile php with cURL support
configure: error: jpeglib.h not found.
configure: error: png.h not found.
configure: error: freetype-config not found.
-bash: make: command not found

这都是因为缺少相关依赖组件导致的,安装依赖即可:

yum install gcc libxml2-devel openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel make

还有这个报错信息:

virtual memory exhausted: Cannot allocate memory
make: *** [Makefile:921: ext/fileinfo/libmagic/apprentice.lo] Error 1

这是因为可用内存不足,分配适当大小的SWAP分区即可:

fallocate -l 512M /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

编译安装完成后,复制/重命名必要的文件:

cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf
cd php-fpm.d
cp www.conf.default www.conf
cp /root/php-7.3.10/php.ini-production /usr/local/php/lib/php.ini

编辑www.conf配置文件:

vi www.conf

listen = 127.0.0.1:9000更改为listen = /dev/shm/php-fpm.sock

再找到以下三行,把前面的分号去掉

;listen.owner = nobody
;listen.group = nobody
;listen.mode = 0660

编辑php.ini配置文件,加上一行zend_extension=opcache.so,开启OPcache

vi /usr/local/php/lib/php.ini

启动PHP-FPM

/usr/local/php/sbin/php-fpm

CentOS8.0使用RPM安装MySQL8.0.18

下载适用于CentOS8.0的MySQL8.0.18:

wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.18-1.el8.x86_64.rpm-bundle.tar

提示-bash: wget: command not found,这是因为没有安装wget,安装wget:

yum install wget

安装后重新下载,下载后解压缩:

tar xf mysql-8.0.18-1.el8.x86_64.rpm-bundle.tar

开始安装:

rpm -ivh mysql-community-common-8.0.18-1.el8.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.18-1.el8.x86_64.rpm
rpm -ivh mysql-community-client-8.0.18-1.el8.x86_64.rpm
rpm -ivh mysql-community-server-8.0.18-1.el8.x86_64.rpm

安装mysql-community-server-8.0.18-1.el8.x86_64.rpm时报错:

error: Failed dependencies:
	/usr/bin/perl is needed by mysql-community-server-8.0.18-1.el8.x86_64
	net-tools is needed by mysql-community-server-8.0.18-1.el8.x86_64
	perl(Getopt::Long) is needed by mysql-community-server-8.0.18-1.el8.x86_64
	perl(strict) is needed by mysql-community-server-8.0.18-1.el8.x86_64

这是因为缺少依赖组件perl和net-tools,安装perl和net-tools:

yum install perl net-tools

然后重新安装mysql-community-server-8.0.18-1.el8.x86_64.rpm,安装后以空密码方式初始化MySQL:

mysqld --initialize-insecure

授予权限:

chown mysql:mysql /var/lib/mysql -R

编辑my.cnf配置文件:

vi /etc/my.cnf

添加一行port=你要设置的端口号,修改MySQL的默认端口

socket=/var/lib/mysql/mysql.sock修改为socket=/dev/shm/mysql.sock

再添加以下两行:

[client]
socket=/dev/shm/mysql.sock

启动MySQL:

systemctl start mysqld.service
回到顶部