一 、下载源码包
Nginx http://nginx.org/en/download.html nginx主程序包
MySQL https://dev.mysql.com/downloads/mysql/ mysql主程序包
PHP https://php.net/downloads.php php主程序包
二、依赖安装
yum install gcc gcc-c++ openssl openssl-devel cyrus-sasl-md5 gcc-toolset-10-gcc gcc-toolset-10-gcc-c++ gcc-toolset-10-binutils git pkgconfig ncurses-devel libtirpc-devel
三、安装nginx
1. 创建nginx专用用户
useradd -M -s /sbin/nologin nginx
-M 不创建家目录
-s 指定Shell
/sbin/nologin 不可登录主机
2、编译安装
tar zxvf nginx-1.20.0.tar.gz
cd nginx-1.20.0/
./configure --prefix=/usr/local/nginx --with-http_ssl_module
make && make install
3、修改配置文件
vim /usr/local/nginx/conf/nginx.conf
user nginx; //第二行,去除注释并修改用户为nginx
4、将Nginx命令路径加入到PATH变量中
- 方法一
vim ~/.bash_profile #打开文件
PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin #加入此代码保存退出
source ~/.bash_profile 加载配置文件
- 方法二
vim /etc/profile.d/nginx.sh
export PATH=/usr/local/nginx/sbin:$PATH
./etc/profile.d/nginx.sh
- 方法三
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/ //软连接
- 方法四
vim /etc/profile
export=$PATH:/usr/local/nginx/sbin
保存退出
立即生效: source /etc/profile
5、设置开机自启动
- 方法一 借助systemd进行开机自启动设置
Systemd服务文件以.service结尾,比如现在要建立nginx为开机启动,如果用yum install命令安装的,yum命令会自动创建nginx.service文件,直接用命令 systemcel enable nginx.service
编译安装的需要手动创建nginx.service
1. 在系统服务目录里创建nginx.service文件
vim /lib/systemd/system/nginx.service
内容:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
说明:
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置