lnmp部署
环境说明
系统 | IP |
---|---|
centos8 | 192.168.200.145 |
1. 关闭防火墙
[root@lnmp ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@lnmp ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@lnmp ~]# setenforce 0
2 安装nginx
//创建系统用户nginx
[root@lnmp ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖环境
[root@lnmp ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@lnmp ~]# yum -y groups mark install 'Development Tools'
//创建日志存放目录
[root@lnmp ~]# mkdir -p /var/log/nginx
[root@lnmp ~]# chown -R nginx.nginx /var/log/nginx
//编译安装nginx
[root@lnmp ~]# cd /usr/src/
[root@lnmp src]# ls
debug mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz php-8.0.10.tar.xz
kernels nginx-1.20.1.tar.gz
[root@lnmp src]# tar xf nginx-1.20.1.tar.gz
[root@lnmp src]# cd nginx-1.20.1
[root@lnmp 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@lnmp nginx-1.20.1]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//配置环境变量
[root@lnmp ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@lnmp ~]# source /etc/profile.d/nginx.sh
//启动nginx
[root@lnmp ~]# /usr/local/nginx/sbin/nginx //默认启动文件
[root@lnmp ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
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 [::]:*
测试nginx访问页面
3. 安装mysql
//安装依赖包
[root@lnmp ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
//创建用户和组
[root@lnmp ~]# useradd -r -M -s /sbin/nologin mysql
//解压mysql软件包
[root@lnmp ~]# cd /usr/src/
[root@lnmp src]# tar xf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@lnmp ~]# cd /usr/local/
[root@lnmp local]# ls
bin games lib libexec nginx share
etc include lib64 mysql-5.7.35-linux-glibc2.12-x86_64 sbin src
[root@lnmp local]# ln -sv mysql-5.7.35-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.35-linux-glibc2.12-x86_64/'
[root@lnmp local]# chown -R mysql.mysql /usr/local/mysql*
//添加环境变量
[root@lnmp ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@lnmp ~]# source /etc/profile.d/mysql.sh
[root@lnmp ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
//头文件软连接
[root@lnmp ~]# ln -s /usr/local/mysql/include /usr/include/mysql
//帮助文档
[root@lnmp ~]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/mysql/man
//库文件
[root@lnmp ~]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@lnmp ~]# ldconfig
//建立数据存放目录
[root@lnmp ~]# mkdir -p /opt/data
[root@lnmp ~]# chown -R mysql.mysql /opt/data/
[root@lnmp ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 10月 26 01:02 data
//初始化数据库
[root@lnmp ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data
//生成配置文件
[root@lnmp ~]# cat /etc/my.cnf #如果这个文件存在,请先备份再修改
[mysqld]
basedir = /usr/local/mysql
datadir = /data
socket = /tmp/mysql.sock
port = 3306
pid-file = /data/mysql.pid
user = mysql
skip-name-resolve
//配置启动服务
[root@lnmp ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /usr/local/mysql/support-files/mysql.server
[root@lnmp ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /usr/local/mysql/support-files/mysql.server
[root@lnmp ~]# cat /usr/lib/systemd/system/mysqld.service
[Unit]
Description=Mysql 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
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
// 启动mysql
[root@lnmp ~]# systemctl daemon-reload
[root@lnmp ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@lnmp ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
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 [::]:*
LISTEN 0 80 *:3306 *:*
//设置数据库密码
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
4. 安装PHP
//先下载epel源
[root@lnmp ~]# yum -y install epel-release
//下载依赖包
[root@lnmp ~]# 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 php-mysqlnd libzip-devel.x86_64 libsqlite3x-devel
[root@lnmp ~]# 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@lnmp ~]# cd /usr/src/
[root@lnmp src]# tar xf php-8.0.10.tar.xz
[root@lnmp src]# cd php-8.0.10
[root@lnmp 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@lnmp php-8.0.10]# make && make install
//设置环境变量
[root@lnmp php-8.0.10]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@lnmp php-8.0.10]# source /etc/profile.d/php.sh
//配置php-fpm
[root@lnmp php-8.0.10]# cp php.ini-production /etc/php.ini
cp:是否覆盖'/etc/php.ini'? y
[root@lnmp php-8.0.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@lnmp php-8.0.10]# chmod +x /etc/init.d/php-fpm
[root@lnmp php-8.0.10]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@lnmp php-8.0.10]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
//启动php-fpm
[root@lnmp ~]# service php-fpm start
Starting php-fpm done
[root@lnmp ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
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 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
5. 配置nginx
//创建php测试页面
[root@lnmp ~]# cat /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
//配置nginx
[root@lnmp ~]# vim /usr/local/nginx/conf/nginx.conf
......
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html index.htm; #添加index.php
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000; #在工作中这里要改为php服务器的地址
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $Document_Root$fastcgi_script_name;
include fastcgi_params;
}
......
//重新加载配置文件
[root@lnmp ~]# nginx -s reload