源码安装LNMP
需要准备的安装包以及下载地址(只是一个大概地址,版本和下载方式需要自行选择):
Nginx http://nginx.org/en/download.html nginx主程序包
MySQL https://dev.mysql.com/downloads/mysql/ mysql主程序包
PHP https://php.net/downloads.php php主程序包
pcre http://pcre.org/ nginx的依赖包,不用安装
boost https://www.boost.org/users/download/ mysql5.7版本以后必须的依赖包
(1).准备安装环境
需要有epel源,yum安装LNMP框架依赖包(大致的一个依赖包):
yum -y install make gcc gcc-c++ flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel gd freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel gettext gettext-devel ncurses-devel gmp-devel unzip libcap lsof php-mcrypt
同时上传下载好的源码包,我使用的是如下版本:boost_1_59_0、mysql-5.7.19、nginx-1.14.1、pcre-8.41、php-7.1.24。
(2).编译安装nginx
1)安装nginx依赖包
yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre*
2)创建nginx专用用户
[root@youxi1 ~]# useradd -M -s /sbin/nologin nginx
3)解压文件,并编译安装
[root@youxi1 ~]# tar -zxf pcre-8.41.tar.gz -C /usr/local/src/ [root@youxi1 ~]# tar -zxf nginx-1.14.1.tar.gz -C /usr/local/src/ [root@youxi1 ~]# cd /usr/local/src/nginx-1.14.1/ [root@youxi1 nginx-1.14.1]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.41 --user=nginx --group=nginx [root@youxi1 nginx-1.14.1]# make -j 4 && make install [root@youxi1 nginx-1.14.1]# echo $? 0
参数说明:
--with-http_dav_module 启用支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)。默认关闭,需要编译开启
--with-http_stub_status_module 启用支持(获取Nginx上次启动以来的工作状态)
--with-http_addition_module 启用支持(作为一个输出过滤器,支持不完全缓冲,分部分相应请求)
--with-http_sub_module 启用支持(允许一些其他文本替换Nginx相应中的一些文本)
--with-http_flv_module 启用支持(提供支持flv视频文件支持)
--with-http_mp4_module 启用支持(提供支持mp4视频文件支持,提供伪流媒体服务端支持)
--with-pcre=/usr/local/src/pcre-8.37 需要注意,这里指的是源码,用#./configure --help |grep pcre查看帮助
4)配置nginx,使其支持php
[root@youxi1 nginx-1.14.1]# cp /usr/local/nginx/conf/nginx.conf{,.bak} [root@youxi1 nginx-1.14.1]# vim /usr/local/nginx/conf/nginx.conf user nginx; //第二行,去除注释并修改用户为nginx location ~ \.php$ { //第65~71行去除注释 root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; //这里的/scripts还要改为网页的主目录。 include fastcgi_params; }
5)配置环境变量,并刷新
[root@youxi1 nginx-1.14.1]# vim /etc/profile.d/nginx.sh export PATH=/usr/local/nginx/sbin:$PATH [root@youxi1 nginx-1.14.1]# . /etc/profile.d/nginx.sh
配置环境变量是为了可以直接调用/usr/local/nginx/sbin/nginx脚本,所以也可以创建一个软链接将脚本直接加入到当前环境变量中,这是一样的效果。操作如下:
[root@youxi1 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/ [root@youxi1 ~]# ll /usr/local/bin/nginx lrwxrwxrwx 1 root root 27 6月 27 12:45 /usr/local/bin/nginx -> /usr/local/nginx/sbin/nginx
这时就可以使用nginx命令了,常用的nginx选项如下(如果不加选项表示启动nginx):
-s [reload|reopen|stop|quit] 重新加载配置|重启|停止|退出 -t 检测配置文件是否有语法错误 -v 显示版本信息 -V 显示版本和配置选项信息
注意:-s reload重新加载配置不会停止nginx,不会影响使用。
注意:重新编译时一定要查看以前的编译配置,只需在原有配置参数后添加新的参数即可。
6)启动nginx并设置开机自启
有两种方法,第一种方法是直接使用nginx命令启动,开机自启是加入到/etc/rc.d/rc.local中。操作如下:
[root@youxi1 ~]# nginx //启动nginx,但没办法查看启动状态 [root@youxi1 ~]# ss -ntlp |grep 80 //这种方法后期想查看nginx的状态时,只能通过ps、netstat和ss命令查看 LISTEN 0 128 *:80 *:* users:(("nginx",pid=1691,fd=6),("nginx",pid=1690,fd=6)) [root@youxi1 ~]# ps aux | grep nginx root 1690 0.0 0.0 18236 640 ? Ss 12:55 0:00 nginx: master process nginx nginx 1691 0.0 0.0 18604 1356 ? S 12:55 0:00 nginx: worker process root 1718 0.0 0.0 112724 984 pts/0 S+ 13:13 0:00 grep --color=auto nginx [root@youxi1 ~]# vim /etc/rc.d/rc.local //添加开机自启 /usr/local/nginx/sbin/nginx //添加一行启动命令 [root@youxi1 ~]# chmod +x /etc/rc.d/rc.local //由于CentOS7中官方将/etc/rc.d/rc.local的开机自启默认禁用,所以需要添加执行权限恢复。
第二种方法,生成一个启动脚本,然后设置开机自启,最后再启动nginx。操作如下:
[root@youxi1 ~]# vim /etc/init.d/nginx //编辑启动脚本 #!/bin/bash # chkconfig: - 99 2 # description: Nginx Service Control Script PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -3 $(cat $PIDF) ;; restart) $0 stop &> /dev/null if [ $? -ne 0 ] ; then continue ; fi $0 start ;; reload) kill -1 $(cat $PIDF) ;; *) echo "Userage: $0 { start | stop | restart | reload }" exit 1 esac exit 0 [root@youxi1 ~]# chmod +x /etc/init.d/nginx //添加执行权限 [root@youxi1 ~]# chkconfig --add nginx //nginx加入到chkconfig管理中 [root@youxi1 ~]# chkconfig --list nginx //输出结果是简写 nginx 0:关 1:关 2:关 3:关 4:关 5:关 6:关 [root@youxi1 ~]# chkconfig nginx on //开启开机自启 [root@youxi1 ~]# chkconfig --list nginx //输出结果是简写 nginx 0:关 1:关 2:开 3:开 4:开 5:开 6:关 [root@youxi1 ~]# init 6 //重启服务器,之后就可以使用systemctl来管理nginx [root@youxi1 ~]# systemctl status nginx ● nginx.service - SYSV: Nginx Service Control Script Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled) Active: active (running) since 四 2019-06-27 13:56:27 CST; 57s ago Docs: man:systemd-sysv-generator(8) Process: 1126 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS) CGroup: /system.slice/nginx.service ├─1143 nginx: master process /usr/local/nginx/sbin/nginx └─1144 nginx: worker process 6月 27 13:56:27 youxi1 systemd[1]: Starting SYSV: Nginx Service Control Sc..... 6月 27 13:56:27 youxi1 systemd[1]: Started SYSV: Nginx Service Control Script. Hint: Some lines were ellipsized, use -l to show in full.
可以不重启服务器,直接使用nginx命令管理。
7)测试
注意:没有关闭防火墙的记得添加端口号
[root@youxi1 ~]# firewall-cmd --permanent --zone=public --add-port=80/tcp success [root@youxi1 ~]# firewall-cmd --reload success [root@youxi1 ~]# firewall-cmd --permanent --zone=public --list-ports 80/tcp
在Windows上查看
(3).编译安装Mysql
1)安装mysql依赖包
yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel
2)卸载系统自带的mysql、mariadb、boost
[root@youxi1 ~]# yum -y remove mysql mariadb* boost*
3)创建mysql专用用户,官方指定的是/bin/false
[root@youxi1 ~]# useradd -M -s /sbin/nologin -r mysql //-M不创建主目录,-s /sbin/nologin不允许登录,-r创建的是系统用户
官方创建mysql专用用户是分成两步写的:groupadd mysql和useradd -M -s /bin/false -r -g mysql mysql。
4)解压文件,准备安装目录和数据目录,并编译安装
[root@youxi1 ~]# tar zxf boost_1_59_0.tar.gz -C /usr/local/src/ [root@youxi1 ~]# tar zxf mysql-5.7.19.tar.gz -C /usr/local/src/ [root@youxi1 ~]# mkdir -p /var/lib/mysql/data //准备安装目录和数据目录 [root@youxi1 ~]# chown -R mysql.mysql /var/lib/mysql/ //修改所属主和所属组 [root@youxi1 ~]# cd /usr/local/src/mysql-5.7.19/ [root@youxi1 mysql-5.7.19]# cmake -DCMAKE_INSTALL_PREFIX=/var/lib/mysql \ -DMYSQL_DATADIR=/var/lib/mysql/data \ -DSYSCONFDIR=/etc \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \ -DMYSQL_TCP_PORT=3306 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DEXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DDOWNLOAD_BOOST=1 \ -DWITH_BOOST=/usr/local/src/boost_1_59_0 [root@youxi1 mysql-5.7.19]# make -j 4 && make install [root@youxi1 mysql-5.7.19]# echo $? 0
参数说明:
DCMAKE_INSTALL_PREFIX:指定MySQL程序的安装目录,默认/usr/local/mysql
DMYSQL_DATADIR:指定MySQL程序的数据目录
DSYSCONFDIR:初始化参数文件目录
DWITH_xxx_STORAGE_ENGINE:指定静态编译到mysql的存储引擎,MyISAM,MERGE,MEMBER以及CSV四种引擎默认即被编译至服务器,不需要特别指定。
DWITH_READLINE:使用readline功能
DMYSQL_UNIX_ADDR:socket文件路径,默认/tmp/mysql.sock
DMYSQL_TCP_PORT:服务端口号,默认3306
DENABLED_LOCAL_INFILE:指定是否允许本地执行LOAD DATA INFILE,默认OFF
DEFAULT_CHARSET:指定服务器默认字符集,默认latin1
DEFAULT_COLLATION:指定服务器默认的校对规则,默认latin1_general_ci
DWITH_BOOST:指定boost的地址
DWITHOUT_xxx_STORAGE_ENGINE:指定不编译的存储引擎
DWITH_COMMENT:指定编译备注信息
注意:在生成环境中,安装数据库之前,需要规划好数据存储的目录。这个目录最好是一块单独的分区或者磁盘,做成raid或者LVM,以便日后磁盘的维护和扩容。另外对于读写比较频繁的业务,可以采用SSD等转速高的磁盘。
5)编辑配置文件/etc/my.cnf
[root@youxi1 mysql-5.7.19]# vim /etc/my.cnf [mysqld] basedir=/var/lib/mysql datadir=/var/lib/mysql/data port=3306 socket=/var/lib/mysql/mysql.sock character-set-server=utf8 log-error=/var/log/mysqld.log pid-file=/tmp/mysqld.pid [mysql] socket=/var/lib/mysql/mysql.sock [client] socket=/var/lib/mysql/mysql.sock
6)配置环境变量并刷新
[root@youxi1 mysql-5.7.19]# vim /etc/profile.d/mysql.sh export PATH=/var/lib/mysql/bin:$PATH [root@youxi1 mysql-5.7.19]# . /etc/profile.d/mysql.sh
7)生成启动脚本,设置开机自启
[root@youxi1 mysql-5.7.19]# cp /var/lib/mysql/support-files/mysql.server /etc/init.d/mysqld [root@youxi1 mysql-5.7.19]# chmod +x /etc/init.d/mysqld [root@youxi1 mysql-5.7.19]# chkconfig --add mysqld [root@youxi1 mysql-5.7.19]# chkconfig --list mysqld mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关 [root@youxi1 mysql-5.7.19]# chkconfig --level 2345 mysqld on //如果上面显示的2345全是关,使用这个命令就好了
注意:重新启动服务器或mysqld添加到开机自启,systemctl start|restart|status|stop mysqld就都可以使用了,但没有办法enbale,因为这不是一个本地服务。
8)初始化数据库,并启动mysqld
[root@youxi1 mysql-5.7.19]# /var/lib/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/var/lib/mysql --datadir=/var/lib/mysql/data [root@youxi1 mysql-5.7.19]# /etc/init.d/mysqld start Starting MySQL..... ERROR! The server quit without updating PID file (/tmp/mysqld.pid).
处理这个错误需要删除数据目录,重新初始化
[root@youxi1 mysql-5.7.19]# rm -rf /var/lib/mysql/data/* [root@youxi1 mysql-5.7.19]# /var/lib/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/var/lib/mysql --datadir=/var/lib/mysql/data [root@youxi1 mysql-5.7.19]# /etc/init.d/mysqld start Starting MySQL. SUCCESS!
9)此时的mysql是没有密码直接可以登录的,使用mysql_secure_installation安全设置初始化
[root@youxi1 mysql-5.7.19]# mysql_secure_installation Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No: y There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0 //设置密码复杂度 Please set the password for root here. New password: //默认最小密码长度为8 Re-enter new password: Estimated strength of the password: 50 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : ... skipping. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : ... skipping. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : ... skipping. 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) : ... skipping. All done!
10)测试
[root@youxi1 mysql-5.7.19]# mysql -uroot -p12345678 mysql>
(4).编译安装php
1)安装php的依赖包
yum -y install php-mcrypt libmcrypt libmcrypt-devel php-pear libxml2 libxml2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype-devel
2)回到文件上传地址,解压文件并安装
[root@youxi1 mysql-5.7.19]# cd [root@youxi1 ~]# tar zxf php-7.1.24.tar.gz -C /usr/local/src/ [root@youxi1 ~]# cd /usr/local/src/php-7.1.24/ [root@youxi1 php-7.1.24]# ./configure --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/ --enable-fpm \ --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \ --with-iconv-dir --with-freetype-dir \ --with-jpeg-dir --with-png-dir --with-zlib \ --with-libxml-dir=/usr --enable-xml --disable-rpath \ --enable-bcmath --enable-shmop --enable-sysvsem \ --enable-inline-optimization --with-curl \ --enable-mbregex --enable-mbstring --with-mcrypt \ --enable-ftp --with-gd --enable-gd-native-ttf \ --with-openssl --with-mhash --enable-pcntl \ --enable-sockets --with-xmlrpc --enable-zip \ --enable-soap --without-pear --with-gettext \ --disable-fileinfo --enable-maintainer-zts [root@youxi1 php-7.1.24]# make -j 4 && make install [root@youxi1 php-7.1.24]# echo $? 0
3)生成php和php-fpm的配置文件
[root@youxi1 php-7.1.24]# cp php.ini-production /usr/local/php/php.ini [root@youxi1 php-7.1.24]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf
4)修改php-fpm配置文件中的运行用户和运行组为nginx
[root@youxi1 php-7.1.24]# vim /usr/local/php/etc/php-fpm.conf user = nginx //第23~24行,nobody改为nginx group = nginx
5)生成php-fpm启动脚本,并添加开机自启
[root@youxi1 php-7.1.24]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@youxi1 php-7.1.24]# chmod +x /etc/init.d/php-fpm [root@youxi1 php-7.1.24]# chkconfig --add php-fpm //添加到chkconfig管理中 [root@youxi1 php-7.1.24]# chkconfig --list php-fpm //查看是否开机自启 php-fpm 0:关 1:关 2:开 3:开 4:开 5:开 6:关 [root@youxi1 php-7.1.24]# chkconfig php-fpm on //如果以上没有开机自启,再使用这条命令
6)启动php-fpm
[root@youxi1 php-7.1.24]# systemctl start php-fpm //启动php-fpm [root@youxi1 php-7.1.24]# systemctl status php-fpm //查看php-fpm状态 ● php-fpm.service - LSB: starts php-fpm Loaded: loaded (/etc/rc.d/init.d/php-fpm; bad; vendor preset: disabled) Active: active (running) since 五 2019-06-28 10:39:36 CST; 7s ago Docs: man:systemd-sysv-generator(8) Process: 3201 ExecStart=/etc/rc.d/init.d/php-fpm start (code=exited, status=0/SUCCESS) CGroup: /system.slice/php-fpm.service ├─3203 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) ├─3204 php-fpm: pool www └─3205 php-fpm: pool www 6月 28 10:39:35 youxi1 systemd[1]: Starting LSB: starts php-fpm... 6月 28 10:39:36 youxi1 php-fpm[3201]: Starting php-fpm done 6月 28 10:39:36 youxi1 systemd[1]: Started LSB: starts php-fpm. [root@youxi1 php-7.1.24]# ss -antup | grep php-fpm //查看是否启动成功 tcp LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=3205,fd=5),("php-fpm",pid=3204,fd=5),("php-fpm",pid=3203,fd=7))
当然还可以使用/etc/init.d/php-fpm start启动。
注意:systemctl不能管理php-fpm的开机自启。
7)修改nginx的配置文件(上面有提到过),然后重启
[root@youxi1 php-7.1.24]# vim /usr/local/nginx/conf/nginx.conf location ~ \.php$ { //第65~71行 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; } [root@youxi1 php-7.1.24]# nginx -s reload
8)测试
创建测试界面
[root@youxi1 php-7.1.24]# vim /usr/local/nginx/html/index.php <?php phpinfo();?>
在Windows上查看