In recent years, WeChat public accounts have become one of the important platforms for communication and promotion for many companies and individuals. In order to provide better services, many public accounts have begun to use node.js for development. In order to ensure that the official account can operate normally, the configuration of the server environment is particularly important. This article will introduce how to use nginx to deploy node.js WeChat official account.
1. Preparation
Before deployment, we need to ensure that we have completed the following preparations:
1. Own a domain name
In order to The official account is more formal and professional, and we need to have a domain name. Since WeChat requires the official account's server to support the https protocol, we need to purchase an SSL certificate for our domain name. It is recommended to use Let's Encrypt free certificate.
2. Install Node.js and pm2
Node.js is the running environment of our WeChat official account, and pm2 is a simple and powerful Node.js process manager that can ensure Processes are always running and can be easily monitored and managed.
3. Install Nginx
Nginx is a high-performance HTTP and reverse proxy server that can be used to host web applications and provide web services. We will use Nginx to reverse proxy the Node.js application.
2. Deployment
1. Deploy Node.js application
First, we need to deploy our Node.js application to the server. Use pm2 to run the application as a daemon.
We can use the following command to run our program on the server:
$ pm2 start app.js
Among them, app.js is the entry file of our Node.js application.
2. Configure Nginx
Next, we need to modify Nginx’s configuration file in order to forward requests to our Node.js application.
Open the Nginx configuration file:
$ sudo nano /etc/nginx/nginx.conf
Add the following code snippet to http { }:
server { listen 80; server_name example.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name example.com; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # WebSocket support proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; } }
Among them, example.com needs to be replaced with our domain name; /etc/ letsencrypt/live/example.com/fullchain.pem and /etc/letsencrypt/live/example.com/privkey.pem are the paths to the Let's Encrypt free SSL certificate we installed; http://localhost:3000 is our Node.js The port number where the application runs is modified according to the actual situation.
Save and exit the configuration file.
3. Restart the Nginx server
Restart the Nginx server to make the new configuration file take effect:
$ sudo service nginx restart
Now, we have successfully deployed our Node.js application Once on the server, use Nginx as a reverse proxy to receive HTTP requests and forward them to our application.
3. Test
In order to test whether our WeChat official account is running normally, we can use ngrok to map the local localhost:3000 port to the public network. The specific usage method is as follows:
1. Download ngrok tool
$ wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
2. Unzip
$ unzip ngrok-stable-linux-amd64.zip
3. Run
$ ./ngrok http 3000
At this time we will get a Public network address, use this address to set the server configuration in developer mode.
4. Conclusion
In this article, we take the deployment of WeChat public accounts as an example to introduce how to use nginx to deploy node.js applications. By using nginx's reverse proxy technology, we can forward http requests to node.js applications, thereby improving the availability and stability of the system, and also strengthening the security of the system. Whether it is an individual or a company, it is necessary to understand and master such a practical and high-performance technology.
The above is the detailed content of nginx deploys nodejs WeChat public account. For more information, please follow other related articles on the PHP Chinese website!