最近在家看孩子的空隙,在自己一台老旧的DELL电脑上安装了cent os7作为平常的开发服务器,废话不多说,下面开始正文:
一、首先准备下载系统镜像:CentOs7(因为我的电脑是64的所以镜像也是64的)
百度网盘下载链接:
https://pan.baidu.com/s/1gFalaeQD7ZDH2s1UO_YYIw?pwd=fo05
二、制作U盘系统盘
下载好CentOS7的系统镜像了,我们需要一个系统启动盘。
这里用到了一个工具:UltraISo.
因为是教学使用所以时间选择试用就好
注:请勿用于商用,如有版权纠纷,与博主无关,
打开UltraISO软件,选择 “文件” -> “打开” 选择linux系统镜像(iso文件)并 “打开”
选择要安装的镜像
插入U盘,选择 “启动” -> “写入硬盘镜像”,硬盘驱动器选择做为系统盘的U盘,其他选项不需要改动。
确认写入的硬盘,以及写入方式
点击 ”写入” ,等待刻录完成即可。
看到刻录成功就可以了
三、使用U盘安装CentOS7系统
1、 插入刚刚制作的系统U盘进入BIOS,更改U盘为第一启动项。f10保存并重启。
2、 进入centos安装界面。不用管有几个选项,选中第一个 按 回车键
3、 进入图形界面如果不出现报错就是成功了,可以正常安装操作系统了。
选择语言,时区
磁盘分区(因为是最小安装择软件包所有没有选)
磁盘分区(因为是最小安装择软件包所有没有选)
开始安装
设置root密码
4. 安装之后重启后关机移除U盘在开机即可正常使用。
四、开始搭建LNMP环境
因为是笔记本改的服务器一直都不会关机,这时候需要:合上笔记本盖子(显示屏关闭),但是SSH远程在操作此机器,则希望合盖不睡眠(包括网络正常使用)
编辑下列文件:/etc/systemd/logind.conf
#HandlePowerKey按下电源键后的行为,默认power off
#HandleSleepKey 按下挂起键后的行为,默认suspend
#HandleHibernateKey按下休眠键后的行为,默认hibernate
#HandleLidSwitch合上笔记本盖后的行为,默认suspend(改为lock;即合盖不休眠)在原文件中,还要去掉前面的#
运行:systemctl restart systemd-logind就会生效.
一、准备资源(源码安装)
nginx 源:https://nginx.org/download/
mysql 源:http://tel.mirrors.163.com/mysql/Downloads/
php 源:https://www.php.net/releases/
二、安装nginx
1.下载Nginx
cd /home/application/tools
wget http://nginx.org/download/nginx-1.20.0.tar.gz
tar xf nginx-1.20.0.tar.gz
2.安装Nginx依赖
pcre-devel 让nginx拥有正则的能力,rewrite等都需要正则匹配
openssl-devel 让nginx拥有开启https的能力。
yum install -y pcre-devel openssl-devel
3.创建Nginx管理用户
useradd -s /sbin/nologin -M nginx
4.编译配置Nginx
–prefix=/application/nginx-1.14.0 置编译安装目录
–user=nginx 配置nginx的管理用户为nginx用户
–group=nginx 配置nginx的管理组为nginx组
–with-http_stub_status_module 配置使用Nginx的监控模块
–with-http_ssl_module 配置应用认证模块(https需要此模块,需要openssl-devel依赖
cd /home/application/tools/nginx-1.20.0
./configure --prefix=/application/nginx-1.14.0 --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
5.编译安装Nginx
make && make install
6.为Nginx安装目录创建软链接(方便以后管理)
ln -s /home/application/nginx-1.20.0/ /home/application/nginx
7.为Nginx命令创建软链接加进root环境变量(方便使用)
ln -s /home/application/nginx/sbin/nginx /sbin/
8.精简化Nginx配置文件
sed -i "/^$/d;/#/d" /home/application/nginx/conf/nginx.conf
9.添加systemctl启动脚本
cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStartPre=/application/nginx/sbin/nginx -t -c /application/nginx/conf/nginx.conf
ExecStart=/application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
10.启动Nginx 并且 查看Nginx进程
nginx或者systemctl start nginx
ps -ef | grep nginx
11.测试Nginx服务端口(80)
ss -lnt | grep 80
12.curl测试Nginx首页(有看到Welcome to nginx!就是成功了)
curl -s 本机IP
13.把Nginx添加到启动项(在末尾添加以下电脑命令:#开机启动Nginx nginx)
cd /etc/rc.d && chmod +x rc.local && vim rc.local
14.重新加载nginx配置
nginx -s reload
三、安装MYSQL
1.安装MySQL依赖环境
ncurses-devel提供字符终端处理库
libaio-devel包的作用是为了支持同步I/O
yum install -y bison-devel ncurses-devel libaio-devel gcc gcc-c++ automake autoconf numactl
2.删除mariadb组件
rpm -qa | grep mariadb
[结果1]
[结果2]
……
yum remove [结果1] [结果2] ……
3.下载MySQL
cd /home/application/tools
wget http://tel.mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
4.解压MySQL5.7
tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /home/application
mv /home/application/mysql-5.7.38-linux-glibc2.12-x86_64 /home/application/mysql-5.7.38
ln -s /home/application/mysql-5.7.38/ 电脑 /home/application/mysql
5.创建管理用户&&授权
useradd -s /sbin/nologin -M mysql
chown -R mysql.mysql /home/application/mysql-5.7.38/
6.创建目录和文件并且分配属主和属组
mkdir -p /data/mysql/{data,logs}
touch /data/mysql/logs/{mysql-error.log,mysql-slow.log}
chown -R mysql.mysql /data/mysql
mkdir /var/run/mysql
chown -R mysql.mysql /var/run/mysql
7.初始化数据库
–initialize-insecure选项参数可以不自动生成初始root密码。
/home/application/mysql/bin/mysqld --initialize-insecure --basedir=/home/application/mysql/ --datadir=/data/mysql/data --user=mysql
8.复制脚本及PATH变量赋值
cp /home/application/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
echo 'PATH=$PATH:/home/application/mysql/bin' >> /etc/profile
source /etc/profile
9.编辑配置文件
vim /etc/my.cnf
[mysql]
# CLIENT #
port = 3306
socket = /tmp/mysql.sock
[mysqld]
# GENERAL #
user = mysql
default-storage-engine = InnoDB
socket = /tmp/mysql.sock
pid-file = /var/run/mysql/mysql.pid
basedir = /home/application/mysql
server-id = 1
port = 3306
default-time-zone = '+08:00'
# MyISAM #
key-buffer-size = 32M
myisam-recover-options = FORCE,BACKUP
# SAFETY #
max-allowed-packet = 16M
max-connect-errors = 1000000
skip-name-resolve
#sql-mode = STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,NO_AUTO_VALUE_ON_ZERO
sql-mode = ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,NO_AUTO_VALUE_ON_ZERO
sysdate-is-now = 1
innodb = FORCE
secure-file-priv = ''
# DATA STORAGE #
datadir = /data/mysql/data/
# BINARY LOGGING #
log-bin = /data/mysql/data/mysql-bin
expire-logs-days = 14
binlog-format = mixed
sync-binlog = 1
#GTID:
gtid-mode = on
enforce-gtid-consistency = on
# CACHES AND LIMITS #
tmp-table-size = 32M
max-heap-table-size = 32M
query-cache-type = 0
query-cache-size = 0
max-connections = 500
thread-cache-size = 50
open-files-limit = 65535
table-definition-cache = 1024
table-open-cache = 200
# INNODB #
innodb-flush-method = O_DIRECT
innodb-log-files-in-group = 2
innodb-log-file-size = 256M
innodb-flush-log-at-trx-commit = 1
innodb-file-per-table = 1
innodb-buffer-pool-size = 8G
# LOGGING #
log-error = /data/mysql/logs/mysql-error.log
log-queries-not-using-indexes = 1
slow-query-log = 1
slow-query-log-file = /data/mysql/logs/mysql-slow.log
10.启动MySQL
/etc/init.d/mysqld start
11.初始化root密码
mysql_secure_installation
==========================================================================
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: n
New password: <==此处输入新密码(如果低于八位数密码,稍后会有系统提示)
Re-enter new password: <==此处重复输入新密码
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
12.把MySQL添加进启动项
cd /etc/rc.d && chmod +x rc.local && vim rc.local
#开机启动mysql
mkdir -p /var/run/mysql
chown -R mysql.mysql /var/run/mysql
/etc/init.d/mysqld start
14.重启MySQL
/etc/init.d/mysqld restart
四、安装PHP
1.预先安装必要的依赖
yum install -y wget gcc gcc-c++ autoconf automake libtool make libxml2 libxml2-devel openssl openssl-devel sqlite-devel libcurl
2.下载解压PHP
cd /home/application/tools
wget wget https://www.php.net/distributions/php-8.1.11.tar.gz
tar xf php-8.1.11.tar.gz -C /home/application
3.编译配置php
./configure --prefix=/home/application/php-8.1.11 --with-config-file-path=/home/application/php-8.1.11/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mysqlnd-compression-support --with-zlib --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --with-curl --enable-mbregex --enable-mbstring --enable-intl --enable-ftp --enable-gd-jis-conv --with-openssl --with-mhash --enable-pcntl --enable-sockets --enable-soap --with-gettext --enable-fileinfo --enable-opcache --with-pear --with-ldap=shared --without-gdbm
编译报错处理
错误1
checking for libcurl >= 7.29.0... no
configure: error: Package requirements (libcurl >= 7.29.0) were not met:
No package 'libcurl' found
解决方案
yum search libcurl
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.ustc.edu.cn
* extras: mirrors.ustc.edu.cn
* updates: mirrors.ustc.edu.cn
======== N/S matched: libcurl =============
libcurl-devel.i686 : Files needed for building applications with libcurl
libcurl-devel.x86_64 : Files needed for building applications with libcurl
libcurl.i686 : A library for getting files from web servers
libcurl.x86_64 : A library for getting files from web servers
perl-WWW-Curl.x86_64 : Perl extension interface for libcurl
python-pycurl.x86_64 : A Python interface to libcurl
发现 libcurl-devel.x86_64 库需要安装。
yum install -y libcurl-devel.x86_64
错误2
checking for icu-uc >= 50.1 icu-io icu-i18n... no
configure: error: Package requirements (icu-uc >= 50.1 icu-io icu-i18n) were not met:
No package 'icu-uc' found
No package 'icu-io' found
No package 'icu-i18n' found
解决方案
yum -y install libicu-devel
错误3
configure: error: Cannot find ldap.h
解决方案
yum install openldap openldap-devel -y
错误4
configure: error: Cannot find ldap libraries in /usr/lib.
解决方案
cp -frp /usr/lib64/libldap* /usr/lib/
错误5
configure: error: Package requirements (oniguruma) were not met:
解决方案
yum install https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-6.9.8-1.el7.remi.x86_64.rpm
yum install https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-devel-6.9.8-1.el7.remi.x86_64.rpm
重新编译,出现上面的这个代码表示编译成功
4.安装php
make && make install
5.创建软链接(方便以后管理)
ln -s /home/application/php-8.1.11/bin/php /usr/local/bin/php
6.测试
php -v
PHP 8.1.11 (cli) (built: Nov 7 2022 15:35:02) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.11, Copyright (c) Zend Technologies
7.修改配置文件
# 源码包 php-8.0.24
cp /home/application/php-8.1.11/php.ini-production /home/application/php-8.1.11/etc
cd /home/application/php-8.1.11/etc
cp php.ini-production php.ini
cd /home/application/php-8.1.11/etc
cp php-fpm.conf.default php-fpm.conf
cd /home/application/php-8.1.11/php-fpm.d
cp www.conf.default www.conf
# 修改www.conf文件
# 监听方式为sock
;listen = 127.0.0.1:9000
listen = /tmp/www.sock
8.开机自启
# 源码包 php-8.1.11/sapi/fpm
cp /home/application/php-8.1.11/sapi/fpm/php-fpm.service /usr/lib/systemd/system/php-fpm-8.1.11.service
server {
listen 80;
server_name www.paul.test;
root /home/web/wwwroot;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/tmp/www.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
9.启动php-fpm
cp /home/application/php-8.1.11/sapi/fpm/php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
php-fpm的启动参数
#测试php-fpm配置/etc/init.d/php-fpm -t
/etc/init.d/php-fpm -c /home/application/php-8.1.11/etc/php.ini -y /home/application/php-8.1.11/etc/php-fpm.conf -t
#启动php-fpm /etc/init.d/php-fpm
/etc/init.d/php-fpm -c /home/application/php-8.1.11/etc/php.ini -y /home/application/php-8.1.11/etc/php-fpm.conf
五、系统部署
1. 在系统部署之前,务必使系统时间、MySQL时间、php时间与当地时间保持一致,即时区一致,比如都是东八区。
2. 防火墙
查看防火墙是否开启
firewall-cmd --state
如果没有 安装:yum install -y firewalld
启动:systemctl start firewalld
设置开机自启动:systemctl enable firewalld
这里通过开放一下httpd和php80的端口
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --permanent --zone=public --add-port=443/tcp
重新加载防火墙
firewall-cmd --reload
六、验证
vim <网站根目录>/phpinfo.php
按i进入编辑模式输入<?php echo phpinfo(); ?>
按Esc键后,输入:wq并回车,保存关闭文件
在本地Windows主机(同局域网),打开浏览器。
在浏览器的地址栏输入http://新安装的CentOSIP地址/phpinfo.php进行访问。
访问结果如下图所示,表示LNMP环境部署成功。
到这里CentOS7 和 LNMP服务已经基本搭建完毕了!!!
后面会持续更新各服务的自动启动相关脚本 谢谢!!!
感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助!
电脑