在CentOS7系统源码安装Nginx+MySQL+PHP+Go
ahcoder 2025-07-01 16:25 3 浏览
以下安装说明仅供参考,请根据实际情况修改配置,进行软件编译安装
软件安装也可以参考阿里云/腾讯云的建站教程:
https://help.aliyun.com/zh/ecs/use-cases/build-a-website/
https://cloud.tencent.com/document/product/213/54814
SSL证书配置参考:
https://cloud.tencent.com/document/product/400/35244
1、Linux
环境:Centos 7.6 64位
【注】
1、文件权限、用户组
2、端口开放
2、Nginx
# 编译工具
yum -y install gcc gcc-c++ autoconf automake make
# 依赖
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
# 添加www用户
groupadd -f www
useradd -g www www
# 安装nginx
wget https://nginx.org/download/nginx-1.18.0.tar.gz
# 解压
tar -xf nginx-1.18.0.tar.gz
# 切换目录
cd nginx-1.18.0
# 服务器原有配置:./configure --prefix=/usr/local/nginx --with-http_ssl_module
# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-http_gzip_static_module --with-http_sub_module
make && make install
# 创建软连接
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
# nginx配置:如nginx.conf ssl
# 启动
nginx -c /usr/local/nginx/conf/nginx.conf
# 查看帮助
# nginx -h
# kill所有nginx进程
# kill $(ps aux|grep '[n]ginx'|awk '{print $2}')
增加GeoIP模块:
# yum安装geoip
yum -y install GeoIP GeoIP-devel GeoIP-data
# 进入源码目录进行编译,示例:
cd /root/nginx-1.18.0
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-http_gzip_static_module --with-http_sub_module --with-http_geoip_module
# 只要执行make,千万不要make install!!!
make
# 备份
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-old
# 替换
cp objs/nginx /usr/local/nginx/sbin/
修改nginx.conf配置:
# 示例:nginx.conf增加geoip相关配置,其余不变
http {
geoip_country /usr/share/GeoIP/GeoIP.dat;
geoip_city /usr/share/GeoIP/GeoIPCity.dat;
server {
# 示例
location ^~ /myip {
default_type text/plain;
return 200 "$remote_addr $geoip_country_name $geoip_city $geoip_latitude $geoip_longitude";
}
}
}
重启nginx:
nginx -s stop
nginx -c /usr/local/nginx/conf/
3、MySQL
# 安装libaio
yum install -y libaio
# 下载地址:https://downloads.mysql.com/archives/community/
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
# 解压
tar -xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
# 切换目录
cd /usr/local/
# 重命名
mv mysql-5.7.24-linux-glibc2.12-x86_64 mysql
# 检查数据库文件是否有,如有删除(linux系统自带)
# 检查:rpm -qa | grep mysql
# 删除:rm -e --nodeps
# 创建mysql用户
# useradd -s /sbin/nologin -M mysql
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
# 创建mysql相关文件
mkdir -p /usr/local/mysql/data/ /usr/local/mysql/var/log/ /usr/local/mysql/var/run/
# 创建mysql.pid、mysql.sock文件
touch /tmp/mysql.sock /usr/local/mysql/var/log/error.log /usr/local/mysql/var/run/mariadb.pid
# 修改用户及用户组
chown -R mysql.mysql /usr/local/mysql
# mysql配置
# \cp -f my.cnf /etc/my.cnf
# 安装mysql
# /usr/local/mysql/bin/mysqld_safe --initialize --defaults-file=/etc/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
# /usr/local/mysql/bin/mysqld_safe --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
# ALTER USER USER() IDENTIFIED BY 'root';
# 配置环境变量
# export PATH=$PATH:/usr/local/mysql/bin
# vi /etc/profile
# 配置生效
# source /etc/profile
# 加入系统进程
# cp -f /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql.server
# 启动服务
# systemctl enable mysql.server
# systemctl start mysql.server
# 或者/usr/local/mysql/support-files/mysql.server start
# 进入mysql,修改密码
# mysql -u用户名 -P端口 -p密码
# set password=password('root');
# grant all privileges on *.* to root@'%' identified by 'root';
# flush privileges;
4、MySQL定时备份
# crontab定时任务
# 每天凌晨2点执行mysql数据备份
0 2 * * * /root/crontab/mysql/mysql_dump_script.sh
# 定时执行脚本mysql.backup.sh
#!/bin/bash
DATE=`date +"%Y%m%d"`
/usr/local/mysql/bin/mysqldump -uroot -proot --databases test > 'test_'${DATE}'.sql'
5、Redis
# 下载
wget http://download.redis.io/releases/redis-6.2.6.tar.gz
# 解压
tar -xf redis-6.2.6.tar.gz
# 进入目录
cd redis-6.2.6
# 编译
make
# 安装
make install PREFIX=/usr/local/redis
# 创建etc、data目录
mkdir /usr/local/redis/etc
mkdir /usr/local/redis/data
# 复制redis.conf到/usr/local/redis/etc目录下
cp redis.conf /usr/local/redis/etc/
# 编辑redis.conf文件,配置redis为后台启动
# 将 daemonize no 改成 daemonize yes
# 将 dir ./ 改成 /usr/local/redis/data
# 将 requirepass foobared 改成 requirepass 123456
# 将 maxmemory-policy noeviction 改成 maxmemory-policy allkeys-lru
# vi /usr/local/redis/etc/redis.conf
sed -ir 's/daemonize no/daemonize yes/g' /usr/local/redis/etc/redis.conf
sed -ir 's/dir \.\//dir \/usr\/local\/redis\/data/g' /usr/local/redis/etc/redis.conf
sed -ir 's/# requirepass foobared/requirepass 123456/g' /usr/local/redis/etc/redis.conf
# 创建软连接
ln -s /usr/local/redis/bin/* /usr/local/bin/
# 启动redis-server
redis-server /usr/local/redis/etc/redis.conf
6、PHP
# 安装libxml2、libcurl、png、freetype、libxslt、libzip、cmake
yum install -y libxml2-devel libcurl-devel libpng-devel freetype-devel libxslt-devel
# 查看libzip版本:rpm -qa |grep libzip
# 查看cmake版本:cmake -version
# 卸载libzip、cmake
# yum remove libzip cmake
pwd=`pwd`
# 安装libzip
# 参考:https://www.cnblogs.com/itbsl/p/10208926.html
wget https://libzip.org/download/libzip-1.8.0.tar.gz --no-check-certificate
tar -xf libzip-1.8.0.tar.gz
cd libzip-1.8.0
mkdir build
cd build
cmake ..
make && make install
# 切换回原目录
cd $pwd
# 安装cmake
# 参考:https://blog.csdn.net/weixin_28909289/article/details/116680815
wget https://cmake.org/files/v3.18/cmake-3.18.6.tar.gz
tar -xf cmake-3.18.6.tar.gz
cd cmake-3.18.6
./bootstrap && make && make install
# 切换回原目录
cd $pwd
# 下载
wget https://www.php.net/distributions/php-7.3.4.tar.gz
# 解压
tar -xf php-7.3.4.tar.gz
# 进入目录
cd php-7.3.4
# 安装
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-curl=/usr/local/curl \
--with-freetype-dir \
--with-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-xmlrpc \
--with-xsl \
--with-zlib \
--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-xml \
--enable-zip
# ======================================================================================================================
# 或者
# 参考:https://blog.csdn.net/weixin_40699635/article/details/115294390
# 参考:https://developer.aliyun.com/article/920010
yum install -y libxml2-devel libcurl-devel libpng-devel freetype-devel libxslt-devel libicu-devel bzip2 bzip2-devel gmp-devel libmcrypt libmcrypt-devel openldap openldap-devel libc-client-devel
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-bz2 \
--with-curl=/usr/local/curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-gmp \
--with-iconv-dir \
--with-imap \
--with-imap-ssl \
--with-kerberos \
--with-ldap \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mcrypt \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--enable-fpm \
--enable-bcmath \
--enable-calendar \
--enable-exif \
--enable-intl \
--enable-inline-optimization \
--enable-libxml \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-wddx \
--enable-xml \
--enable-zip
# ======================================================================================================================
make && make install
# php配置
# \cp -Rf etc/* /usr/local/php/etc/
cp php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
mkdir -p /usr/local/php/var/log /usr/local/php/var/run
touch /usr/local/php/var/log/php-fpm.log /usr/local/php/var/run/php-fpm.pid
# 创建软连接
ln -s /usr/local/php/bin/php /usr/local/bin/php
# 启动php-fpm
/usr/local/php/sbin/php-fpm
# nginx进行配置,curl进行访问
# 示例:curl 127.0.0.1/index.php
7、Go
# 下载
wget https://studygolang.com/dl/golang/go1.17.3.linux-amd64.tar.gz
# 解压
tar -xf go1.17.3.linux-amd64.tar.gz
# 移动目录
mv go /usr/local/go
# 配置环境变量
# export PATH=$PATH:/usr/local/go/bin
# vi /etc/profile
# 配置生效
# source /etc/profile
# 查看go环境配置信息:go env
# 查看系统环境变量:echo $PATH
8、Docker
参考:
https://help.aliyun.com/document_detail/51853.html
9、SSL证书部署
参考:
https://cloud.tencent.com/document/product/400/35244
10、FTP服务
参考:
https://help.aliyun.com/document_detail/92048.html
https://www.yisu.com/zixun/164857.html
# 请根据实际情况配置ftp
# 安装vsftp
yum install vsftpd -y
# 创建test用户,默认家目录为/home/test,并且不能通过shell进行登录
useradd -s /sbin/nologin test
# 防止出现“530 Login incorrect”问题,有两种方式进行修改
#
# 方式一:编辑/etc/pam.d/vsftpd文件
# 可以对“auth required pam_shells.so”这行添加“#”,进行注释
# 或者将其改为“auth required pam_nologin.so”
# 对下面这行添加#,进行注释
#
# 方式二:
# 编辑/etc/shells文件,末尾追加/sbin/nologin
/sbin/nologin
# 防止ftp访问其他目录,编辑/etc/vsftpd/vsftpd.conf文件
chroot_local_user=YES ## 原本就有,取掉注释就好
allow_writeable_chroot=YES ## 添加
# 启动
service vsftpd start
相关推荐
- ClickHouse日志平台这样建,分分钟秒掉ELK
-
目前业界的日志生态,最常用的是ELK,其次就是ClickHouse,本文会演示如何使用Vector+ClickHouse来采集Nginx日志并做清洗,最终写入ClickHouse。至...
- nginx打印请求头日志方法-openresty
-
一、前言之前想用nginx打印收到的请求的请求头,但是只找到打印请求体的,没有打印请求头的,感觉原版nginx不支持。建议如果想打印请求头,先换成openresty(本人安装的是openresty-1...
- 文心快码帮你解大厂面试题:如何使用shell找到access log?
-
【大厂面试真题】系列,带你攻克大厂面试真题,秒变offer收割机!今日问题:在8g内存的机器,能否启动一个7G堆大小的java进程?一起看看文心快码BaiduComate给出的答案吧!如果这个问题你...
- Nginx奇技淫巧之:按日期自动生成日志文件
-
时光闹钟app开发者,请关注我,后续分享更精彩!坚持原创,共同进步!前言之前文章:Nginx奇技淫巧之:用户行为埋点数据采集实现,介绍了Nginx获取post请求body参数生成日志文件的方法。当业务...
- Nginx 日志文件详解:监控与诊断利器
-
随着互联网技术的快速发展,Nginx已成为最受欢迎的Web服务器之一,其稳定性、高性能和灵活性备受推崇。Nginx日志文件是Nginx服务器中非常重要的组成部分,对于监控和诊断Web应...
- Nginx从入门到放弃05-访问日志与日志切割
-
设置访问日志当我们访问nginx服务时,nginx会记录日志,nginx日志分两种,一种是访问日志,一种是错误日志,访问日志记录在”access.log”文件中,错误日志记录在”error.log”文...
- nginx系列:常用利用shell统计日志
-
0x01:根据访问IP统计UVUV(UniqueVisitor)独立访客,统计访问某站点的用户数;IP(InternetProtocol)独立IP数,是指独立的浏览了页面的不同IP,即统计不同的I...
- Linux 必须重点监控的 17 个日志文件:运维与安全必备指南
-
在Linux系统的日常运维与安全管理中,日志文件的重要性不言而喻。日志不仅记录着系统运行的点点滴滴,更是排查故障、发现异常、提前预警的第一手证据。作为一名系统管理员、安全工程师,甚至普通开发者,了...
- nginx 常用日志参数(nginx日志详解)
-
Nginx提供了多种日志参数(变量),可以用来记录请求的不同方面。常用日志参数$remote_addr:客户端的IP地址。$remote_user:客户端用户名。$time_local:局部时...
- GoAccess轻量nginx日志分析工具(nginx日志收集方案)
-
什么是GoAccessGoAccess是一款开源、实时,运行在命令行终端下的Web日志分析工具。该工具提供快速、多样的HTTP状态统计。分析结果,可以通过XShell等客户端工具查看,并...
- 后端实践:Nginx日志配置(超详细)(nginx日志配置文件)
-
作者:antwang来源:https://juejin.im/post/5aa09bb3f265da238f121b6c前言Nginx日志对于统计、系统服务排错很有用。Nginx日志主要分为两种:...
- Nginx access_log 运行日志查询和配置
-
1.介绍当我们学会Nginx的基本配置之后,可以通过Nginx配置Service代理。管理服务器所有的http和https请求。那么接下来就需要了解Nginx的日志控制,以及相关的文档查看了。你通过...
- Nginx记录用户请求Header到access log
-
为了统计和其它用途,经常有人需要自定义Nginx日志,把http请求中的某个字段记录到日志中,刚好在看lua+nginx的文章,第一想到的是用lua赋值来做,但是想想有点小恶心,于是Google了一番...
- 介绍五款Web服务器日志分析软件(web服务器日志是什么)
-
每个站长必须要看的数据统计表,都是由日志分析软件统计和分析网站情况所得出的。日志分析软件是一种解析Nginx/Apache/IIS/Lighttpd和任何其他Web服务器日志文件的软件...
- Nginx系列:Nginx自带后端健康检查
-
严格说Nginx并没有自带针对负载均衡后端节点的健康检查功能,但是可以通过默认自带的ngx_http_proxy_module模块和ngx_http_upstream_module模块中的相关指令...
- 一周热门
- 最近发表
- 标签列表
-
- linux 远程 (37)
- u盘 linux (32)
- linux 登录 (34)
- linux 路径 (33)
- linux 文件命令 (35)
- linux 是什么 (35)
- linux 界面 (34)
- 查看文件 linux (35)
- linux 语言 (33)
- linux代码 (32)
- linux 查看命令 (33)
- 关闭linux (34)
- root linux (33)
- 删除文件 linux (35)
- linux 主机 (34)
- linux与 (33)
- linux 函数 (35)
- linux .ssh (35)
- cpu linux (35)
- linux 防火墙 (33)
- linux 镜像 (34)
- linux ip地址 (34)
- linux 用户查看 (33)
- nginx配置 解析 (37)
- nginx 频率限制 (34)