前言
LNMP是一个基于CentOS/Debian编写的Nginx、PHP、MySQL、PHPMyAdmin、eAccelerator一键安装包。可以在VPS、独立主机上轻松的安装LNMP生产环境。
LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
Nginx较为稳定、功能丰富、安装配置简单、低系统资源
Nginx既可以在内部直接支持Rails和PHP,也可以支持作为HTTP代理服务器对外进行服务。Nginx用C编写,不论是系统资源开销还是CPU使用效率都比Perlbal好得多
目录
1.安装nginx
注意关闭防火墙以及Selinux,依赖环境的安装等。
#关闭防火墙,Selinux
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service
# 关闭Selinux 重启生效
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
reboot
#创建软件包存放目录
mkdir -p /opt/tools && cd /opt/tools
#创建不能登录的用户
useradd -M -s /sbin/nologin nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -xvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
#配置
./configure \
--prefix=/usr/local/nginx \
--user=nginx --group=nginx \
--with-debug \
--with-pcre-jit \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_auth_request_module \
--with-http_v2_module \
--with-http_dav_module \
--with-http_slice_module \
--with-threads \
--with-http_addition_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_image_filter_module=dynamic \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_sub_module \
--with-http_xslt_module=dynamic \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-stream_realip_module \
--with-stream
#编译安装
make -j4 && make install
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
#制作service
cat >>/usr/lib/systemd/system/nginx.service<<EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/bin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s TERM \$MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
#刷新文件
systemctl daemon-reload
#nginx开机自启动
systemctl enable nginx.service
2.安装Mysql8.0.29
#下载Mysql源
wget https://repo.mysql.com//mysql80-community-release-el8-4.noarch.rpm
#卸载系统中mariadb
rpm -e {mariadb-server,mariadb,mariadb-errmessage,mariadb-common}
#安装Mysql
[root@localhost mysqlpackage]# yum install mysql-community-server
#启动Mysql
[root@localhost ~]# systemctl start mysqld.service
#查看Mysql初始密码
#root@localhost:后面为默认初始密码
[root@localhost ~]# grep -r password /var/log/mysqld.log
2022-07-15T03:18:40.695138Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: MpkmgH0X+lPZ
#修改初始密码,注意密码要求8位数、大写字母、特殊字符
[root@localhost mysqlpackage]# mysql -uroot -pMpkmgH0X+lPZ
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 8.0.29 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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.
#修改登录密码为Admin.123
mysql> ALTER USER USER() IDENTIFIED BY 'Admin.123';
设置Mysql开机自启
[root@localhost mysqlpackage]# systemctl enable mysqld.service
3.安装PHP
3.1装依赖环境
[root@localhost mysqlpackage]# yum -y install libtool sqlite-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel
3.2安装oniguruma-6.9.4
[root@localhost mysqlpackage]# wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
[root@localhost mysqlpackage]# tar -xvf oniguruma-6.9.4.tar.gz
[root@localhost mysqlpackage]# cd oniguruma-6.9.4/
[root@localhost mysqlpackage]#./autogen.sh && ./configure --prefix=/usr --libdir=/lib64
[root@localhost mysqlpackage]# make && make install
3.3下载安装libmcrypt
[root@localhost mysqlpackage]# wget https://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
[root@localhost mysqlpackage] tar xf libmcrypt-2.5.8.tar.gz
[root@localhost mysqlpackage] cd libmcrypt-2.5.8
[root@localhost mysqlpackage]# ./configure --prefix=/usr/local/libmcrypt
[root@localhost mysqlpackage]# make && make install
3.4编译安装php
cd /opt/tools
[root@localhost tools]# tar -xvf php-7.4.16.tar.gz
[root@localhost tools]# cd php-7.4.16
#编辑PHP的配置项
./configure --prefix=/usr/local/php \
--with-config-file-path=/etc \
--enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx \
--enable-inline-optimization --disable-debug --disable-rpath \
--enable-shared --enable-soap --with-xmlrpc \
--with-openssl --with-mhash --with-sqlite3 \
--with-zlib --enable-bcmath --with-iconv --with-bz2 \
--enable-calendar --with-curl --with-cdb --enable-dom \
--enable-exif --enable-fileinfo --enable-filter \
--enable-ftp --with-openssl-dir --with-zlib-dir \
--enable-gd-jis-conv --with-gettext --with-gmp --with-mhash \
--enable-json --enable-mbstring --enable-mbregex \
--enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
--with-zlib-dir --with-pdo-sqlite --with-readline --enable-session \
--enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg \
--enable-sysvsem --enable-sysvshm --with-xsl \
--enable-mysqlnd-compression-support --with-pear \
--enable-opcache --disable-fileinfo
#编译安装
make -j 3 && make install
4.修改配置
#添加环境变量
echo "export PATH=$PATH:/usr/local/php/bin" >> /etc/profile
source /etc/profile
#准备配置文件
cp php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
ln -s /usr/local/php/etc/ /etc/php
#修改 /usr/local/php/etc/php-fpm.conf 运行用户和组改为nginx
chown nginx.nginx /usr/local/php/etc/php-fpm.conf
chown -R nginx.nginx /etc/php
#禁用PHP功能
sed -i "s#disable_functions =#disable_functions =\"passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd,wnam,posix_getpwuid, posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname\"#" /etc/php/php.ini
#支持mysql
sed -i "s#pdo_mysql.default_socket=#pdo_mysql.default_socket=/var/lib/mysql/mysql.sock
#" /etc/php/php.ini
sed -i "s#mysqli.default_socket =#mysqli.default_socket =/var/lib/mysql/mysql.sock
#" /etc/php/php.ini
#设置开机自启,并启动
cp /opt/tools/php-7.4.16/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
chkconfig php-fpm on
systemctl start php-fpm
5.修改Nginx
#修改nginx
cp /usr/local/nginx/conf/nginx.conf{,.bak}
vim /usr/local/nginx/conf/nginx.conf
...
location / {
root html;
index index.html index.htm index.php; #添加 index.php
}
...
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#将/scripts$fastcgi_script_name修改为$document_root$fastcgi_script_name。
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
...
#检查配置
nginx -t
systemctl start nginx #启动nginx
6.检查服务是否启动
7.测试
#测试
cat > /usr/local/nginx/html/index.php <<eof
<?php
phpinfo();
?>
eof
curl http://localhost/index.php
#浏览器访问测试
http://127.0.0.1/index.php
总结
PHP7.4.16在银河麒麟SP2上已经完全兼容,但是依赖环境较为复杂,需要仔细。