环境说明
IP地址 | 所需服务 |
---|---|
192.168.129.250 | nginx mysql php |
安装yum源
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# yum -y install vim wget make
部署nginx
//关闭防火墙与SELINUX
[root@localhost ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
[root@localhost ~]# setenforce 0
//创建用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖、工具、所需包组
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@localhost ~]# yum -y groups mark install 'Development Tools'
//创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
//编译安装
[root@localhost src]# ls
debug kernels nginx-1.20.1.tar.gz
[root@localhost src]# tar xf nginx-1.20.1.tar.gz
[root@localhost src]# cd nginx-1.20.1
[root@localhost nginx-1.20.1]# ./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@localhost nginx-1.20.1]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# . /etc/profile.d/nginx.sh
[root@localhost ~]# which nginx
/usr/local/nginx/sbin/nginx
//服务控制方式,使用nginx命令
-t //检查配置文件语法
-v //输出nginx的版本
-c //指定配置文件的路径
-s //发送服务控制信号,可选值有{stop|quit|reopen|reload}
//启动nginx
[root@localhost ~]# nginx
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
部署mysql
//创建用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
//安装依赖包
[root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
//下载二进制格式的mysql软件包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# ls mysql*
mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
//解压
[root@localhost src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# ls /usr/local/
bin etc games include lib lib64 libexec mysql-5.7.34-linux-glibc2.12-x86_64 nginx sbin share src
//创建软连接
[root@localhost src]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.34-linux-glibc2.12-x86_64/'
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root root 45 10月 23 00:28 bin
drwxr-xr-x. 2 root root 6 10月 26 20:53 etc
drwxr-xr-x. 2 root root 6 8月 12 2018 games
drwxr-xr-x. 2 root root 6 8月 12 2018 include
drwxr-xr-x. 3 root root 21 10月 23 00:28 lib
drwxr-xr-x. 2 root root 6 8月 12 2018 lib64
drwxr-xr-x. 2 root root 6 8月 12 2018 libexec
lrwxrwxrwx 1 root root 36 10月 26 21:38 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64/
drwxr-xr-x 9 root root 129 10月 26 21:37 mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 11 root root 151 10月 26 21:16 nginx
drwxr-xr-x. 2 root root 6 10月 26 20:51 sbin
drwxr-xr-x. 5 root root 49 9月 28 15:13 share
drwxr-xr-x. 2 root root 6 8月 12 2018 src
//修改属主属组
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql
[root@localhost ~]# ll /usr/local/mysql -d
lrwxrwxrwx 1 mysql mysql 36 10月 26 21:38 /usr/local/mysql -> mysql-5.7.34-linux-glibc2.12-x86_64/
//添加环境变量
[root@localhost ~]# ls /usr/local/mysql
bin docs include lib LICENSE man README share support-files
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh
[root@localhost ~]# which mysql
/usr/local/mysql/bin/mysql
//头文件(include)、读取lib
[root@localhost ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
'/usr/local/include/mysql' -> '/usr/local/mysql/include/'
[root@localhost ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# ldconfig
//创建数据存放目录
[root@localhost ~]# mkdir -p /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data/
[root@localhost ~]# ll /opt/data/
总用量 0
//初始化数据库
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/ #这种方法初始化完成后是没有密码
2021-10-26T13:44:15.653137Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-10-26T13:44:15.802617Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-10-26T13:44:15.877996Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-10-26T13:44:15.934051Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: cf9a66e7-3662-11ec-b2ef-000c29aa87a6.
2021-10-26T13:44:15.935817Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-10-26T13:44:16.451993Z 0 [Warning] CA certificate ca.pem is self signed.
2021-10-26T13:44:16.479129Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
//生成配置文件
[root@localhost ~]# 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
//配置service服务启动文件
[root@localhost ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /usr/local/mysql/support-files/mysql.server
[root@localhost ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /usr/local/mysql/support-files/mysql.server
[root@localhost ~]# cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=Mysqld server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
[Install]
WantedBy=multi-user.target
EOF
//重新加载
[root@localhost ~]# systemctl daemon-reload
//启动mysql
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
//修改密码
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
部署php
//安装依赖包
[root@localhost ~]# yum -y install sqlite-devel libzip-devel 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 readline readline-devel libxslt libxslt-devel
[root@localhost ~]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
//下载php软件包并解压
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://www.php.net/distributions/php-8.0.10.tar.gz
[root@localhost src]# tar xf php-8.0.10.tar.gz
//编译及安装
[root@localhost ~]# cd /usr/src/php-8.0.10
[root@localhost php-8.0.10]# ./configure --prefix=/usr/local/php8 \
--with-config-file-path=/etc \
--enable-fpm \
--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-zlib-dir \
--with-freetype \
--with-gettext \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
[root@localhost php-8.0.10]# make && make install
//配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@localhost ~]# source /etc/profile.d/php.sh
[root@localhost ~]# which php
/usr/local/php8/bin/php
//配置php-fpm文件
[root@localhost ~] # cd /usr/src/php-8.0.10
[root@localhost php-8.0.10]# \cp php.ini-production /etc/php.ini
[root@localhost php-8.0.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-8.0.10]# chmod +x /etc/init.d/php-fpm
[root@localhost php-8.0.10]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@localhost php-8.0.10]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
//配置.service服务启动文件
[root@localhost ~]# cat > /usr/lib/systemd/system/php-fpm.service <<EOF
[Unit]
Description=Php-fpm server daemon
After=network.target
[Service]
Type=forking
ExecStart=service php-fpm start
ExecStop=service php-fpm stop
[Install]
WantedBy=multi-user.target
EOF
//重新加载
[root@localhost ~]# systemctl daemon-reload
//启动服务
[root@localhost ~]# systemctl start php-fpm
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
nginx配置
//创建PHP访问页面
[root@localhost ~]# cat > /usr/local/nginx/html/index.php <<EOF
<?php
phpinfo();
?>
EOF
//修改nginx配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
......
location / {
root html;
index index.php index.html index.htm; #在45行中添加index.php
}
......
#取消location ~ .php$ 大括号前的注释(65~71)行
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #将/script改为$document_root
include fastcgi_params;
}
//重启nginx服务
[root@localhost ~]# nginx -s reload