Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
Mysql是一个小型关系型数据库管理系统。
PHP是一种在服务器端执行的嵌入HTML文档的脚本语言
lnmp是linux+nginx+mysql+php是针对于访问量很大的web构架,成为一个免费、高效、扩展性强的网站服务系统
系统 | 主机名 | ip | 服务 |
---|---|---|---|
centos8 | nginx | 192.168.136.239 | lnmp |
nginx包下载
#安装环境工具包
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@nginx ~]# yum -y groups mark install 'Development Tools'
#创建系统用户
[root@nginx ~]# useradd -r -M -s /sbin/nologin nginx
#创建日志存放目录
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx
#下载nginx
[root@nginx ~]# cd /usr/src/
[root@nginx src]# wget https://nginx.org/download/nginx-1.20.1.tar.gz
#编译安装nginx
[root@nginx src]# tar -xf nginx-1.20.1.tar.gz
[root@nginx src]# cd nginx-1.20.1/
[root@nginx 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@nginx nginx-1.20.1]# make -j $(grep 'processor' /proc/cpuinfo |wc -l) && make install
#环境变量配置
[root@nginx ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx ~]# source /etc/profile.d/nginx.sh
[root@nginx ~]# which nginx
/usr/local/nginx/sbin/nginx
#开机自启
[root@nginx ~]# cat /usr/lib/systemd/system/nginxd.service
[Unit]
Description=Nginx server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
[Install]
WantedBy=multi-user.target
[root@nginx ~]# systemctl daemon-reload
nginx命令语法
#查看版本
[root@nginx ~]# nginx -v
nginx version: nginx/1.20.1
#查看配置文件语法错误
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
-c:指定配置文件路径
-s:发送控制服务信号,可选值:{stop|quit|reopen|reload}
启动|关闭|重新加载nginx
[root@nginx ~]# nginx //启动
[root@nginx ~]# nginx -s stop //关闭
[root@nginx ~]# nginx -s reload //重启
[root@nginx ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80
mysql编译安装
php编译安装
nginx配置
[root@nginx ~]# cat /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.php index.html index.htm; #添加在开头index.php格式
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; #scripts修改成web访问路径
include fastcgi_params;
}
php配置
[root@nginx ~]# vim /usr/local/php7/etc/php-fpm.d/www.conf
; will be used.
user = nginx
group = nginx
重启所有服务
[root@nginx ~]# systemctl restart nginxd
[root@nginx ~]# service php-fpm reload
[root@nginx ~]# systemctl restart mysqld.service
[root@localhost ~]# tree /tmp/lanmp/
/tmp/lanmp/
├── install_lanmp.sh
└── soft
├── apr-1.7.0.tar.bz2
├── apr-util-1.6.1.tar.bz2
├── httpd-2.4.48.tar.bz2
├── mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
├── nginx-1.20.1.tar.gz
├── nginx.conf
└── php-8.0.10.tar.xz
脚本内容
[root@localhost ~]# cat /tmp/lanmp/install_lanmp.sh
#!/bin/bash
if [ $UID -ne 0 ];then
echo -e "\e[1;31m 请使用管理员身份运行脚本 \e[0m"
fi
#variables
makedir=/usr/local
tardir=/usr/src
packdir=/tmp/lanmp
datadir=/opt/data
read -p "请输入要安装的版本:(默认是lamp,输入其它的字符是lnmp)" count
if [ -z $count ];then
count=1
else
count=2
fi
#安装依赖包
echo -e "\e[1;31m 正在安装依赖包 \e[0m"
yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make epel-release "@Development Tools" ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs gd-devel &>/dev/null
sleep 2
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 &>/dev/null
yum -y install libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel epel-release php-mysqlnd.x86_64 libsqlite3x-devel oniguruma &>/dev/null
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 libzip-devel &>/dev/null
#创建用户
id mysql &>/dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin mysql
fi
#解压包
cd $packdir/soft/
tar -xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C $makedir
tar -xf php-8.0.10.tar.xz -C $tardir
#创建软链接和环境配置
if [ ! -d $makedir/mysql ];then
ln -s $makedir/mysql-5.7.33-linux-glibc2.12-x86_64/ $makedir/mysql
fi
echo "export PATH=/usr/local/mysql/bin:\$PATH" > /etc/profile.d/mysql.sh
chown -R mysql.mysql $makedir/mysql*
#创建数据目录
if [ ! -d $datadir ];then
mkdir $datadir
fi
chown -R mysql.mysql $datadir
#mysql初始化
echo -e "\e[1;31m 正在mysql初始化 \e[0m"
DB=$(ls /opt/data/ |wc -l)
if [ $DB -eq 0 ];then
$makedir/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=$datadir
fi
#mysql配置文件
cat > /etc/my.cnf <<EOF
[mysqld]
user=mysql
port=3306
basedir=$makedir/mysql
datadir=$datadir
pid-file=$datadir/mysql.pid
skip-name-resolve
EOF
#配置mysql脚本
sed -ri "s#^(basedir=).*#\1$makedir/mysql#g" $makedir/mysql/support-files/mysql.server
sed -ri "s#^(datadir=).*#\1$datadir#g" $makedir/mysql/support-files/mysql.server
#mysql.service文件
cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=mysql server daemon
After=network.target mysql-keygen.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
EOF
systemctl daemon-reload
systemctl enable --now mysqld.service
echo -e "\e[1;31m mysql部署成功 \e[0m"
################################################################################################
echo -e "\e[1;31m 正在编译php \e[0m"
cd $tardir/php-8.0.10/
if [ ! -d $makedir/php8 ];then
./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
make && make install
fi
echo "export PATH=$makedir/php8/bin:\$PATH" > /etc/profile.d/php8.sh
cp /usr/src/php-8.0.10/php.ini-production /etc/php.ini
cp /usr/src/php-8.0.10/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/rc.d/init.d/php-fpm
cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
#service文件
cat > /usr/lib/systemd/system/php-fpm.service <<EOF
[Unit]
Description=php server daemon
After=network.target php-keygen.target
[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now php-fpm.service &>/dev/null
echo -e "\e[1;31m php部署成功 \e[0m"
#编译lamp
function lamp(){
#创建用户
id apache &>/dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin apache
fi
#解压包
cd $packdir/soft/
tar -xf apr-1.7.0.tar.bz2 -C $tardir
tar -xf apr-util-1.6.1.tar.bz2 -C $tardir
tar -xf httpd-2.4.48.tar.bz2 -C $tardir
#修改apr包
sed -i '/$RM "$cfgfile"/d' $tardir/apr-1.7.0/configure
echo -e "\e[1;31m 正在编译apache \e[0m"
cd /usr/src/apr-1.7.0/
if [ ! -d $makedir/apr ];then
./configure --prefix=$makedir/apr && make && make install
fi
cd ../apr-util-1.6.1/
if [ ! -d $makedir/apr-util ];then
./configure --prefix=$makedir/apr-util --with-apr=$makedir/apr && make && make install
fi
cd ../httpd-2.4.48/
if [ ! -d $makedir/apache/ ];then
./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
make && make install
fi
#apache 环境配置
echo "export PATH=$makedir/apache/bin:\$PATH" > /etc/profile.d/apache.sh
#service-service文件配置
cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=httpd server daemon
After=network.target httpd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now httpd
echo -e "\e[1;31m httpd自启动成功 \e[0m"
#index.php配置
mkdir -p /usr/local/apache/htdocs/test
chown -R apache.apache /usr/local/apache/htdocs/test
cat > /usr/local/apache/htdocs/test/index.php <<EOF
<?php
phpinfo();
?>
EOF
cat > /etc/httpd24/extra/httpd-vhosts.conf <<EOF
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/test"
ServerName www.clq.com
DirectoryIndex index.php
ProxyRequests OFF
ProxyPassMatch ^/(.*.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/test/\$1
<Directory "/usr/local/apache/htdocs/test">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
EOF
sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf
sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd24/httpd.conf
sed -i '/httpd-vhosts/s/#//g' /etc/httpd24/httpd.conf
grep 'index.php' /etc/httpd24/httpd.conf &>/dev/null
if [ $? -eq 1 ];then
sed -i 's/index.html/index.php index.php/g' /etc/httpd24/httpd.conf
fi
grep 'httpd-php .php' /etc/httpd24/httpd.conf &>/dev/null
if [ $? -eq 1 ];then
sed -i '399iAddType application/x-httpd-php .php' /etc/httpd24/httpd.conf
fi
grep 'httpd-php-source .phps' /etc/httpd24/httpd.conf &>/dev/null
if [ $? -eq 1 ];then
sed -i '400iAddType application/x-httpd-php-source .phps' /etc/httpd24/httpd.conf
fi
systemctl stop firewalld
setenforce 0
systemctl restart httpd
systemctl restart mysqld
systemctl restart php-fpm.service
echo -e "\e[1;31m lamp部署成功 \e[0m"
}
#编译lnmp
function lnmp(){
#创建用户
id nginx &>/dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin nginx
fi
#解压包
cd $packdir/soft/
tar -xf nginx-1.20.1.tar.gz -C $tardir
#编译安装nginx
cd $tardir/nginx-1.20.1/
if [ ! -d $makedir/nginx ];then
./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
make -j $(grep 'processor' /proc/cpuinfo |wc -l) && make install
fi
#环境配置
echo "export PATH=/usr/local/nginx/sbin:\$PATH" > /etc/profile.d/nginx.sh
#nginx开机自启
cat > /usr/lib/systemd/system/nginxd.service <<EOF
[Unit]
Description=nginx server daemon
After=network.target nginx-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP
[Install]
WantedBy=multi-user.target
EOF
#nginx配置
cat > /usr/local/nginx/html/index.php <<EOF
<?php
phpinfo();
?>
EOF
cp $packdir/soft/nginx.conf $makedir/nginx/conf/nginx.conf
#php配置
sed -i '23c user = nginx' /usr/local/php8/etc/php-fpm.d/www.conf
sed -i '24c group = nginx' /usr/local/php8/etc/php-fpm.d/www.conf
systemctl restart nginxd.service
systemctl restart mysqld.service
systemctl restart php-fpm.service
systemctl stop firewalld
setenforce 0
echo -e "\e[1;31m lnmp部署成功 \e[0m"
}
if [ $count -eq 1 ];then
lamp
else
lnmp
fi
/tmp/lanmp/nginx.conf内容
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
端口
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 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 5 [::1]:631 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*