Centos7下编译安装Nginx、Mysql、PHP(文章底部包含一键安装脚本)
实现环境
Centos 7 64位
IP地址:172.16.0.20
Nginx:1.12.0
Mysql:5.7.18
PHP:7.1.4
yum源:aliyun源
如果你的系统是新安装的redhat 7或 Centos 7 ,可以使用我的一键优化脚本优化系统,有利于系统的使用和下面的安装。点击我查看
首先下载好我们的需要的包
创建一个目录存放下载的软件
[root@localhost ~]# mkdir /software
[root@localhost ~]# cd /software/
软件下载地址
或者去它们各自的官网下载最新版,下载命令参考
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.18.tar.gz
wget https://sourceforge.net/projects/pcre/files/pcre/8.40/pcre-8.40.tar.gz
wget http://nginx.org/download/nginx-1.12.0.tar.gz
wget http://hk1.php.net/distributions/php-7.1.4.tar.gz
这是需要的包
[root@localhost software]# ll
总用量 757804
-rw-r--r-- 1 root root 61612105 4月 18 14:55 mysql-boost-5.7.18.tar.gz
-rw-r--r--. 1 root root 980831 4月 14 17:08 nginx-1.12.0.tar.gz
-rw-r--r-- 1 root root 20319716 4月 18 15:41 php-7.1.4.tar.gz
关闭系统限制
关闭系统防火墙
[root@localhost software]# systemctl stop firewalld.service
[root@localhost software]# systemctl disable firewalld.service
关闭SElinux
[root@localhost software]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@localhost software]# setenforce 0
开始安装nginx
创建www账户 用来启动nginx
[root@localhost software]# useradd www -s /sbin/nologin
安装依赖的包
[root@localhost software]# yum -y install pcre pcre-devel zlib zlib-devel gcc-c++ gcc openssl*
解压Nginx源码包
root@localhost software]# tar zxvf nginx-1.12.0.tar.gz
进入解压后的目录,对Nginx进行配置
[root@localhost software]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre
编译和安装
[root@localhost nginx-1.12.0]# make && make install
启动Nginx
[root@localhost nginx-1.12.0]# /usr/local/nginx/sbin/nginx
浏览器访问测试是否ok
关闭Nginx进程
[root@localhost nginx-1.12.0]# killall nginx
[root@localhost nginx-1.12.0]# ps -ef|grep nginx
nginx命令做软连接方便使用
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /sbin/nginx
编写nginx启动脚本
cat >> /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=//usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
修改完systemctl服务,需要重新加载下daemon
[root@localhost nginx-1.12.0]# systemctl daemon-reload
用systemctl启动Nginx服务,并查看状态
[root@localhost nginx-1.12.0]# systemctl start nginx
[root@localhost nginx-1.12.0]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2017-04-18 14:06:58 CST; 8s ago
Docs: http://nginx.org/en/docs/
Process: 11816 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 11813 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Main PID: 11818 (nginx)
CGroup: /system.slice/nginx.service
├─11818 nginx: master process /usr/sbin/nginx
└─11820 nginx: worker process
4月 18 14:06:58 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
4月 18 14:06:58 localhost.localdomain nginx[11813]: nginx: the configuration file /usr/local/nginx/conf/nginx...s ok
4月 18 14:06:58 localhost.localdomain nginx[11813]: nginx: configuration file /usr/local/nginx/conf/nginx.con...sful
4月 18 14:06:58 localhost.localdomain systemd[1]: Failed to read PID from file /usr/local/nginx/logs/nginx.pi...ment
4月 18 14:06:58 localhost.localdomain systemd[1]: Started nginx - high performance web server.
设置nginx开机启动
[root@localhost nginx-1.12.0]# systemctl enable nginx
nginx安装完成,下面安装mysql
安装MySQL
安装依赖包
[root@localhost nginx-1.12.0]# cd /software/
[root@localhost software]# yum -y install ncurses ncurses-devel bison cmake gcc gcc-c++
创建用户和组
[root@localhost software]# groupadd mysql
[root@localhost software]# useradd -s /sbin/nologin -g mysql mysql -M
[root@localhost software]# id mysql
解压mysql源码包
[root@localhost software]# tar zxvf mysql-boost-5.7.18.tar.gz
进入解压后的目录,对mysql进行配置(5.5以上都是cmake)
[root@localhost software]# cd mysql-5.7.18/
[root@localhost mysql-5.7.18]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DSYSCONFDIR=/usr/local/mysql/etc -DSYSTEMD_PID_DIR=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/usr/local/mysql/data -DWITH_BOOST=boost -DWITH_SYSTEMD=1
记住这个/usr/local/mysql/mysql.sock,php连接mysql会用到。
编译和安装
[root@localhost mysql-5.7.18]# make && make install
初始化数据库及启动
[root@localhost mysql-5.7.18]# chown -R mysql.mysql /usr/local/mysql/
[root@localhost mysql-5.7.18]# cd /usr/local/mysql/
cat >> my.cnf << EOF
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
default-character-set=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
#