环境:
系统 | IP | 所需服务 |
---|---|---|
CentOS7 | 192.168.32.129 | nginx、mysql、php |
# 关闭防火墙和SELINUX
[root@Sunako ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@Sunako ~]# getenforce
Disabled
# 配置网络源
[root@Sunako ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@Sunako ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo
[root@Sunako ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@Sunako ~]# yum -y install vim wget
[root@Sunako ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
1. 安装nginx
#安装依赖包
[root@Sunako ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget
[root@Sunako ~]# yum -y groups mark install 'Development Tools'
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirror.bit.edu.cn
Marked install: Development Tools
#创建用户
[root@Sunako ~]# useradd -r -M -s /sbin/nologin nginx
[root@Sunako ~]# id nginx
uid=998(nginx) gid=996(nginx) groups=996(nginx)
#创建日志存放目录
[root@Sunako ~]# mkdir -p /var/log/nginx
[root@Sunako ~]# chown -R nginx.nginx /var/log/nginx
#下载并安装
[root@Sunako ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@Sunako ~]# tar xf nginx-1.18.0.tar.gz
[root@Sunako ~]# cd nginx-1.18.0
[root@Sunako nginx-1.18.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@Sunako ~]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
#配置环境变量
[root@Sunako ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@Sunako ~]# source /etc/profile.d/nginx.sh
#启动nginx
[root@Sunako ~]# nginx
[root@Sunako ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
浏览器访问
2. 安装mysql
#安装依赖包
[root@Sunako ~]# yum -y install libaio ncurses-devel openssl-devel openssl cmake mariadb-devel
#创建用户和组
[root@Sunako ~]# useradd -r -M -s /sbin/nologin -u 306 mysql
[root@Sunako ~]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)
#下载并解压
[root@Sunako ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
[root@Sunako ~]# tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C /usr/local
#创建软连接,修改配置文件属主
[root@Sunako ~]# cd /usr/local/
[root@Sunako local]# ln -s mysql-5.7.24-linux-glibc2.12-x86_64/ mysql //创建软链接
[root@Sunako local]# chown -R mysql.mysql mysql*
[root@Sunako local]# ll -d mysql
lrwxrwxrwx. 1 mysql mysql 36 Aug 9 15:39 mysql -> mysql-5.7.24-linux-glibc2.12-x86_64/
#添加环境变量
[root@Sunako ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@Sunako ~]# source /etc/profile.d/mysql.sh
#创建数据存放目录
[root@Sunako ~]# mkdir /opt/data
[root@Sunako ~]# chown -R mysql.mysql /opt/data
[root@Sunako ~]# ll -d /opt/data/
drwxr-xr-x 2 mysql mysql 6 8月 9 16:29 /opt/data
#初始化数据库,并配置
[root@Sunako ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data
//这里加入了-insecure选项,可以不要密码
[root@Sunako ~]# ln -s /usr/local/mysql/include /usr/local/include/mysql
[root@Sunako ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@Sunako ~]# ldconfig
#生成配置文件
[root@Sunako ~]# cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
EOF
#配置服务启动脚本
[root@Sunako ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@Sunako ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@Sunako ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
#启动mysql并修改密码
[root@Sunako ~]# chkconfig mysqld on //设为开机启动
[root@Sunako ~]# service mysqld start
[root@Sunako ~]# ss -antl |grep 3306
LISTEN 0 80 :::3306 :::*
[root@Sunako ~]# mysql -e "set password = password('123456')"
[root@Sunako ~]# mysql -uroot -123456
.....
mysql> quit
Bye
3. 安装php
#配置yum源
[root@Sunako ~]# wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@Sunako ~]# yum -y install remi-release-7.rpm
[root@Sunako ~]# yum clean all
[root@Sunako ~]# yum makecache --enablerepo=remi-php74
#安装依赖包
[root@Sunako ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php74-php-mysqlnd
#下载并安装php
[root@Sunako ~]# wget https://www.php.net/distributions/php-7.4.7.tar.gz
[root@Sunako ~]# tar xf php-7.4.7.tar.gz
[root@Sunako ~]# cd php-7.4.7
[root@Sunako ~]# ./configure --prefix=/usr/local/php7 \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-png \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
//这里如果报错需要安装两个依赖包,安装好后再次执行上面的命令
[root@Sunako php-7.4.7]# yum -y install libsqlite3x-devel oniguruma-devel
[root@Sunako php-7.4.7]# make -j $(cat /proc/cpuinfo |grep processor|wc -l)
[root@Sunako php-7.4.7]# make install
#配置环境变量
[root@Sunako ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@Sunako ~]# source /etc/profile.d/php7.sh
[root@Sunako ~]# php -v
PHP 7.4.7 (cli) (built: Aug 9 2020 17:02:42) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
#配置php-fpm
[root@Sunako ~]# cp php-7.4.7/php.ini-production /etc/php.ini
[root@Sunako ~]# cp php-7.4.7/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@Sunako ~]# chmod +x /etc/rc.d/init.d/php-fpm
[root@Sunako ~]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@Sunako ~]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
[root@Sunako ~]# vim /usr/local/php7/etc/php-fpm.conf
#添加以下四行
pm.max_children = 50 //最多同时提供50个进程提供50个并发服务
pm.start_servers = 5 //启动时启动5个进程
pm.min_spare_servers = 2 //最小空闲进程数
pm.max_spare_servers = 8 //最大空闲进程数
#启动php-fpm
[root@Sunako ~]# service php-fpm start
Starting php-fpm done
[root@node2 ~]# chkconfig php-fpm on
[root@Sunako ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:9000 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
#配置监听所有IP
[root@Sunako ~]# vim /usr/local/php7/etc/php-fpm.d/www.conf
listen = 127.0.0.1:9000 //将这里的127.0.0.1改为0.0.0.0
[root@node2 ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@Sunako ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:9000 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005
4. 配置nginx
#创建php测试页面
[root@Sunako ~]# vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
#修改nginx主配置文件
[root@Sunako ~]# vim /usr/local/nginx/conf/nginx.conf
...
location / {
root html;
index index.php index.html index.htm;
}
...
//以下内容取消注释并修改
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
...
#重新加载
[root@Sunako ~]# nginx -s reload