macOS安装使用Nginx
使用brewhome进行安装,安装brewhome可参考此文章
sudo brew install nginx
安装路径为/usr/local/etc/nginx
,配置文件也在此目录下。
常用操作
sudo nginx
# 启动
sudo nginx -s stop
# 停止
sudo nginx -s reload
# 重载
使用brewhome进行安装,安装brewhome可参考此文章
sudo brew install nginx
安装路径为/usr/local/etc/nginx
,配置文件也在此目录下。
常用操作
sudo nginx
# 启动
sudo nginx -s stop
# 停止
sudo nginx -s reload
# 重载
脚本代码如下:
#!/bin/bash
echo -n "请设置MySQL密码:"
read password
echo "更新系统内核..."
dnf -y update
echo "正在安装 EPEL && Remi..."
dnf -y install https://dl.Fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
echo "正在安装 Nginx-1.18..."
dnf -y module reset nginx
dnf -y module enable nginx:1.18
dnf -y install nginx
echo "正在设置 Nginx..."
echo 'user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
use epoll;
worker_connections 102400;
}
http {
log_format main '"'"'$remote_addr - $remote_user [$time_local] "$request" '"'"'
'"'"'$status $body_bytes_sent "$http_referer" '"'"'
'"'"'"$http_user_agent" "$http_x_forwarded_for"'"'"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/php-fpm.conf;
include /var/www/wwwconf/*.conf;
}' > /etc/nginx/nginx.conf
echo 'location ~ [^/]\.php(/|$){
try_files $uri =404;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php-fpm;
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
}' > /etc/nginx/default.d/php.conf
echo "正在安装 PHP-8.0"
dnf -y module reset php
dnf -y module enable php:remi-8.0
dnf -y install php php-fpm php-pdo php-mysqlnd php-mbstring
echo "正在安装 MySQL-8.0"
dnf -y module reset mysql
dnf -y module enable mysql:8.0
dnf -y install mysql mysql-server
echo "正在设置 MySQL..."
systemctl start mysqld
dnf -y install expect
echo '#!/usr/bin/expect
set timeout 60
set password [lindex $argv 0]
spawn mysql_secure_installation
expect {
"Press Y" { send "Y\r"; exp_continue}
"Press y" { send "y\r"; exp_continue}
"Please enter 0 = LOW" { send "0\r"; exp_continue}
"password" { send "$password\r"; exp_continue}
}
interact ' > mysql_secure_installation.exp
chmod +x mysql_secure_installation.exp
./mysql_secure_installation.exp $password
rm -rf mysql_secure_installation.exp
echo "正在创建站点目录..."
rm -rf /var/www/*
mkdir /var/www/wwwconf
mkdir /var/www/wwwroot
mkdir /var/www/wwwssl
mkdir /var/www/wwwlog
echo '正在创建默认站点...'
echo 'server {
listen 80 default_server;
listen [::]:80;
server_name 127.0.0.1;
index index.php index.html;
root /var/www/wwwroot/default;
access_log /var/www/wwwlog/default.log;
include /etc/nginx/default.d/php.conf;
}' > /var/www/wwwconf/default.conf
mkdir /var/www/wwwroot/default
echo '<?php phpinfo(); ?>' > /var/www/wwwroot/default/index.php
echo "正在安装 phpmyadmin..."
wget -O phpmyadmin.zip https://files.phpmyadmin.net/phpMyAdmin/5.0.4/phpMyAdmin-5.0.4-all-languages.zip
unzip phpmyadmin.zip -d /var/www/wwwroot/default
mv /var/www/wwwroot/default/phpMyAdmin-5.0.4-all-languages /var/www/wwwroot/default/phpmyadmin
chmod -R 777 /var/www/wwwroot/default/*
rm -rf phpmyadmin.zip
echo "设置自动更新..."
echo "30 01 * * * dnf -y update >> /tmp/tmp.txt" >> /var/spool/cron/root
systemctl restart crond
echo "正在启动 Nginx..."
systemctl enable nginx
systemctl start nginx
echo "正在启动 PHP..."
systemctl enable php-fpm
systemctl start php-fpm
echo "正在启动 MySQL..."
systemctl enable mysqld
systemctl restart mysqld
echo "安装完成!"
需要配置的字段记不太清楚,存个模板下次安装直接粘贴
站点配置文件
server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name domain;
index index.php index.html;
root /var/www/wwwroot/website;
access_log /var/www/wwwlog/website.log;
# HTTPS相关配置
ssl_certificate /var/www/wwwssl/website/website.pem;
ssl_certificate_key /var/www/wwwssl/website/website.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
# 强制HTTPS
if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}
# 伪静态规则,需根据程序修改
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
include /etc/nginx/default.d/php.conf;
include /etc/nginx/default.d/pathinfo.conf;
}
站点配置文件加入/修改以下代码
# 打开高效传输文件模式
sendfile on;
# 最大上传文件为128MB
client_max_body_size 128M;
# 连接时间,默认60s
keepalive_timeout 600;
在站点配置文件加入此代码
location ~ (.db)$ {
return 404;
}
若还有其他不允许被查看的文件,如 .key,如此增加即可
location ~ (.db|.key)$ {
return 404;
}
在 /etc/nginx/nginx.conf 的 http{} 中添加以下代码
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6].";
打算练练手,CentOS8.2下使用 yum 安装的 nginx1.16 和 php7.4,没装MySQL,用的阿里云的单机数据库。
安装过程总的来说还算顺利,就是不懂nginx的规则,在pathinfo卡了好一会。
在站点配置文件加入以下代码
# Typecho伪静态代码,其他程序替换为对应规则即可
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?$1 last;
break;
}
# 开启Pathinf,我单独创建了个文件,然后使用站点配置文件引入的,如果是单站点也可以直接写到文件里
location ~ .*.php(/.*)*$
{
# Nginx默认是使用Unix Socket与php-fpm通信,我改成了通过TCP Socket.
# 这里需要改成自己的,通信方式可以在/etc/php-fpm.d/www.conf中查看/修改listen的值
# fastcgi_pass unix:/run/php-fpm/www.sock; # 若为Unix Socket这里需要改为listen的值
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
}