[Nginx] install lnmp environment in centos6.5, nginxlnmp
This article is an original CSDN blog from a bit drizzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.
Preface
I haven't written a blog for a long time. It's really bad. I feel more and more things are coming into contact. If I don't record them, I will slowly forget them. Because of service requirements, apache's load capacity can no longer meet the current needs, so we decided to use nginx as the server to carry more interface access.
Preparations
One centos6.5 virtual machine, reinstalled system, nothing else
Nginx
Update Source
Because yum does not support nginx, you need to update it yourself.
[root@localhost ~]# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpmRetrieving http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpmwarning: /var/tmp/rpm-tmp.piHjNP: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEYPreparing... ########################################### [100%] 1:nginx-release-centos ########################################### [100%][root@localhost ~]# yum info nginxLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfile * base: mirrors.cug.edu.cn * extras: mirrors.cug.edu.cn * updates: mirrors.cug.edu.cnnginx | 2.9 kB 00:00nginx/primary_db | 34 kB 00:00Available PackagesName : nginxArch : x86_64Version : 1.6.2Release : 1.el6.ngxSize : 336 kRepo : nginxSummary : High performance web serverURL : http://nginx.org/License : 2-clause BSD-like licenseDescription : nginx [engine x] is an HTTP and reverse proxy server, as well as : a mail proxy server.
Install nginx
[root@localhost ~]# yum install nginxLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfile * base: mirrors.cug.edu.cn * extras: mirrors.cug.edu.cn * updates: mirrors.cug.edu.cnSetting up Install ProcessResolving Dependencies--> Running transaction check---> Package nginx.x86_64 0:1.6.2-1.el6.ngx will be installed--> Finished Dependency ResolutionDependencies Resolved================================================================================ Package Arch Version Repository Size================================================================================Installing: nginx x86_64 1.6.2-1.el6.ngx nginx 336 kTransaction Summary================================================================================Install 1 Package(s)Total download size: 336 kInstalled size: 828 kIs this ok [y/N]: yDownloading Packages:nginx-1.6.2-1.el6.ngx.x86_64.rpm | 336 kB 00:00Running rpm_check_debugRunning Transaction TestTransaction Test SucceededRunning TransactionWarning: RPMDB altered outside of yum. Installing : nginx-1.6.2-1.el6.ngx.x86_64 1/1----------------------------------------------------------------------Thanks for using nginx!Please find the official documentation for nginx here:* http://nginx.org/en/docs/Commercial subscriptions for nginx are available on:* http://nginx.com/products/---------------------------------------------------------------------- Verifying : nginx-1.6.2-1.el6.ngx.x86_64 1/1Installed: nginx.x86_64 0:1.6.2-1.el6.ngxComplete!
Set nginx to start and start the service
[Root @ localhost ~] # Chkconfig nginx on [root @ localhost ~] # Service nginx start is starting nginx: [OK]
I tried it. I didn't think it was successful. I didn't configure a firewall. So I stopped the firewall and then restarted nginx.
[Root @ localhost ~] # Service iptables stopiptables: Set the chain to the policy ACCEPT: filter [OK] iptables: Clear the firewall rule: [OK] iptables: uninstalling the module: [OK] [root @ localhost ~] # Service nginx start [root @ localhost ~] # Service nginx restart stop nginx: [OK] Starting nginx: [OK]
Nginx is installed successfully!
Install MySQL directly on the MySQL client and server
[root@localhost ~]# yum install mysql mysql-server
Start the service
[Root @ localhost ~] # Service mysqld start initialize MySQL database: Installing MySQL system tables... OKFilling help tables... OKTo start mysqld at boot time you have to copysupport-files/mysql. server to the right place for your systemPLEASE remember to set a password for the MySQL root USER! To do so, start the server, then issue the following commands: /usr/bin/mysqladmin-u root password 'new-password'/usr/bin/mysqladmin-u root-h localhost. localdomain password 'new-password' Alternatively you can run:/usr/bin/mysql_secure_installationwhich will also give you the option of removing the testdatabases and anonymous user created by default. this isstronugly recommended for production serv Ers. see the manual for more instructions. you can start the MySQL daemon with: cd/usr;/usr/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.plcd/usr/mysql-test; perl mysql-test-run.plPlease report any problems with the/usr/bin/mysqlbug script! [OK] Starting mysqld: [OK]
Set to start mysql at startup and back up the configuration file
[Root @ localhost ~] # Chkconfig mysqld on [root @ localhost ~] # Cp/usr/share/mysql/my-medium.cnf/etc/my. cnfcp: overwrite "/etc/my. cnf "? Y
Set the root user password
[root@localhost ~]# mysql_secure_installationNOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!In order to log into MySQL to secure it, we'll need the currentpassword for the root user. If you've just installed MySQL, andyou haven't set the root password yet, the password will be blank,so you should just press enter here.Enter current password for root (enter for none):OK, successfully used password, moving on...Setting the root password ensures that nobody can log into the MySQLroot user without the proper authorisation.Set root password? [Y/n] yNew password:Re-enter new password:Password updated successfully!Reloading privilege tables.. ... Success!By default, a MySQL installation has an anonymous user, allowing anyoneto log into MySQL without having to have a user account created forthem. This is intended only for testing, and to make the installationgo a bit smoother. You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] ... Success!Normally, root should only be allowed to connect from 'localhost'. Thisensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] ... Success!By default, MySQL comes with a database named 'test' that anyone canaccess. This is also intended only for testing, and should be removedbefore moving into a production environment.Remove test database and access to it? [Y/n] - Dropping test database... ... Success! - Removing privileges on test database... ... Success!Reloading the privilege tables will ensure that all changes made so farwill take effect immediately.Reload privilege tables now? [Y/n] ... Success!Cleaning up...All done! If you've completed all of the above steps, your MySQLinstallation should now be secure.Thanks for using MySQL!
Restart mysql
[Root @ localhost ~] # Service mysqld restart stop mysqld: [OK] Starting mysqld: [OK]
Install PHP and install php-fpm
As a server, Nginx does not support php. We need a php-fpm to support php parsing.
[root@localhost ~]# yum install php php-fpm
Install php Extension
[root@localhost ~]# yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt
Several extensions are not installed. Please note!
Setting up Install ProcessNo package php-mcrypt available.No package php-mhash available.No package libmcrypt available.
Start php-fpm and php-fpm
[Root @ localhost ~] # Chkconfig php-fpm on [root @ localhost ~] # Service php-fpm start is starting php-fpm: [OK]
Environment configuration nginx supports php
Backup configuration file
[root@localhost ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak
Modify configuration file
[root@localhost ~]# vi /etc/nginx/nginx.conf
Modify the row of the user
user nginx nginx;
Backup configuration file, Edit
[root@localhost ~]# cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.confbak[root@localhost ~]# vi /etc/nginx/conf.d/default.conf
Configuration file after simple modification
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { root /usr/share/nginx/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 /usr/share/nginx/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 $document_root$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; #}}
Restart Nginx
Modify PHP configurations
[root@localhost ~]# vi /etc/php.ini
Date. timezone = PRC # Remove the semicolon in front of row 946 and change it to date. timezone = region = 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, Region Ror, delimiter, posix_getgrnam, posix_getgroups, posix_getlogin, delimiter, delimiter, posix_getpid, delimiter, delimiter, posix_getpwuid, delimiter, posix_getsid, posix_getuid, delimiter, posix_kill, posix_mkfifo, posix_setegid, posix_seteuid, posix_setgid, posix_setpgid, posix_setsid, posix_setuid, posix_strerror, posix_times, posix_t Tyname, posix_uname # list functions that can be disabled by PHP in row 386. If some programs need this function, they can be deleted and disabled. Expose_php = Off # disable the display of php version information in row 432 magic_quotes_gpc = On # enable magic_quotes_gpc in row 745 to prevent SQL injection into short_open_tag = ON # support for php short label open_basedir =. :/tmp/# Set in row 380 to allow access to the current directory (that is, the directory where the PHP script file is located) And/tmp/directory, which can prevent php trojans from being cross-site, if there is a problem with the installation program after the change (for example, zhimeng Content Management System), you can log out of this line or directly write the program directory/data/www.osyunwei.com/:/tmp/
Save
Modify php-fpm Configuration
Back up the configuration file and modify it.
[root@localhost ~]# cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.confbak[root@localhost ~]# vi /etc/php-fpm.d/www.conf
; Unix user/group of processes; Note: The user is mandatory. If the group is not set, the default user's group; will be used.; RPM: apache Choosed to be able to access some dir as httpduser = nginx; RPM: Keep a group allowed to write in log dir.group = nginx
Test
Write the test file in the directory where nginx is located, Set permissions, and restart nginx and php-prm
[Root @ localhost html] # vi info. php [root @ localhost html] # chown nginx. nginx/usr/share/nginx/html-R [root @ localhost html] # service nginx restart stop nginx: [OK] Starting nginx: [OK] [root @ localhost html] # service php-fpm restart stop php-fpm: [OK] Starting php-fpm: [27-Dec-2014 13:11:21] NOTICE: PHP message: PHP Warning: Directive 'Magic _ quotes_gpc 'is deprecated in PHP 5.3 and greater in Unknown on line 0 [OK]
An error occurred while accessing the PHP file.
File not found.
Solve the problem
Configuration File error. The following is the configuration file for successful debugging.
[root@localhost html]# vi /etc/nginx/conf.d/default.confserver { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { root /usr/share/nginx/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 /usr/share/nginx/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 /usr/share/nginx/html; include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/share/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; #}}
Configuration complete
Postscript
After completing the basic configuration, you need to consider some issues.
- Virtual Machine Configuration
- Url rewriting
- Php extension Installation
- Matching with Server Load balancer